#!/usr/bin/env bash # Git push(main) 후 Jenkins goldenChart-Desktop-Pipeline 트리거 set -euo pipefail 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}" curl_get() { if [[ -n "$JENKINS_USER" && -n "$JENKINS_TOKEN" ]]; then curl -sf -u "${JENKINS_USER}:${JENKINS_TOKEN}" "$@" else curl -sf "$@" fi } curl_post() { if [[ -n "$JENKINS_USER" && -n "$JENKINS_TOKEN" ]]; then curl -sf -X POST -u "${JENKINS_USER}:${JENKINS_TOKEN}" "$@" else curl -sf -X POST "$@" fi } 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 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 log "[ERROR] Jenkins build trigger failed" exit 1