|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# This script expects the following environment variables: |
| 5 | +# REPO: owner/repo |
| 6 | +# HEAD_SHA: the commit SHA for which the producer workflows ran |
| 7 | +# TOKEN: a token with read access to Actions (GITHUB_TOKEN or a PAT) |
| 8 | +# OTHER_WORKFLOWS: comma-separated list of workflow filenames or workflow IDs |
| 9 | +# ARTIFACT_NAMES: comma-separated list of artifact names (in the same order as WORKFLOWS) |
| 10 | +# Optional: |
| 11 | +# RETRIES: number of polling attempts per workflow (default: 30) |
| 12 | +# SLEEP_SEC: seconds to wait between attempts (default: 10) |
| 13 | + |
| 14 | +IFS=',' read -r -a WORKFLOWS_ARR <<< "${OTHER_WORKFLOWS}" |
| 15 | +IFS=',' read -r -a ARTIFACTS_ARR <<< "${ARTIFACT_NAMES}" |
| 16 | +REPO="${REPO}" |
| 17 | +SHA="${HEAD_SHA}" |
| 18 | +TOKEN="${TOKEN}" |
| 19 | +API_BASE="https://api.github.com/repos/${REPO}" |
| 20 | +RETRIES=${RETRIES:-30} |
| 21 | +SLEEP_SEC=${SLEEP_SEC:-10} |
| 22 | + |
| 23 | +mkdir -p artifacts |
| 24 | + |
| 25 | +if [ ${#WORKFLOWS_ARR[@]} -ne ${#ARTIFACTS_ARR[@]} ]; then |
| 26 | + echo "ERROR: OTHER_WORKFLOWS and ARTIFACT_NAMES must have the same number of items" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +for idx in "${!WORKFLOWS_ARR[@]}"; do |
| 31 | + wf_raw="${WORKFLOWS_ARR[$idx]}" |
| 32 | + wf=$(echo "$wf_raw" | tr -d '[:space:]') |
| 33 | + art_raw="${ARTIFACTS_ARR[$idx]}" |
| 34 | + art=$(echo "$art_raw" | tr -d '[:space:]') |
| 35 | + |
| 36 | + echo "-- Waiting for workflow '$wf' to succeed for sha $SHA (artifact: $art)" |
| 37 | + attempt=0 |
| 38 | + |
| 39 | + while true; do |
| 40 | + attempt=$((attempt+1)) |
| 41 | + echo " attempt $attempt/$RETRIES" |
| 42 | + |
| 43 | + # 1) Liste Runs für das Workflow (workflow id/name/file) |
| 44 | + resp=$(curl -s -H "Authorization: Bearer ${TOKEN}" "${API_BASE}/actions/workflows/${wf}/runs?per_page=50") |
| 45 | + |
| 46 | + # 2) Finde Run mit matching head_sha |
| 47 | + run_id=$(echo "$resp" | jq -r --arg SHA "$SHA" '.workflow_runs[] | select(.head_sha==$SHA) | .id' | head -n1 || true) |
| 48 | + run_status=$(echo "$resp" | jq -r --arg SHA "$SHA" '.workflow_runs[] | select(.head_sha==$SHA) | .status' | head -n1 || true) |
| 49 | + run_conclusion=$(echo "$resp" | jq -r --arg SHA "$SHA" '.workflow_runs[] | select(.head_sha==$SHA) | .conclusion' | head -n1 || true) |
| 50 | + |
| 51 | + if [ -n "${run_id}" ] && [ "${run_id}" != "null" ]; then |
| 52 | + echo " Found run ${run_id} status=${run_status} conclusion=${run_conclusion}" |
| 53 | + |
| 54 | + if [ "${run_status}" = "completed" ]; then |
| 55 | + if [ "${run_conclusion}" = "success" ]; then |
| 56 | + echo " Run succeeded — listing artifacts" |
| 57 | + art_resp=$(curl -s -H "Authorization: Bearer ${TOKEN}" "${API_BASE}/actions/runs/${run_id}/artifacts") |
| 58 | + art_id=$(echo "$art_resp" | jq -r --arg NAME "$art" '.artifacts[] | select(.name==$NAME) | .id' | head -n1 || true) |
| 59 | + |
| 60 | + if [ -n "${art_id}" ] && [ "${art_id}" != "null" ]; then |
| 61 | + echo " Downloading artifact '$art' (id=${art_id})" |
| 62 | + out_zip="artifacts/${art}.zip" |
| 63 | + curl -L -s -H "Authorization: Bearer ${TOKEN}" "${API_BASE}/actions/artifacts/${art_id}/zip" -o "${out_zip}" |
| 64 | + echo " Unpacking to artifacts/${art}/" |
| 65 | + mkdir -p "artifacts/${art}" |
| 66 | + unzip -o "${out_zip}" -d "artifacts/${art}" >/dev/null |
| 67 | + rm -f "${out_zip}" |
| 68 | + echo " Artifact $art downloaded and unpacked" |
| 69 | + break |
| 70 | + else |
| 71 | + echo " ERROR: Artifact '$art' not found in run ${run_id}" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + else |
| 75 | + echo " ERROR: Run completed but conclusion='${run_conclusion}'" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + else |
| 79 | + echo " Run exists but not completed yet (status=${run_status}), will retry" |
| 80 | + fi |
| 81 | + else |
| 82 | + echo " No run found for workflow '${wf}' and sha ${SHA} yet" |
| 83 | + fi |
| 84 | + |
| 85 | + if [ ${attempt} -ge ${RETRIES} ]; then |
| 86 | + echo "ERROR: Timeout waiting for workflow '${wf}' to succeed for sha ${SHA}" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + |
| 90 | + sleep ${SLEEP_SEC} |
| 91 | + done |
| 92 | + |
| 93 | +done |
| 94 | + |
| 95 | +echo "All requested artifacts downloaded into ./artifacts/" |
| 96 | +exit 0 |
0 commit comments