From 73a4a286c2430227e645fdc4560af974ae6a2f21 Mon Sep 17 00:00:00 2001 From: Lectom C Han Date: Fri, 19 Dec 2025 14:10:00 +0900 Subject: [PATCH] =?UTF-8?q?pre-fetch=EB=A1=9C=20=EB=A8=BC=EC=A0=80=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EC=BB=A4=EB=B0=8B=20=ED=95=B4=EC=8B=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=A1=9D=ED=95=98=EA=B3=A0=20=EB=B0=B1=EC=97=85?= =?UTF-8?q?=EC=97=AC=EB=B6=80=20=ED=8C=90=EB=8B=A8=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?=EB=AA=A8=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/backup.yml | 471 ++++++++++++++++++++++++++++++++++++ .gitea/workflows/mirror.yml | 2 - README.md | 21 +- to-do.md | 4 + 4 files changed, 492 insertions(+), 6 deletions(-) create mode 100644 .gitea/workflows/backup.yml diff --git a/.gitea/workflows/backup.yml b/.gitea/workflows/backup.yml new file mode 100644 index 0000000..e71136d --- /dev/null +++ b/.gitea/workflows/backup.yml @@ -0,0 +1,471 @@ +name: Git Repository Backup (Pre-scan) + +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 17 * * *' # UTC 17:00 == KST 02:00 + +jobs: + backup: + 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.42.118 >> ~/.ssh/known_hosts + + - name: Backup Branches (pre-scan → decision → execution) + 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 + TARGET_SEED_DEPTH: ${{ vars.TARGET_SEED_DEPTH }} # Optional, fallback seed depth (default 50) + run: | + set -euo pipefail + + CENTER_ORG="center_dev" + AUTH_HEADER="Authorization: token ${BASE_GITEA_TOKEN}" + SOURCE_SSH_HOST="engdev@172.16.42.118" + 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 + TS_KST=$(TZ=Asia/Seoul date '+%Y%m%d_%H%M%S') + REPORT_DIR="${ROOT_DIR}/backup_reports" + mkdir -p "${REPORT_DIR}" + SOURCE_HEADS_FILE="${REPORT_DIR}/source_heads_${TS_KST}.tsv" + TARGET_HEADS_FILE="${REPORT_DIR}/target_heads_${TS_KST}.tsv" + DECISIONS_FILE="${REPORT_DIR}/decisions_${TS_KST}.tsv" + + echo -e "source_repo\tbranch\tcommit" > "${SOURCE_HEADS_FILE}" + echo -e "target_repo\tbranch\tcommit\texists" > "${TARGET_HEADS_FILE}" + echo -e "source_repo\tbranch\talias\tresolved_repo\tsource_commit\ttarget_commit\tdecision\tnote" > "${DECISIONS_FILE}" + + notify_status() { + local status="$1" repo="$2" branch="$3" mode="$4" start_epoch="$5" reason="${6:-}" details="${7:-}" + 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, 이유: ${reason}, 시각: ${ts})" + ;; + *) + text="센터Git ${repo} 상태: ${status} (${ts})" + ;; + esac + if [[ -n "${details}" ]]; then + text="${text} (heads: ${details})" + fi + 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 + } + + map_repo_name() { + local branch_name="$1" alias_name="$2" resolved + resolved="${branch_name}" + if [[ "${branch_name}" == "Develop_Net8" ]]; then + resolved="base" + elif [[ "${branch_name}" == "Develop_Net8_"* ]]; then + resolved="${branch_name#Develop_Net8_}" + elif [[ "${branch_name}" == "Develop_"* ]]; then + resolved="${branch_name#Develop_}" + elif [[ "${branch_name}" == "develop_"* ]]; then + resolved="${branch_name#develop_}" + fi + if [[ -n "${alias_name}" ]]; then + resolved="${alias_name}" + fi + echo "${resolved}" + } + + declare -A SOURCE_HEADS + declare -A ENTRY_SOURCE + declare -A ENTRY_BRANCH + declare -A ENTRY_ALIAS + declare -A ENTRY_REPO + declare -A SOURCE_REPOS + declare -a ENTRY_KEYS=() + + add_entry() { + local entry_raw="$1" entry alias_name source_repo branch_name repo_name idx + entry="${entry_raw%%#*}" + entry="$(echo "$entry" | xargs)" + [[ -z "${entry}" ]] && return + + 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 + + repo_name="$(map_repo_name "${branch_name}" "${alias_name}")" + idx="${#ENTRY_KEYS[@]}" + ENTRY_KEYS+=("${idx}") + ENTRY_SOURCE["${idx}"]="${source_repo}" + ENTRY_BRANCH["${idx}"]="${branch_name}" + ENTRY_ALIAS["${idx}"]="${alias_name}" + ENTRY_REPO["${idx}"]="${repo_name}" + SOURCE_REPOS["${source_repo}"]=1 + } + + if [[ -n "${INPUT_BRANCHES}" ]]; then + echo "Processing manually specified branches (pre-scan mode): ${INPUT_BRANCHES}" + IFS=',' read -r -a branches_to_process <<< "${INPUT_BRANCHES}" + for branch in "${branches_to_process[@]}"; do + add_entry "${branch}" + done + else + echo "Processing branch_list in pre-scan mode." + while IFS= read -r branch_line || [[ -n "${branch_line}" ]]; do + add_entry "${branch_line}" + done < branch_list + fi + + if (( ${#ENTRY_KEYS[@]} == 0 )); then + echo "::error::No valid branch entries to process." + exit 1 + fi + + echo "Step 1) 소스 브랜치 해시 스캔" + for source_repo in "${!SOURCE_REPOS[@]}"; do + source_repo_url="${SOURCE_SSH_HOST}:${source_repo}" + echo " - ${source_repo_url}" + if ! remote_output=$(git ls-remote --heads "${source_repo_url}"); then + echo "::warning::Failed to ls-remote ${source_repo_url}. Entries for this repo may fail." + continue + fi + while IFS=$'\t' read -r commit ref || [[ -n "${commit}" ]]; do + [[ -z "${commit}" || -z "${ref}" ]] && continue + branch="${ref#refs/heads/}" + SOURCE_HEADS["${source_repo}|${branch}"]="${commit}" + echo -e "${source_repo}\t${branch}\t${commit}" >> "${SOURCE_HEADS_FILE}" + done <<< "${remote_output}" + done + + echo "Step 2) 타겟 해시 스캔 및 실행 계획 수립" + GITEA_HOSTNAME=$(echo "${BASE_GITEA_URL}" | sed -e 's~^https*://~~' -e 's~/$~~') + + for idx in "${ENTRY_KEYS[@]}"; do + source_repo="${ENTRY_SOURCE[${idx}]}" + branch_name="${ENTRY_BRANCH[${idx}]}" + alias_name="${ENTRY_ALIAS[${idx}]}" + repo_name="${ENTRY_REPO[${idx}]}" + start_epoch=$(date +%s) + backup_mode="미정" + note="" + target_commit="" + heads_detail="src=unknown tgt=unknown" + + source_commit="${SOURCE_HEADS[${source_repo}|${branch_name}]:-}" + if [[ -z "${source_commit}" ]]; then + note="source branch 없음/조회 실패" + echo -e "${source_repo}\t${branch_name}\t${alias_name}\t${repo_name}\t\t\t건너뜀\t${note}" >> "${DECISIONS_FILE}" + heads_detail="src=none tgt=unknown" + notify_status "error" "${repo_name}" "${branch_name}" "사전 스캔 실패" "${start_epoch}" "${note}" "${heads_detail}" + echo "Skipping ${branch_name} (${repo_name}) - ${note}" + continue + fi + + GITEA_REMOTE="https://${BASE_GITEA_USER}:${BASE_GITEA_TOKEN}@${GITEA_HOSTNAME}/${CENTER_ORG}/${repo_name}.git" + 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}" == "200" ]]; then + repo_exists=true + elif [[ "${http_status}" == "404" ]]; then + repo_exists=false + else + note="repo 조회 실패 (HTTP ${http_status})" + echo "::warning::${note}" + echo -e "${source_repo}\t${branch_name}\t${alias_name}\t${repo_name}\t${source_commit}\t\t건너뜀\t${note}" >> "${DECISIONS_FILE}" + heads_detail="src=${source_commit} tgt=unknown" + notify_status "error" "${repo_name}" "${branch_name}" "사전 조회 실패" "${start_epoch}" "${note}" "${heads_detail}" + continue + fi + + target_commit="" + 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}') + fi + heads_detail="src=${source_commit} tgt=${target_commit:-none}" + exists_text=$(${repo_exists} && echo "true" || echo "false") + echo -e "${repo_name}\tmain\t${target_commit:-}\t${exists_text}" >> "${TARGET_HEADS_FILE}" + + decision="" + if ! ${repo_exists}; then + decision="신규 백업" + note="타겟 저장소 없음" + elif [[ -z "${target_commit}" ]]; then + decision="신규 백업" + note="타겟 main 없음" + elif [[ "${source_commit}" == "${target_commit}" ]]; then + decision="동일 커밋 - 건너뜀" + note="동일 커밋" + else + decision="증분 백업" + note="커밋 상이" + fi + + echo -e "${source_repo}\t${branch_name}\t${alias_name}\t${repo_name}\t${source_commit}\t${target_commit}\t${decision}\t${note}" >> "${DECISIONS_FILE}" + + if [[ "${decision}" == "동일 커밋 - 건너뜀" ]]; then + notify_status "skip" "${repo_name}" "${branch_name}" "${decision}" "${start_epoch}" "" "${heads_detail}" + echo "Pre-scan marked ${branch_name} (${repo_name}) as skip (same commit)." + continue + fi + + if [[ "${decision}" == "신규 백업" ]]; then + backup_mode="신규 백업" + echo "Repository 'center_dev/${repo_name}' does not exist or main is missing. Will create/push." + attempt=1 + create_status="" + if ! ${repo_exists}; then + while (( attempt<=max_api_retry )); do + create_status=$(curl -s -o /dev/null -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}" + notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "repo 생성 실패 (HTTP ${create_status})" "${heads_detail}" + continue + fi + echo "Repository created successfully." + repo_exists=true + just_created=true + fi + else + backup_mode="증분 백업" + just_created=false + fi + + notify_status "start" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "" "${heads_detail}" + + shallow_exclude_args=() + if ${repo_exists} && [[ -n "${target_commit:-}" ]]; then + shallow_exclude_args=(--shallow-exclude="${target_commit}") + fi + + 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_SSH_HOST}:${source_repo}" "${CLONE_DIR}"; then + echo "::error::Failed to clone source repository ${SOURCE_SSH_HOST}:${source_repo}" + notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source clone 실패" "${heads_detail}" + rm -rf "${CLONE_DIR}" + continue + 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 실패" "${heads_detail}" + cd "${ROOT_DIR}" + rm -rf "${CLONE_DIR}" + continue + fi + git remote add origin "${GITEA_REMOTE}" + git remote add source "${SOURCE_SSH_HOST}:${source_repo}" + fi + + echo "Fetching latest branch '${branch_name}' from source..." + FETCH_LOG="${CLONE_DIR}/fetch_shallow.log" + if ! git fetch --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 --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 --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 오류(폴백)" "${heads_detail}" + cd "${ROOT_DIR}" + rm -rf "${CLONE_DIR}" + continue + 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 오류" "${heads_detail}" + cd "${ROOT_DIR}" + rm -rf "${CLONE_DIR}" + continue + fi + fi + + if [[ "${SYNC_TAGS}" == "true" ]]; then + echo "Fetching tags from source..." + if ! git fetch --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 오류" "${heads_detail}" + cd "${ROOT_DIR}" + rm -rf "${CLONE_DIR}" + continue + 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)" "${heads_detail}" + cd "${ROOT_DIR}" + rm -rf "${CLONE_DIR}" + continue + 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 오류" "${heads_detail}" + fi + fi + + cd "${ROOT_DIR}" + rm -rf "${CLONE_DIR}" + + echo "Successfully mirrored ${branch_name} to center_dev/${repo_name}" + set_default_branch_main "${repo_name}" + final_heads_detail="src=${source_commit} tgt=${source_commit}" + notify_status "success" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "" "${final_heads_detail}" + echo "=================================================" + echo "" + done + + 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}" + echo "Source head 기록: ${SOURCE_HEADS_FILE}" + echo "Target head 기록: ${TARGET_HEADS_FILE}" + echo "판정 테이블: ${DECISIONS_FILE}" + 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 diff --git a/.gitea/workflows/mirror.yml b/.gitea/workflows/mirror.yml index 794ed40..ad9ff6d 100644 --- a/.gitea/workflows/mirror.yml +++ b/.gitea/workflows/mirror.yml @@ -7,8 +7,6 @@ on: 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: '7 16 * * *' # UTC 16:07 == KST 01:07 jobs: mirror: diff --git a/README.md b/README.md index 1a7efce..4937cdf 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,16 @@ # Git Repository Mirroring Workflow ## 목적 -- 외부 SSH Git 저장소(센터 공용 코드 관리용 깃)의 특정 브랜치를 Gitea `center_dev` 조직의 개별 저장소로 순차 복제합니다. -- 브랜치마다 대응 저장소명을 결정해 `main` 브랜치로 강제 푸시하며, 태그 동기화 옵션을 제공합니다. +- 외부 SSH Git 저장소(센터 공용 코드 관리용 깃)의 특정 브랜치를 Gitea `center_dev` 조직으로 순차 복제합니다. +- 브랜치→저장소 매핑 규칙을 적용해 `main` 브랜치로 강제 푸시하며, 태그 동기화 옵션을 제공합니다. +- `backup.yml`은 소스/타겟의 head 커밋을 먼저 스캔해 건너뜀/증분/신규 여부를 사전 결정하고, `mirror.yml`은 즉시 복제합니다. + +## 워크플로우 파일 +- `.gitea/workflows/backup.yml`: 프리스캔 기반 백업(사전 해시 기록 → 계획 결정 → 실행). 예약 실행: 매일 02:00 KST (`cron: 0 17 * * *`, UTC 기준) + `workflow_dispatch`. +- `.gitea/workflows/mirror.yml`: 기존 즉시 복제 워크플로우. 스케줄 없음, `workflow_dispatch`로만 수동 실행. ## 실행 환경 - Runner 라벨: `internal` (해당 라벨이 있는 셀프호스티드 러너에서만 실행) -- 예약 실행: 매일 02:07 KST (`cron: 7 17 * * *`, Gitea 스케줄 cron은 UTC 기준) - 수동 실행: `workflow_dispatch` 입력을 통해 선택 브랜치만 실행 가능 ## 시크릿/변수 @@ -34,7 +38,16 @@ - `branches`: 콤마로 구분한 `sourceRepo/branch` 또는 `sourceRepo/branch,alias` 목록 - 입력이 비어 있으면 `branch_list` 전체를 처리 -## 동작 개요 +## backup.yml (프리스캔 백업) 동작 개요 +1) 입력 또는 `branch_list`를 파싱해 유효 항목을 확정하고, 소스 저장소별로 `git ls-remote --heads`를 한 번씩 수행해 브랜치→커밋 해시를 수집합니다. +2) Gitea에 해당 저장소가 있는지 조회 후 `main` 해시를 확인합니다. +3) 사전 판정: 타겟이 없거나 `main`이 없으면 “신규 백업”, 해시가 같으면 “건너뜀”, 다르면 “증분 백업”으로 결정합니다. +4) 해시 스냅샷과 판정 테이블을 `backup_reports/` 폴더에 `source_heads_*.tsv`, `target_heads_*.tsv`, `decisions_*.tsv`로 기록합니다. +5) “건너뜀”은 바로 알림 후 종료, “신규/증분”만 fetch→push 실행합니다. +6) 실행 단계: 저장소 존재 확인/생성(`default_branch=main`) → shallow-exclude 기반 fetch(미지원 시 타겟 main 얕은 시드 후 일반 fetch) → `main`으로 강제 푸시 → 태그 동기화(옵션) → 기본 브랜치 `main` 패치 → 임시 폴더 정리. +7) 알림은 시작/성공/실패/건너뜀에 대해 KST 타임스탬프, 모드, 소요시간을 포함해 전송합니다. + +## mirror.yml (즉시 복제) 동작 개요 1) 브랜치 존재 여부 확인 (`git ls-remote`) 2) 타겟 저장소 존재 확인/생성 (`default_branch=main`) 3) 커밋 동일 시 skip (알림 전송) diff --git a/to-do.md b/to-do.md index 31fd3cb..fd55c3a 100644 --- a/to-do.md +++ b/to-do.md @@ -8,6 +8,9 @@ - [x] `mirror.yml`: 브랜치명이 `Develop_Net8`로 시작하지 않으면 브랜치명을 그대로 저장소 이름으로 사용하도록 로직 추가 - [x] `to-do.md`: 모든 작업 완료 후 KST 시간 기준으로 완료 시간 기록 - [x] 문서: AGENTS.md 생성 및 최신 요구사항(기존 저장소 업데이트, 비-Develop_Net8 브랜치 저장소명 규칙) 반영 +- [x] `backup.yml`: 프리스캔 기반 백업 워크플로우 신규 작성(소스/타겟 head 해시 기록 → 판정 테이블 생성 → skip/증분/신규 분기 실행), 알림에 heads 정보 포함 +- [x] `backup.yml`: 스케줄을 매일 02:00 KST(UTC 17:00)로 설정, `mirror.yml`은 수동 실행만 유지 +- [x] 문서: README에 backup.yml 흐름/스케줄과 보고서 경로를 추가하고 mirror.yml을 수동 전용으로 명시 --- @@ -31,3 +34,4 @@ 2025-12-17 09:38:49 KST 추가 업데이트: Gitea API 오류가 발생해도 전체 작업이 중단되지 않도록 exit 대신 return 처리하여 다음 브랜치로 계속 진행하도록 수정. 2025-12-17 09:50:00 KST 추가 업데이트: Gitea API(조회/생성/기본 브랜치 설정)에 최대 3회 재시도(5초 대기)를 추가해 일시 오류 발생 시에도 작업이 이어지도록 개선, README 반영. 2025-12-18 08:55:36 KST 추가 업데이트: 최근 크론 실행에서 푸시 성공 후 set -e가 미처리된 curl 실패에 반응해 Step이 실패한 사례 대응. Gitea API curl 호출이 실패해도 000 코드로 재시도하도록 가드했고, 전체 처리 후 TOTAL_ERROR>0이면 명시적으로 exit 1, 아니면 0으로 종료하도록 종료 코드를 고정해 불필요한 실패를 방지함. +2025-12-19 14:09:09 KST 추가 업데이트: backup.yml을 프리스캔 기반으로 신규 작성하여 소스/타겟 head 해시 스냅샷과 판정 테이블을 TSV로 기록하고 heads 정보를 포함한 알림을 전송하도록 구성. backup.yml 스케줄을 02:00 KST로 설정하고 mirror.yml은 수동 실행 전용으로 유지. README에 신규 워크플로우와 보고서 경로를 반영.