|
Here’s a Github Action to get the URL to the current job and specific step in
the job to open. This is useful for giving users a direct link to specific jobs.
name: Get current job URL
description: Output the current Github Workflow Job URL
inputs:
step_name:
description: Specific job step to include in the URL
required: true
outputs:
job_url:
description: "URL to the calling job and specific input step"
value: ${{steps.get_job_url.outputs.job_url}}
runs:
using: "composite"
steps:
- id: get_job_url
env:
GH_TOKEN: ${{github.token}}
shell: bash
run: |
echo "job_url=$(gh run --repo ${{github.repository}} view ${{github.run_id}} \
--json jobs \
--jq '.jobs[]
| select(.name == "${{github.job}}")
| .url,
(
.steps[]
| select(.name == "${{inputs.step_name}}")
| "#step:\(.number):1"
)' \
| tr -d "\n")" | tee "$GITHUB_OUTPUT"
The key part is this bash script which uses the gh CLI tool’s --jq option
to extract the URL:
echo "job_url=$(gh run --repo ${{github.repository}} view ${{github.run_id}} \
--json jobs \
--jq '.jobs[]
| select(.name == "${{github.job}}")
| .url,
(
.steps[]
| select(.name == "${{inputs.step_name}}")
| "#step:\(.number):1"
)' \
| tr -d "\n")" | tee "$GITHUB_OUTPUT"
You can access this job URL output in other Github Action steps via
${{steps.job_url.outputs.job_url}} , like this:
# ...
jobs:
foobar:
steps:
- uses: ./.github/actions/job_url
id: job_url
with:
step_name: 'some_step'
- shell: bash
run: |
echo "${{steps.job_url.outputs.job_url}}"
See also: https://docs.github.com/en/actions/sharing-automations/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions
View post:
Github Action to get the current job URL
|
|
|
|