Files
goldenChart/scripts/trigger-mobile-jenkins-build.sh
T
Macbook a76cc2b1e8 fix: Jenkins 트리거 URL — host에서 localhost로 치환
.env의 host.docker.internal은 Docker용이므로 post-receive hook 실행 시
127.0.0.1로 바꿔 Jenkins 빌드를 큐에 등록한다.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 16:47:34 +09:00

95 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Git push(main) 후 Jenkins goldenChart-Mobile-Pipeline 트리거
set -euo pipefail
WORK_TREE="${WORK_TREE:-/Users/aidev/apps/goldenChart}"
if [[ -f "$WORK_TREE/.env" ]]; then
set -a
# shellcheck disable=SC1091
source "$WORK_TREE/.env"
set +a
fi
JENKINS_URL="${JENKINS_URL:-${GC_MOBILE_JENKINS_URL:-http://127.0.0.1:8090}}"
# Docker backend용 host.docker.internal → hook/호스트에서는 localhost
if [[ "$JENKINS_URL" == *host.docker.internal* ]]; then
JENKINS_URL="${JENKINS_URL//host.docker.internal/127.0.0.1}"
fi
JENKINS_JOB="${JENKINS_JOB:-${GC_MOBILE_JENKINS_JOB:-goldenChart-Mobile-Pipeline}}"
JENKINS_USER="${JENKINS_USER:-${GC_MOBILE_JENKINS_USER:-}}"
JENKINS_TOKEN="${JENKINS_TOKEN:-${GC_MOBILE_JENKINS_TOKEN:-}}"
JENKINS_TRIGGER_TOKEN="${JENKINS_TRIGGER_TOKEN:-${GC_MOBILE_JENKINS_TRIGGER_TOKEN:-goldenchart-mobile-build}}"
LOG_TAG="[trigger-mobile]"
log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $LOG_TAG $*"; }
JENKINS_URL="${JENKINS_URL%/}"
JOB_API="${JENKINS_URL}/job/${JENKINS_JOB// /%20}"
COOKIE_JAR="$(mktemp)"
trap 'rm -f "$COOKIE_JAR"' EXIT
curl_get() {
local auth=()
if [[ -n "$JENKINS_USER" && -n "$JENKINS_TOKEN" ]]; then
auth=(-u "${JENKINS_USER}:${JENKINS_TOKEN}")
fi
curl -sf "${auth[@]}" -c "$COOKIE_JAR" -b "$COOKIE_JAR" "$@"
}
curl_post() {
local auth=()
if [[ -n "$JENKINS_USER" && -n "$JENKINS_TOKEN" ]]; then
auth=(-u "${JENKINS_USER}:${JENKINS_TOKEN}")
fi
curl -sf "${auth[@]}" -c "$COOKIE_JAR" -b "$COOKIE_JAR" -X POST "$@"
}
log "Jenkins mobile build — $JENKINS_JOB @ $JENKINS_URL"
if ! curl -sf -o /dev/null "${JENKINS_URL}/login" 2>/dev/null; then
log "[ERROR] Jenkins unreachable: $JENKINS_URL"
exit 1
fi
if curl_get "${JOB_API}/lastBuild/api/json?tree=building" 2>/dev/null | grep -q '"building":true'; then
log "[SKIP] 이미 빌드 진행 중"
exit 0
fi
CRUMB_HDR=()
CRUMB_JSON="$(curl_get "${JENKINS_URL}/crumbIssuer/api/json" 2>/dev/null || true)"
if [[ -n "$CRUMB_JSON" ]]; then
FIELD="$(node -e "try{console.log(JSON.parse(process.argv[1]).crumbRequestField||'')}catch{}" "$CRUMB_JSON" 2>/dev/null || true)"
VAL="$(node -e "try{console.log(JSON.parse(process.argv[1]).crumb||'')}catch{}" "$CRUMB_JSON" 2>/dev/null || true)"
if [[ -n "$FIELD" && -n "$VAL" ]]; then
CRUMB_HDR=(-H "${FIELD}: ${VAL}")
fi
fi
if ((${#CRUMB_HDR[@]})); then
if curl_post "${CRUMB_HDR[@]}" -H "Content-Type: application/x-www-form-urlencoded" \
"${JOB_API}/build?token=${JENKINS_TRIGGER_TOKEN}"; then
log "빌드 큐 등록 완료 (token)"
exit 0
fi
if curl_post "${CRUMB_HDR[@]}" -H "Content-Type: application/x-www-form-urlencoded" "${JOB_API}/build"; then
log "빌드 큐 등록 완료"
exit 0
fi
else
if curl_post -H "Content-Type: application/x-www-form-urlencoded" \
"${JOB_API}/build?token=${JENKINS_TRIGGER_TOKEN}"; then
log "빌드 큐 등록 완료 (token)"
exit 0
fi
if curl_post -H "Content-Type: application/x-www-form-urlencoded" "${JOB_API}/build"; then
log "빌드 큐 등록 완료"
exit 0
fi
fi
log "[ERROR] Jenkins build trigger failed"
exit 1