Skip to content

Instantly share code, notes, and snippets.

@japaz
Created February 18, 2025 07:59
Show Gist options
  • Select an option

  • Save japaz/9051de7eb0e3ed589fefaa06de203f4f to your computer and use it in GitHub Desktop.

Select an option

Save japaz/9051de7eb0e3ed589fefaa06de203f4f to your computer and use it in GitHub Desktop.
Script to sync multiple git repositories from master to a given branch
#!/bin/bash
# Description: Syncs target branch with latest master, trying multiple clone paths if needed
# Usage: ./sync-branches.sh <target-branch> <repo1> <repo2> ...
set -eo pipefail
# Formatting
BOLD="\033[1m"
CYAN="\033[0;36m"
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
RESET="\033[0m"
# Configuration
SOURCE_BRANCH="master"
REMOTE="origin"
GITLAB_DEVELOPMENT="[email protected]:flywire/development"
GITLAB_SRE="[email protected]:flywire/sre"
show_help() {
echo -e "${BOLD}${CYAN}Usage:${RESET} $0 ${BOLD}<target-branch>${RESET} ${BOLD}<repo1>${RESET} [repo2] ..."
echo -e "Example: $0 poc-sandemo frontend backend"
}
# Validate arguments
if [[ $# -lt 2 ]]; then
echo -e "${RED}✖ Error: You must specify a target branch and at least one repository${RESET}"
show_help
exit 1
fi
TARGET_BRANCH="$1"
shift # Remove target branch from arguments
# Process repositories
for repo_name in "$@"; do
echo -e "\n${BOLD}${CYAN}=== Processing: ${repo_name} ===${RESET}"
# Auto-cloning with fallback
if [[ ! -d "$repo_name" ]]; then
echo -e "${YELLOW}⚡ Directory not found, attempting to clone...${RESET}"
# Try development path first
clone_url="${GITLAB_DEVELOPMENT}/${repo_name}.git"
if git clone "$clone_url" "$repo_name" --quiet 2>/dev/null; then
echo -e "${GREEN}✔ Cloned from development group${RESET}"
else
# Fallback to SRE path
echo -e "${YELLOW}⚠ Failed in development, trying SRE...${RESET}"
clone_url="${GITLAB_SRE}/${repo_name}.git"
if git clone "$clone_url" "$repo_name" --quiet 2>/dev/null; then
echo -e "${GREEN}✔ Cloned from SRE group${RESET}"
else
echo -e "${RED}✖ Failed to clone from:${RESET}"
echo -e "${RED} - ${GITLAB_DEVELOPMENT}/${repo_name}.git"
echo -e "${RED} - ${GITLAB_SRE}/${repo_name}.git${RESET}"
continue
fi
fi
fi
# Validate Git repository
if [[ ! -d "${repo_name}/.git" ]]; then
echo -e "${RED}✖ Not a Git repository: ${repo_name}${RESET}"
continue
fi
# Execute Git operations
pushd "$repo_name" >/dev/null
echo -e "${YELLOW}⬇ Fetching changes from ${SOURCE_BRANCH}...${RESET}"
if ! git fetch "$REMOTE" "$SOURCE_BRANCH" --quiet 2>/dev/null; then
echo -e "${RED}✖ Failed to fetch changes in ${repo_name}${RESET}"
popd >/dev/null
continue
fi
echo -e "${YELLOW}⬆ Pushing to ${TARGET_BRANCH}...${RESET}"
if git push "$REMOTE" "${REMOTE}/${SOURCE_BRANCH}:${TARGET_BRANCH}" --force \
-o ci.variable="SKIP_MANUAL=true" \
-o ci.variable="FULL_PIPELINE=true" --quiet 2>/dev/null; then
echo -e "${GREEN}✔ ${TARGET_BRANCH} updated in ${repo_name}${RESET}"
else
echo -e "${RED}✖ Failed to push in ${repo_name}${RESET}"
fi
popd >/dev/null
done
echo -e "\n${BOLD}${GREEN}✅ Process completed!${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment