Created
March 23, 2024 07:02
-
-
Save kammce/f49d2e2061f9ce5ac414c86e360ad511 to your computer and use it in GitHub Desktop.
Command to automate deploying sweeping changes through github repos. Pass it a list of github URLs, absolute path to a script and a commit message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if enough arguments are passed | |
if [ "$#" -lt 3 ]; then | |
echo "Usage: $0 <file_with_repo_urls> <path_to_script> <'commit_message'>" | |
exit 1 | |
fi | |
REPO_FILE=$1 | |
SCRIPT_PATH=$2 | |
COMMIT_MESSAGE="[skip ci] $3" | |
# Check if the script file exists and is executable | |
if [ ! -x "$SCRIPT_PATH" ]; then | |
echo "The script at $SCRIPT_PATH does not exist or is not executable." | |
exit 2 | |
fi | |
# Initialize an empty array | |
repo_urls=() | |
# Read file line by line into the array | |
while IFS= read -r line || [[ -n "$line" ]]; do | |
repo_urls+=("$line") | |
done < "$REPO_FILE" | |
# Check if the array is empty | |
if [ ${#repo_urls[@]} -eq 0 ]; then | |
echo "The list file is empty." | |
exit 3 | |
fi | |
# Iterate over the array | |
for repo_url in "${repo_urls[@]}"; do | |
# Extract repo name for directory cloning and branching | |
repo_name=$(basename "$repo_url" .git) | |
echo "Cloning $repo_url..." | |
git clone "$repo_url" "$repo_name" | |
cd "$repo_name" | |
# Create a new branch for the commit | |
branch_name="update-$(date +%Y%m%d%H%M%S)" | |
git checkout -b "$branch_name" | |
# Run the script on the cloned repo | |
echo "Running script on $repo_name..." | |
"$SCRIPT_PATH" | |
# Commit the changes, if any | |
if [ -n "$(git status --porcelain)" ]; then | |
git add . | |
git commit -m "$COMMIT_MESSAGE" | |
git push origin "$branch_name" | |
# Use GitHub CLI to create a pull request | |
gh pr create --base main --head "$branch_name" --title "${COMMIT_MESSAGE:0:50}" --body "Automated changes by script" | |
else | |
echo "No changes in $repo_name. No commit or PR will be created." | |
fi | |
# Cleanup: Go back to the original directory and remove the cloned repo | |
cd .. | |
rm -rf "$repo_name" | |
done < "$REPO_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment