diff --git a/.gitea/workflows/cicd-start.yaml b/.gitea/workflows/cicd-start.yaml index 536b284..8f0495b 100644 --- a/.gitea/workflows/cicd-start.yaml +++ b/.gitea/workflows/cicd-start.yaml @@ -127,64 +127,51 @@ jobs: CANDIDATE_API_BASES+=("http://${GITEA_REGISTRY_IP}:3001/api/v1") fi + ensure_curl() { + if command -v curl >/dev/null 2>&1; then + return 0 + fi + + if command -v apt-get >/dev/null 2>&1; then + export DEBIAN_FRONTEND=noninteractive + apt-get update -qq + apt-get install -y -qq curl ca-certificates + fi + + if command -v curl >/dev/null 2>&1; then + return 0 + fi + + echo "❌ curl is required for dispatch and could not be installed" + return 1 + } + + ensure_curl + dispatched=false for API_BASE in "${CANDIDATE_API_BASES[@]}"; do DISPATCH_URL="${API_BASE}/repos/${REPO_OWNER}/${REPO_NAME}/actions/workflows/${TARGET_WORKFLOW}/dispatches" - HTTP_RESULT=$( - printf '%s\n' \ - 'import os' \ - 'import sys' \ - 'import urllib.error' \ - 'import urllib.request' \ - '' \ - 'url = os.environ["DISPATCH_URL"]' \ - 'token = os.environ["DISPATCH_TOKEN"]' \ - 'payload = os.environ["DISPATCH_PAYLOAD"]' \ - '' \ - 'request = urllib.request.Request(' \ - ' url,' \ - ' data=payload.encode("utf-8"),' \ - ' headers={' \ - ' "Authorization": "token " + token,' \ - ' "Content-Type": "application/json" ,' \ - ' "User-Agent": "plex-playlist-cicd-start" ,' \ - ' },' \ - ' method="POST",' \ - ')' \ - '' \ - 'try:' \ - ' with urllib.request.urlopen(request, timeout=20) as response:' \ - ' sys.stdout.write(f"{response.status}\\n")' \ - ' body = response.read().decode("utf-8", "replace")' \ - ' if body:' \ - ' sys.stdout.write(body)' \ - 'except urllib.error.HTTPError as error:' \ - ' sys.stdout.write(f"{error.code}\\n")' \ - ' body = error.read().decode("utf-8", "replace")' \ - ' if body:' \ - ' sys.stdout.write(body)' \ - 'except Exception as error:' \ - ' sys.stdout.write(f"000\\n{error}\\n")' \ - | DISPATCH_URL="${DISPATCH_URL}" \ - DISPATCH_TOKEN="${DISPATCH_TOKEN}" \ - DISPATCH_PAYLOAD="${payload}" \ - python3 - - ) + RESPONSE_FILE="/tmp/dispatch-response.txt" + : > "${RESPONSE_FILE}" - HTTP_CODE=$(printf '%s\n' "${HTTP_RESULT}" | sed -n '1p') - RESPONSE_BODY=$(printf '%s\n' "${HTTP_RESULT}" | sed '1d') + HTTP_CODE=$(curl -sS --connect-timeout 5 --max-time 20 -o "${RESPONSE_FILE}" -w "%{http_code}" -X POST \ + -H "Authorization: token ${DISPATCH_TOKEN}" \ + -H "Content-Type: application/json" \ + -H "User-Agent: plex-playlist-cicd-start" \ + "${DISPATCH_URL}" \ + -d "${payload}" || true) - if [ "${HTTP_CODE}" -ge 200 ] && [ "${HTTP_CODE}" -lt 300 ]; then + if [[ "${HTTP_CODE}" =~ ^[0-9]+$ ]] && [ "${HTTP_CODE}" -ge 200 ] && [ "${HTTP_CODE}" -lt 300 ]; then echo "✓ Dispatch succeeded via ${API_BASE}" dispatched=true break fi echo "Dispatch attempt failed via ${API_BASE} (HTTP ${HTTP_CODE})" - if [ -n "${RESPONSE_BODY}" ]; then + if [ -s "${RESPONSE_FILE}" ]; then echo "Response body:" - printf '%s\n' "${RESPONSE_BODY}" + cat "${RESPONSE_FILE}" fi done