매터모스트 메시지 알림 추가
This commit is contained in:
@@ -33,6 +33,7 @@ jobs:
|
||||
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
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
@@ -40,6 +41,42 @@ jobs:
|
||||
AUTH_HEADER="Authorization: token ${BASE_GITEA_TOKEN}"
|
||||
SOURCE_SSH_HOST="engdev@172.16.42.118"
|
||||
ROOT_DIR="$(pwd)"
|
||||
NOTIFY_WEBHOOK="${NOTIFY_WEBHOOK:-}"
|
||||
|
||||
notify_status() {
|
||||
local status="$1" repo="$2" branch="$3" mode="$4" start_epoch="$5" extra="${6:-}"
|
||||
local ts end_epoch duration text
|
||||
[[ -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})"
|
||||
;;
|
||||
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
|
||||
cat <<EOF | curl -sS -i -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @- \
|
||||
"${NOTIFY_WEBHOOK}" >/dev/null || echo "::warning::Notification failed for ${repo} (${status})"
|
||||
{
|
||||
"username": "Gitea",
|
||||
"icon_url": "https://gitea.hmac.kr/assets/img/logo.svg",
|
||||
"text": "${text}"
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
set_default_branch_main() {
|
||||
local repo_name="$1"
|
||||
@@ -59,8 +96,10 @@ jobs:
|
||||
}
|
||||
|
||||
process_entry() {
|
||||
local entry branch_name source_repo source_repo_url
|
||||
local entry branch_name source_repo source_repo_url start_epoch backup_mode
|
||||
entry="$(echo "$1" | xargs)" # trim whitespace
|
||||
start_epoch=$(date +%s)
|
||||
backup_mode="미확인"
|
||||
|
||||
if [[ -z "$entry" ]]; then
|
||||
return
|
||||
@@ -95,11 +134,13 @@ jobs:
|
||||
# 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
|
||||
|
||||
@@ -115,6 +156,7 @@ jobs:
|
||||
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})"
|
||||
exit 1
|
||||
fi
|
||||
echo "Repository created successfully."
|
||||
@@ -122,11 +164,18 @@ jobs:
|
||||
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})"
|
||||
exit 1
|
||||
else
|
||||
echo "Repository 'center_dev/${repo_name}' already exists."
|
||||
repo_exists=true
|
||||
fi
|
||||
if ${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~/$~~')
|
||||
@@ -140,6 +189,7 @@ jobs:
|
||||
echo "Cloning existing target repository for update (bare)..."
|
||||
if ! git clone --bare "${GITEA_REMOTE}" "${CLONE_DIR}"; then
|
||||
echo "::error::Failed to clone existing target repository ${GITEA_REMOTE}"
|
||||
notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "target clone 실패"
|
||||
rm -rf "${CLONE_DIR}"
|
||||
return
|
||||
fi
|
||||
@@ -149,6 +199,7 @@ jobs:
|
||||
echo "Cloning source repository for first-time push (bare)..."
|
||||
if ! git clone --bare "${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
|
||||
@@ -160,14 +211,16 @@ jobs:
|
||||
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"
|
||||
notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "source fetch 오류"
|
||||
cd "${ROOT_DIR}"
|
||||
rm -rf "${CLONE_DIR}"
|
||||
return
|
||||
fi
|
||||
|
||||
echo "Pushing '${branch_name}' to Gitea repository '${repo_name}'..."
|
||||
if ! git push --force origin "refs/heads/${branch_name}:refs/heads/main"; then
|
||||
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
|
||||
@@ -179,6 +232,7 @@ jobs:
|
||||
|
||||
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 ""
|
||||
}
|
||||
|
||||
2
to-do.md
2
to-do.md
@@ -11,4 +11,4 @@
|
||||
|
||||
---
|
||||
|
||||
* 모든 작업 완료 (2025-12-15 18:01:51 KST, KST 기준)
|
||||
2025-12-16 09:04:36 KST 업데이트: mirror.yml에서 대상 저장소를 `git clone --mirror` 대신 `--bare`로 받도록 변경하여 `fatal: --mirror can't be combined with refspecs` 오류를 제거. 기존 저장소가 있을 때는 Gitea 원격을 bare 클론 후 `source` 리모트를 원본 SSH에 추가하고 브랜치만 fetch → `origin`의 `main`으로 강제 푸시하도록 정리. 새 저장소일 때는 원본을 bare 클론해 `origin`을 `source`로 rename 후 Gitea를 `origin`으로 추가, 같은 fetch→push 동작 수행. 작업 루트 경로를 `ROOT_DIR`에 저장해 fetch/push 실패 시에도 항상 원위치로 돌아가며 임시 디렉터리를 정리하도록 수정.
|
||||
|
||||
Reference in New Issue
Block a user