|
| 1 | +name: 'Get Job Data from GitHub Actions' |
| 2 | +description: 'Fetches the GitHub Actions job data from the GitHub API' |
| 3 | +inputs: |
| 4 | + job-name: |
| 5 | + description: 'The name of the job to find' |
| 6 | + required: true |
| 7 | + github-token: |
| 8 | + description: 'GitHub token for API authentication' |
| 9 | + required: true |
| 10 | + repository: |
| 11 | + description: 'Repository in owner/repo format' |
| 12 | + required: true |
| 13 | + run-id: |
| 14 | + description: 'GitHub Actions run ID' |
| 15 | + required: true |
| 16 | +outputs: |
| 17 | + job_html_url: |
| 18 | + description: 'The HTML URL of the job' |
| 19 | + value: ${{ steps.get_url.outputs.job_html_url }} |
| 20 | +runs: |
| 21 | + using: 'composite' |
| 22 | + steps: |
| 23 | + - name: Fetch job URL from GitHub API |
| 24 | + id: get_url |
| 25 | + shell: bash |
| 26 | + run: | |
| 27 | + # Fetch the numeric job ID from GitHub API |
| 28 | + CURL_ERROR_FILE=$(mktemp) |
| 29 | + NETRC_FILE=$(mktemp) |
| 30 | +
|
| 31 | + # Write GitHub API credentials to a temporary netrc file to avoid |
| 32 | + # passing the token directly on the curl command line. |
| 33 | + printf '%s\n' \ |
| 34 | + 'machine api.github.com' \ |
| 35 | + ' login x-access-token' \ |
| 36 | + " password ${{ inputs.github-token }}" \ |
| 37 | + > "$NETRC_FILE" |
| 38 | + chmod 600 "$NETRC_FILE" |
| 39 | +
|
| 40 | + # Ensure temporary file cleanup on exit |
| 41 | + cleanup() { |
| 42 | + if [ -n "$CURL_ERROR_FILE" ] && [ -f "$CURL_ERROR_FILE" ]; then |
| 43 | + rm -f "$CURL_ERROR_FILE" |
| 44 | + fi |
| 45 | + if [ -n "$NETRC_FILE" ] && [ -f "$NETRC_FILE" ]; then |
| 46 | + rm -f "$NETRC_FILE" |
| 47 | + fi |
| 48 | + } |
| 49 | + trap cleanup EXIT |
| 50 | +
|
| 51 | + API_RESPONSE=$(curl -sS -w "\n%{http_code}" \ |
| 52 | + --netrc-file "$NETRC_FILE" \ |
| 53 | + -H "Accept: application/vnd.github+json" \ |
| 54 | + "https://api.github.com/repos/${{ inputs.repository }}/actions/runs/${{ inputs.run-id }}/jobs" 2>"$CURL_ERROR_FILE") |
| 55 | +
|
| 56 | + CURL_EXIT_CODE=$? |
| 57 | + if [ $CURL_EXIT_CODE -ne 0 ]; then |
| 58 | + echo "❌ ERROR: curl request to GitHub API failed with exit code $CURL_EXIT_CODE" |
| 59 | + if [ -s "$CURL_ERROR_FILE" ]; then |
| 60 | + echo "curl error output:" |
| 61 | + cat "$CURL_ERROR_FILE" |
| 62 | + fi |
| 63 | + echo "job_html_url=" >> "$GITHUB_OUTPUT" |
| 64 | + exit 0 |
| 65 | + fi |
| 66 | +
|
| 67 | + HTTP_CODE=$(echo "$API_RESPONSE" | tail -n1) |
| 68 | + RESPONSE_BODY=$(echo "$API_RESPONSE" | sed '$d') |
| 69 | +
|
| 70 | + if [ "$HTTP_CODE" != "200" ]; then |
| 71 | + echo "⚠️ WARNING: GitHub API request failed with $HTTP_CODE" |
| 72 | + echo "job_html_url=" >> "$GITHUB_OUTPUT" |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | +
|
| 76 | + EXPECTED_JOB_NAME="${{ inputs.job-name }}" |
| 77 | + JOB_URL=$(echo "$RESPONSE_BODY" | jq -r \ |
| 78 | + --arg job_name "$EXPECTED_JOB_NAME" \ |
| 79 | + '.jobs[] | select(.name == $job_name) | .html_url') |
| 80 | +
|
| 81 | + if [ -z "$JOB_URL" ] || [ "$JOB_URL" = "null" ]; then |
| 82 | + echo "⚠️ WARNING: Failed to extract job URL from response for job name '$EXPECTED_JOB_NAME'." |
| 83 | + echo "Possible causes:" |
| 84 | + echo " - The job name does not match exactly (including spaces and case)." |
| 85 | + echo " - The job has not started yet at the time this action ran." |
| 86 | + echo " - The GitHub API response format was unexpected." |
| 87 | + echo "Please verify that the job has started before this action runs and double-check the exact job name in the GitHub Actions UI." |
| 88 | + echo "job_html_url=" >> "$GITHUB_OUTPUT" |
| 89 | + exit 0 |
| 90 | + fi |
| 91 | +
|
| 92 | + echo "job_html_url=$JOB_URL" >> "$GITHUB_OUTPUT" |
| 93 | + echo "Job URL: $JOB_URL" |
0 commit comments