diff --git a/create-domain/action.yml b/create-domain/action.yml index eebb85b..e669bdb 100644 --- a/create-domain/action.yml +++ b/create-domain/action.yml @@ -82,25 +82,45 @@ runs: FORWARD_SCHEME="http" # 대상 프로토콜 http 고정 EMAIL="$NPM_USER" # Let's Encrypt 이메일 = NPM 계정 + # NPM API 호출 헬퍼: 응답 본문과 상태코드를 분리해, 2xx가 아니면 + # NPM이 보낸 에러 본문을 그대로 출력하고 실패시킵니다. + # 사용법: api [JSON_DATA] (성공 시 응답 본문을 stdout으로) + api() { + local method="$1" path="$2" data="${3:-}" resp code body + if [ -n "$data" ]; then + resp=$(curl -sS -w $'\n%{http_code}' -X "$method" "${BASE}${path}" \ + -H "$AUTH" -H "Content-Type: application/json" -d "$data") + else + resp=$(curl -sS -w $'\n%{http_code}' -X "$method" "${BASE}${path}" -H "$AUTH") + fi + code="${resp##*$'\n'}" + body="${resp%$'\n'*}" + if [ "$code" -lt 200 ] || [ "$code" -ge 300 ]; then + echo "API 오류 [$method $path] HTTP $code: $body" >&2 + return 1 + fi + printf '%s' "$body" + } + # --------------------------------------------------------------- # 1. 토큰 발급 (로그인) # --------------------------------------------------------------- - TOKEN=$(curl -fsS -X POST "${BASE}/tokens" \ + TOKEN_RESP=$(curl -sS -w $'\n%{http_code}' -X POST "${BASE}/tokens" \ -H "Content-Type: application/json" \ - -d "$(jq -nc --arg i "$NPM_USER" --arg s "$NPM_PASS" \ - '{identity:$i, secret:$s}')" \ - | jq -r '.token') - - if [ -z "$TOKEN" ] || [ "$TOKEN" = "null" ]; then - echo "NPM 토큰 발급 실패: 자격증명을 확인하세요" + -d "$(jq -nc --arg i "$NPM_USER" --arg s "$NPM_PASS" '{identity:$i, secret:$s}')") + TOKEN_CODE="${TOKEN_RESP##*$'\n'}" + TOKEN_BODY="${TOKEN_RESP%$'\n'*}" + if [ "$TOKEN_CODE" != "200" ]; then + echo "NPM 토큰 발급 실패 (HTTP $TOKEN_CODE): $TOKEN_BODY" exit 1 fi + TOKEN=$(printf '%s' "$TOKEN_BODY" | jq -r '.token') AUTH="Authorization: Bearer ${TOKEN}" # --------------------------------------------------------------- # 2. 도메인 중복 체크 (2번째 실행 대응) # --------------------------------------------------------------- - HOST_ID=$(curl -fsS "${BASE}/nginx/proxy-hosts" -H "$AUTH" \ + HOST_ID=$(api GET "/nginx/proxy-hosts" \ | jq -r --arg d "$DOMAIN" \ 'map(select(.domain_names | index($d))) | .[0].id // empty') @@ -115,15 +135,14 @@ runs: # 3. Let's Encrypt 인증서 발급 — 인증서도 멱등 처리 # --------------------------------------------------------------- # 같은 도메인의 기존 인증서가 있으면 재사용합니다. - CERT_ID=$(curl -fsS "${BASE}/nginx/certificates" -H "$AUTH" \ + CERT_ID=$(api GET "/nginx/certificates" \ | jq -r --arg d "$DOMAIN" \ 'map(select(.provider=="letsencrypt" and (.domain_names | index($d)))) | .[0].id // empty') if [ -z "$CERT_ID" ]; then echo "Let's Encrypt 인증서 발급 중: ${DOMAIN}" - CERT_ID=$(curl -fsS -X POST "${BASE}/nginx/certificates" \ - -H "$AUTH" -H "Content-Type: application/json" \ - -d "$(jq -nc --arg d "$DOMAIN" --arg e "$EMAIL" \ + CERT_ID=$(api POST "/nginx/certificates" \ + "$(jq -nc --arg d "$DOMAIN" --arg e "$EMAIL" \ '{provider:"letsencrypt", domain_names:[$d], meta:{letsencrypt_email:$e, letsencrypt_agree:true, dns_challenge:false}}')" \ @@ -168,9 +187,7 @@ runs: locations:[], meta:{}}') - HOST_ID=$(curl -fsS -X POST "${BASE}/nginx/proxy-hosts" \ - -H "$AUTH" -H "Content-Type: application/json" \ - -d "$PAYLOAD" | jq -r '.id') + HOST_ID=$(api POST "/nginx/proxy-hosts" "$PAYLOAD" | jq -r '.id') if [ -z "$HOST_ID" ] || [ "$HOST_ID" = "null" ]; then echo "프록시 호스트 생성 실패: ${DOMAIN}"