Skip to content

Commit 713ba5f

Browse files
IONOS(build): create get-job-data action to retrieve GitHub workflow information
Created a new composite action that fetches job data from GitHub API and integrated it into the build workflow to enrich artifact metadata.
1 parent b788dee commit 713ba5f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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"

.github/workflows/build-artifact.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ jobs:
302302
# Ping the server
303303
jf rt ping
304304
305+
- name: Get Job data
306+
id: get_job_data
307+
continue-on-error: true
308+
uses: ./.github/actions/get-job-data
309+
with:
310+
job-name: 'build-external-apps (${{ matrix.app_info.name }})'
311+
github-token: ${{ github.token }}
312+
repository: ${{ github.repository }}
313+
run-id: ${{ github.run_id }}
314+
305315
- name: Push ${{ matrix.app_info.name }} to JFrog
306316
run: |
307317
set -e
@@ -354,6 +364,12 @@ jobs:
354364
JFROG_PROPS_LIST+=("vcs.branch=${{ github.ref_name }}")
355365
JFROG_PROPS_LIST+=("vcs.revision=${{ github.sha }}")
356366
367+
# Add job URL if available
368+
JOB_URL="${{ steps.get_job_data.outputs.job_html_url }}"
369+
if [ -n "$JOB_URL" ]; then
370+
JFROG_PROPS_LIST+=("job.html_url=${JOB_URL}")
371+
fi
372+
357373
# Join properties into a single semicolon-separated string using IFS and array expansion
358374
# This bash technique temporarily sets IFS (Internal Field Separator) to ';' and expands
359375
# the array with [*] which joins elements using the first character of IFS
@@ -648,6 +664,12 @@ jobs:
648664
BUILD_NAME: "nextcloud-workspace-snapshot"
649665

650666
steps:
667+
# Checkout is required to access the local composite action at ./.github/actions/get-job-data
668+
- name: Checkout repository
669+
uses: actions/checkout@v5
670+
with:
671+
fetch-depth: 1
672+
651673
- name: Check prerequisites
652674
run: |
653675
# count the number of secrets that are set
@@ -693,6 +715,16 @@ jobs:
693715
# Ping the server
694716
jf rt ping
695717
718+
- name: Get Job data
719+
id: get_job_data
720+
continue-on-error: true
721+
uses: ./.github/actions/get-job-data
722+
with:
723+
job-name: 'Push to artifactory'
724+
github-token: ${{ github.token }}
725+
repository: ${{ github.repository }}
726+
run-id: ${{ github.run_id }}
727+
696728
- name: Upload build to artifactory
697729
id: artifactory_upload
698730
run: |
@@ -721,6 +753,12 @@ jobs:
721753
JFROG_PROPS_LIST+=("vcs.branch=${{ github.ref_name }}")
722754
JFROG_PROPS_LIST+=("vcs.revision=${{ github.sha }}")
723755
756+
# Add job URL if available
757+
JOB_URL="${{ steps.get_job_data.outputs.job_html_url }}"
758+
if [ -n "$JOB_URL" ]; then
759+
JFROG_PROPS_LIST+=("job.html_url=${JOB_URL}")
760+
fi
761+
724762
# Join properties into a single semicolon-separated string using IFS and array expansion
725763
# This bash technique temporarily sets IFS (Internal Field Separator) to ';' and expands
726764
# the array with [*] which joins elements using the first character of IFS

0 commit comments

Comments
 (0)