name: Git Repository Mirroring on: workflow_dispatch: inputs: branches: description: 'Comma-separated list of sourceRepo/branch entries to mirror (e.g., dev_Net8.git/Develop_Net8,dev.git/develop). If empty, all entries from branch_list file will be mirrored.' required: false default: '' schedule: - cron: '0 2 * * *' # Runs every day at 2:00 AM jobs: mirror: runs-on: ubuntu-latest timeout-minutes: 360 # 6 hours timeout steps: - name: Checkout uses: actions/checkout@v4 - name: Set up SSH env: SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} run: | mkdir -p ~/.ssh echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan -p 22 172.16.42.118 >> ~/.ssh/known_hosts - name: Mirror Branches env: BASE_GITEA_TOKEN: ${{ secrets.BASE_GITEA_TOKEN }} BASE_GITEA_URL: ${{ vars.BASE_GITEA_URL }} # e.g., https://gitea.example.com BASE_GITEA_USER: ${{ vars.BASE_GITEA_USER }} # The user who owns the token INPUT_BRANCHES: ${{ github.event.inputs.branches }} run: | set -euo pipefail CENTER_ORG="center_dev" AUTH_HEADER="Authorization: token ${BASE_GITEA_TOKEN}" SOURCE_SSH_HOST="engdev@172.16.42.118" set_default_branch_main() { local repo_name="$1" local response http_status body response=$(curl -s -w "\n%{http_code}" -X PATCH -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") http_status=$(echo "${response}" | tail -n1) body=$(echo "${response}" | sed '$d') if [[ "${http_status}" != "200" ]]; then echo "::warning::Failed to set default branch to 'main' for ${CENTER_ORG}/${repo_name} (status ${http_status})" if [[ -n "${body}" ]]; then echo "${body}" fi else echo "Default branch set to 'main' for ${CENTER_ORG}/${repo_name}" fi } process_entry() { local entry branch_name source_repo source_repo_url entry="$(echo "$1" | xargs)" # trim whitespace if [[ -z "$entry" ]]; then return fi if [[ "$entry" != */* ]]; then echo "::warning::Entry '${entry}' is missing sourceRepo/branch format. Skipping." return fi source_repo="${entry%%/*}" branch_name="${entry#*/}" if [[ -z "$source_repo" || -z "$branch_name" ]]; then echo "::warning::Invalid entry '${entry}'. Skipping." return fi source_repo_url="${SOURCE_SSH_HOST}:${source_repo}" echo "=================================================" echo "Processing source: ${source_repo} / branch: ${branch_name}" repo_name="${branch_name}" echo "Target repository name: ${repo_name}" # Skip if the source branch does not exist or is empty branch_ref="$(git ls-remote --heads "${source_repo_url}" "${branch_name}")" || { echo "::warning::Failed to query branch '${branch_name}' from source. Skipping." return } if [[ -z "${branch_ref}" ]]; then echo "::warning::Branch '${branch_name}' does not exist or is empty on source. Skipping." return fi # Check if repository exists on Gitea repo_exists=false http_status=$(curl -s -o /dev/null -w "%{http_code}" -H "${AUTH_HEADER}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") if [ "${http_status}" == "404" ]; then echo "Repository 'center_dev/${repo_name}' does not exist. Creating it..." create_tmp="$(mktemp)" create_status=$(curl -s -o "${create_tmp}" -w "%{http_code}" -X POST -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"name\":\"${repo_name}\",\"private\":true,\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/orgs/${CENTER_ORG}/repos") if [[ "${create_status}" != "201" ]]; then echo "::error::Failed to create repository. HTTP ${create_status}" cat "${create_tmp}" rm -f "${create_tmp}" exit 1 fi echo "Repository created successfully." repo_exists=true rm -f "${create_tmp}" elif [ "${http_status}" != "200" ]; then echo "::error::Error checking repository. HTTP status: ${http_status}" exit 1 else echo "Repository 'center_dev/${repo_name}' already exists." repo_exists=true fi # Define remote URL's hostname, stripping protocol GITEA_HOSTNAME=$(echo "${BASE_GITEA_URL}" | sed -e 's~^https*://~~' -e 's~/$~~') GITEA_REMOTE="https://${BASE_GITEA_USER}:${BASE_GITEA_TOKEN}@${GITEA_HOSTNAME}/${CENTER_ORG}/${repo_name}.git" # Create a temporary directory for cloning CLONE_DIR=$(mktemp -d) echo "Working directory: ${CLONE_DIR}" if ${repo_exists}; then echo "Cloning existing target repository for update..." if ! git clone --mirror "${GITEA_REMOTE}" "${CLONE_DIR}"; then echo "::error::Failed to clone existing target repository ${GITEA_REMOTE}" rm -rf "${CLONE_DIR}" return fi cd "${CLONE_DIR}" git remote add source "${source_repo_url}" echo "Fetching latest branch '${branch_name}' from source..." if ! git fetch source "+refs/heads/${branch_name}:refs/heads/${branch_name}"; then echo "::error::Failed to fetch branch '${branch_name}' from source repo" cd .. rm -rf "${CLONE_DIR}" return fi else echo "Cloning source repository for first-time push..." if ! git clone --mirror "${source_repo_url}" "${CLONE_DIR}"; then echo "::error::Failed to clone source repository ${source_repo_url}" rm -rf "${CLONE_DIR}" return fi cd "${CLONE_DIR}" fi git remote set-url origin "${GITEA_REMOTE}" echo "Pushing '${branch_name}' to Gitea repository '${repo_name}'..." if ! git push --force origin "refs/heads/${branch_name}:refs/heads/main"; then echo "::error::Failed to push branch '${branch_name}' to target repository" cd .. rm -rf "${CLONE_DIR}" return fi # Cleanup cd .. rm -rf "${CLONE_DIR}" echo "Successfully mirrored ${branch_name} to center_dev/${repo_name}" set_default_branch_main "${repo_name}" echo "=================================================" echo "" } if [[ -n "${INPUT_BRANCHES}" ]]; then echo "Processing manually specified branches: ${INPUT_BRANCHES}" # Split comma-separated string into an array IFS=',' read -r -a branches_to_process <<< "${INPUT_BRANCHES}" for branch in "${branches_to_process[@]}"; do # Trim whitespace trimmed_branch=$(echo "$branch" | xargs) process_entry "${trimmed_branch}" done else echo "Processing all branches from branch_list file." while IFS= read -r branch_name || [[ -n "$branch_name" ]]; do process_entry "${branch_name}" done < branch_list fi