#!/usr/bin/env bash # Git push(main) 후 Jenkins goldenChart-Desktop-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_DESKTOP_JENKINS_URL:-http://127.0.0.1:8090}}" JENKINS_JOB="${JENKINS_JOB:-${GC_DESKTOP_JENKINS_JOB:-goldenChart-Desktop-Pipeline}}" JENKINS_USER="${JENKINS_USER:-${GC_DESKTOP_JENKINS_USER:-}}" JENKINS_TOKEN="${JENKINS_TOKEN:-${GC_DESKTOP_JENKINS_TOKEN:-}}" JENKINS_TRIGGER_TOKEN="${JENKINS_TRIGGER_TOKEN:-${GC_DESKTOP_JENKINS_TRIGGER_TOKEN:-goldenchart-desktop-build}}" LOG_TAG="[trigger-desktop]" 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 desktop 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