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: '' jobs: mirror: runs-on: [internal] timeout-minutes: 500 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.10.191 >> ~/.ssh/known_hosts - name: Refresh source mirror repositories (git fetch --mirror) run: | set -euo pipefail echo "Refreshing mirror repos on 172.16.10.191 ..." ssh engdev@172.16.10.191 'set -euo pipefail; export GIT_PROGRESS_DELAY=0 GIT_FLUSH=1; shopt -s nullglob; for repo in *.git; do [ -d "${repo}" ] || continue; echo "Updating ${repo}"; (cd "${repo}" && git fetch --progress --mirror --prune); done' - 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 }} NOTIFY_WEBHOOK: ${{ vars.NOTIFY_WEBHOOK }} # Optional chat webhook SYNC_TAGS: ${{ vars.SYNC_TAGS }} # Optional, "false" to skip tag sync run: | set -euo pipefail export GIT_PROGRESS_DELAY=0 export GIT_FLUSH=1 CENTER_ORG="center_dev" AUTH_HEADER="Authorization: token ${BASE_GITEA_TOKEN}" SOURCE_SSH_HOST="engdev@172.16.10.191" ROOT_DIR="$(pwd)" NOTIFY_WEBHOOK="${NOTIFY_WEBHOOK:-}" SYNC_TAGS="${SYNC_TAGS:-true}" TARGET_SEED_DEPTH="${TARGET_SEED_DEPTH:-50}" TOTAL_SUCCESS=0 TOTAL_SKIP=0 TOTAL_ERROR=0 notify_status() { local status="$1" repo="$2" branch="$3" mode="$4" start_epoch="$5" extra="${6:-}" local ts end_epoch duration text payload case "${status}" in success) ((++TOTAL_SUCCESS)) ;; skip) ((++TOTAL_SKIP)) ;; error) ((++TOTAL_ERROR)) ;; esac [[ -z "${NOTIFY_WEBHOOK}" ]] && return ts=$(TZ=Asia/Seoul date '+%Y-%m-%d %H:%M:%S %Z') case "${status}" in start) text="센터Git ${repo} 백업을 ${ts}에 시작합니다. (branch: ${branch}, mode: ${mode})" ;; skip) end_epoch=$(date +%s) duration=$((end_epoch - start_epoch)) text="센터Git ${repo} 백업을 건너뜁니다. (branch: ${branch} -> main, mode: ${mode}, duration: ${duration}s, 시각: ${ts})" ;; success) end_epoch=$(date +%s) duration=$((end_epoch - start_epoch)) text="센터Git ${repo} 백업을 완료했습니다. (branch: ${branch} -> main, mode: ${mode}, duration: ${duration}s, 완료시각: ${ts})" ;; error) end_epoch=$(date +%s) duration=$((end_epoch - start_epoch)) text="센터Git ${repo} 백업이 실패했습니다. (branch: ${branch}, mode: ${mode}, duration: ${duration}s, 이유: ${extra}, 시각: ${ts})" ;; *) text="센터Git ${repo} 상태: ${status} (${ts})" ;; esac payload=${text//\"/\\\"} curl -sS -i -X POST \ -H "Content-Type: application/json" \ -d "{\"username\":\"Gitea\",\"icon_url\":\"https://gitea.hmac.kr/assets/img/logo.svg\",\"text\":\"${payload}\"}" \ "${NOTIFY_WEBHOOK}" >/dev/null || echo "::warning::Notification failed for ${repo} (${status})" } set_default_branch_main() { local repo_name="$1" local response http_status body attempt max_api_retry max_api_retry=3 attempt=1 while (( attempt<=max_api_retry )); do 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}") || response=$'\n000' http_status=$(echo "${response}" | tail -n1) body=$(echo "${response}" | sed '$d') if [[ "${http_status}" == "000" || "${http_status}" =~ ^5 || "${http_status}" == "429" ]]; then echo "::warning::Retrying default branch set (HTTP ${http_status}) for ${repo_name} (${attempt}/3)..." sleep 5 ((attempt++)) continue fi break done 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_raw entry alias_name branch_name source_repo source_repo_url start_epoch backup_mode attempt max_api_retry entry_raw="$1" # Remove inline comments and trim entry="${entry_raw%%#*}" entry="$(echo "$entry" | xargs)" # trim whitespace start_epoch=$(date +%s) backup_mode="미확인" if [[ -z "$entry" ]]; then return fi # Support alias via comma: sourceRepo/branch,aliasRepo alias_name="" if [[ "$entry" == *","* ]]; then IFS=',' read -r entry alias_name <<< "$entry" entry="$(echo "$entry" | xargs)" alias_name="$(echo "$alias_name" | xargs)" 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}" if [[ "${branch_name}" == "Develop_Net8" ]]; then repo_name="base" elif [[ "${branch_name}" == "Develop_Net8_"* ]]; then repo_name="${branch_name#Develop_Net8_}" elif [[ "${branch_name}" == "Develop_"* ]]; then repo_name="${branch_name#Develop_}" elif [[ "${branch_name}" == "develop_"* ]]; then repo_name="${branch_name#develop_}" fi if [[ -n "${alias_name}" ]]; then repo_name="${alias_name}" fi 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." notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source branch 조회 실패" return } if [[ -z "${branch_ref}" ]]; then echo "::warning::Branch '${branch_name}' does not exist or is empty on source. Skipping." notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source branch 없음/비어 있음" return fi branch_commit=$(echo "${branch_ref}" | awk '{print $1}') # Check if repository exists on Gitea repo_exists=false just_created=false max_api_retry=3 attempt=1 while (( attempt<=max_api_retry )); do http_status=$(curl -s -o /dev/null -w "%{http_code}" -H "${AUTH_HEADER}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") || http_status="000" if [[ "${http_status}" == "000" || "${http_status}" =~ ^5 || "${http_status}" == "429" ]]; then echo "::warning::Repo check HTTP ${http_status} for ${repo_name} (${attempt}/3); retrying in 5s..." sleep 5 ((attempt++)) continue fi break done if [ "${http_status}" == "404" ]; then echo "Repository 'center_dev/${repo_name}' does not exist. Creating it..." create_tmp="$(mktemp)" attempt=1 while (( attempt<=max_api_retry )); do 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") || create_status="000" if [[ "${create_status}" == "000" || "${create_status}" =~ ^5 || "${create_status}" == "429" ]]; then echo "::warning::Repo create HTTP ${create_status} for ${repo_name} (${attempt}/3); retrying in 5s..." sleep 5 ((attempt++)) continue fi break done if [[ "${create_status}" != "201" ]]; then echo "::error::Failed to create repository. HTTP ${create_status}" cat "${create_tmp}" rm -f "${create_tmp}" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "repo 생성 실패 (HTTP ${create_status})" return fi echo "Repository created successfully." repo_exists=true just_created=true rm -f "${create_tmp}" elif [ "${http_status}" != "200" ]; then echo "::error::Error checking repository. HTTP status: ${http_status}" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "repo 조회 실패 (HTTP ${http_status})" return else echo "Repository 'center_dev/${repo_name}' already exists." repo_exists=true fi if ${just_created}; then backup_mode="신규 전체 백업" elif ${repo_exists}; then backup_mode="증분 업데이트 (pull/fetch)" else backup_mode="신규 전체 백업" fi notify_status "start" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" # 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" # If target repo exists and refs match, skip heavy operations if ${repo_exists}; then target_main_ref=$(git ls-remote "${GITEA_REMOTE}" "refs/heads/main" || true) target_commit=$(echo "${target_main_ref}" | awk '{print $1}') if [[ -n "${branch_commit:-}" && -n "${target_commit}" && "${branch_commit}" == "${target_commit}" ]]; then echo "Target main already at source commit (${branch_commit}). Skipping clone/push." notify_status "skip" "${repo_name}" "${branch_name}" "변경 없음 (동일 커밋)" "${start_epoch}" return fi fi shallow_exclude_args=() if ${repo_exists} && [[ -n "${target_commit:-}" ]]; then shallow_exclude_args=(--shallow-exclude="${target_commit}") fi # Create a temporary directory for cloning CLONE_DIR=$(mktemp -d) echo "Working directory: ${CLONE_DIR}" if ${just_created}; then echo "Target repo newly created; cloning source branch for initial push..." if ! git clone --bare --no-tags --single-branch --branch "${branch_name}" "${source_repo_url}" "${CLONE_DIR}"; then echo "::error::Failed to clone source repository ${source_repo_url}" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source clone 실패" rm -rf "${CLONE_DIR}" return fi cd "${CLONE_DIR}" git remote rename origin source git remote add origin "${GITEA_REMOTE}" else cd "${CLONE_DIR}" if ! git init --bare; then echo "::error::Failed to init bare repository" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "bare init 실패" cd "${ROOT_DIR}" rm -rf "${CLONE_DIR}" return fi git remote add origin "${GITEA_REMOTE}" git remote add source "${source_repo_url}" fi FETCH_REMOTE="${SOURCE_FETCH_REMOTE:-${SOURCE_SSH_HOST}:${source_repo}}" prep_elapsed=$(( $(date +%s) - start_epoch )) echo "Prep completed in ${prep_elapsed}s. Fetching latest branch '${branch_name}' from source ${FETCH_REMOTE} ..." FETCH_LOG="${CLONE_DIR}/fetch_shallow.log" if ! git fetch --progress --no-tags "${shallow_exclude_args[@]}" source "+refs/heads/${branch_name}:refs/heads/${branch_name}" 2> >(tee "${FETCH_LOG}" >&2); then if [[ "${#shallow_exclude_args[@]}" -gt 0 ]]; then echo "::warning::shallow-exclude fetch failed (likely unsupported). Log: ${FETCH_LOG}" if [[ -s "${FETCH_LOG}" ]]; then echo "[shallow-exclude stderr tail]" tail -n 40 "${FETCH_LOG}" fi echo "[fallback] Seeding target main depth=${TARGET_SEED_DEPTH} then retrying full fetch without shallow-exclude" SEED_LOG="${CLONE_DIR}/fetch_seed.log" git fetch --progress --no-tags --depth="${TARGET_SEED_DEPTH}" origin "refs/heads/main:refs/heads/main" 2> >(tee "${SEED_LOG}" >&2) || echo "::warning::Seeding from target main skipped (fetch failed or branch missing)" backup_mode="증분 업데이트 (폴백: shallow-exclude 미지원)" FULL_FETCH_LOG="${CLONE_DIR}/fetch_full.log" if ! git fetch --progress --no-tags source "+refs/heads/${branch_name}:refs/heads/${branch_name}" 2> >(tee "${FULL_FETCH_LOG}" >&2); then if [[ -s "${SEED_LOG:-}" ]]; then echo "[seed stderr tail]" tail -n 40 "${SEED_LOG}" fi if [[ -s "${FULL_FETCH_LOG}" ]]; then echo "[fallback fetch stderr tail]" tail -n 40 "${FULL_FETCH_LOG}" fi echo "::error::Failed to fetch branch '${branch_name}' from source repo (fallback without shallow-exclude)" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source fetch 오류(폴백)" cd "${ROOT_DIR}" rm -rf "${CLONE_DIR}" return fi else echo "::error::Failed to fetch branch '${branch_name}' from source repo" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source fetch 오류" cd "${ROOT_DIR}" rm -rf "${CLONE_DIR}" return fi fi if [[ "${SYNC_TAGS}" == "true" ]]; then echo "Fetching tags from source..." if ! git fetch --progress --prune --prune-tags --no-tags source "refs/tags/*:refs/tags/*"; then echo "::error::Failed to fetch tags from source repo" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "tag fetch 오류" cd "${ROOT_DIR}" rm -rf "${CLONE_DIR}" return fi fi echo "Pushing '${branch_name}' to Gitea repository '${repo_name}'..." if ! git push --progress --force origin "refs/heads/${branch_name}:refs/heads/main"; then echo "::error::Failed to push branch '${branch_name}' to target repository" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "push 오류 (-> main)" cd "${ROOT_DIR}" rm -rf "${CLONE_DIR}" return fi if [[ "${SYNC_TAGS}" == "true" ]]; then echo "Pushing tags to Gitea repository '${repo_name}'..." if ! git push --force --prune origin "refs/tags/*:refs/tags/*"; then echo "::warning::Failed to push tags to target repository" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "tag push 오류" fi fi # Cleanup cd "${ROOT_DIR}" rm -rf "${CLONE_DIR}" echo "Successfully mirrored ${branch_name} to center_dev/${repo_name}" set_default_branch_main "${repo_name}" notify_status "success" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" 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 TOTAL_PROCESSED=$((TOTAL_SUCCESS + TOTAL_SKIP + TOTAL_ERROR)) SUMMARY_TS=$(TZ=Asia/Seoul date '+%Y-%m-%d %H:%M:%S %Z') SUMMARY_TEXT="브랜치 동기화 완료: 총 ${TOTAL_PROCESSED}개 (성공 ${TOTAL_SUCCESS}, 동일로 건너뜀 ${TOTAL_SKIP}, 오류 ${TOTAL_ERROR}) - ${SUMMARY_TS}" echo "${SUMMARY_TEXT}" if [[ -n "${NOTIFY_WEBHOOK}" ]]; then SUMMARY_PAYLOAD=${SUMMARY_TEXT//\"/\\\"} curl -sS -i -X POST \ -H "Content-Type: application/json" \ -d "{\"username\":\"Gitea\",\"icon_url\":\"https://gitea.hmac.kr/assets/img/logo.svg\",\"text\":\"${SUMMARY_PAYLOAD}\"}" \ "${NOTIFY_WEBHOOK}" >/dev/null || echo "::warning::Summary notification failed" fi if (( TOTAL_ERROR > 0 )); then echo "::warning::One or more branches failed (${TOTAL_ERROR})." exit 1 fi