From 97d8c7991f80653a1e9f795b80f6bfd923d36f4e Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 14 Jan 2025 09:59:23 +0100 Subject: [PATCH] WIP --- .gitlab-ci.yml | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index af485a86ef9..561d9eb86d2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -158,13 +158,15 @@ vaccine: needs: [create-multiarch-lib-injection-image] script: | GH_VACCINE_PAT=$(vault kv get -field=vaccine-token kv/k8s/gitlab-runner/dd-trace-rb/github-token) + REPO="TonyCTHsu/vaccine" + POLL_INTERVAL=30 # seconds - # Store the API response + # Trigger workflow HTTP_RESPONSE=$(curl -X POST \ -H "Accept: application/vnd.github.v3+json" \ -H "Authorization: token $GH_VACCINE_PAT" \ -w "\n%{http_code}" \ - https://api.github.com/repos/TonyCTHsu/vaccine/actions/workflows/vaccine.yml/dispatches \ + "https://api.github.com/repos/$REPO/actions/workflows/vaccine.yml/dispatches" \ -d '{"ref":"master", "inputs": {"commit_sha": "'$CI_COMMIT_SHA'"}}' 2>&1) # Get the HTTP status code @@ -176,36 +178,49 @@ vaccine: echo "Error: Authentication failed. Please check GitHub token." echo "Response: $RESPONSE_BODY" exit 1 - elif [ "$HTTP_STATUS" -ne 201 ] && [ "$HTTP_STATUS" -ne 204 ]; then + elif [ "$HTTP_STATUS" -ne 204 ]; then echo "Error: Unexpected status code $HTTP_STATUS" echo "Response: $RESPONSE_BODY" exit 1 fi - # Poll GitHub workflow status - WORKFLOW_ID=$(curl -s -H "Authorization: token $GH_VACCINE_PAT" \ - "https://api.github.com/repos/TonyCTHsu/vaccine/actions/runs?event=workflow_dispatch" \ - | jq -r '.workflow_runs[0].id') + echo "Successfully triggered workflow. Waiting for workflow to start..." + sleep 10 # Give GitHub a moment to create the workflow run - echo "Waiting for workflow completion..." + # Get the latest workflow run that matches our commit + WORKFLOW_ID=$(curl -s \ + -H "Authorization: token $GH_VACCINE_PAT" \ + "https://api.github.com/repos/$REPO/actions/runs?event=workflow_dispatch" \ + | jq -r --arg SHA "$CI_COMMIT_SHA" '.workflow_runs[] | select(.head_sha == $SHA) | .id' | head -n1) + + if [ -z "$WORKFLOW_ID" ]; then + echo "Error: Could not find workflow run for commit $CI_COMMIT_SHA" + exit 1 + fi + + echo "Found workflow run ID: $WORKFLOW_ID" + + # Poll workflow status while true; do - WORKFLOW_STATUS=$(curl -s -H "Authorization: token $GH_VACCINE_PAT" \ - "https://api.github.com/repos/TonyCTHsu/vaccine/actions/runs/$WORKFLOW_ID" \ - | jq -r '.status,.conclusion') + WORKFLOW_STATUS=$(curl -s \ + -H "Authorization: token $GH_VACCINE_PAT" \ + "https://api.github.com/repos/$REPO/actions/runs/$WORKFLOW_ID" \ + | jq -r '[.status, .conclusion] | @tsv') - STATUS=$(echo "$WORKFLOW_STATUS" | head -n1) - CONCLUSION=$(echo "$WORKFLOW_STATUS" | tail -n1) + STATUS=$(echo "$WORKFLOW_STATUS" | cut -f1) + CONCLUSION=$(echo "$WORKFLOW_STATUS" | cut -f2) if [ "$STATUS" = "completed" ]; then if [ "$CONCLUSION" = "success" ]; then - echo "Workflow completed successfully!" + echo "✅ Workflow completed successfully!" exit 0 else - echo "Workflow failed with conclusion: $CONCLUSION" + echo "❌ Workflow failed with conclusion: $CONCLUSION" + echo "See details: https://github.com/$REPO/actions/runs/$WORKFLOW_ID" exit 1 fi fi - echo "Workflow status: $STATUS" - sleep 30 + echo "Current status: $STATUS (Checking again in ${POLL_INTERVAL}s)" + sleep $POLL_INTERVAL done