forked from Tiryoh/gha-jobid-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
53 lines (50 loc) · 2.2 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
name: "GitHub Actions job_id parser"
description: "GitHub Action to get the current workflow run's job_id"
inputs:
github_token:
description: "GITHUB_TOKEN to use GitHub API v3"
required: true
repository:
description: "target GitHub repository"
required: false
run_id:
description: "run_id of target workflow run"
required: false
job_name:
description: "job_name of tartget workflow jobs"
required: true
per_page:
description: "Results per page (max 100) of target workflow run"
required: false
outputs:
job_id:
description: "job_id of target workflow jobs"
value: ${{ steps.request.outputs.job_id }}
html_url:
description: "html_url of target workflow jobs"
value: ${{ steps.request.outputs.html_url }}
branding:
icon: "link"
color: "blue"
runs:
using: 'composite'
steps:
- name: Parse job_id
id: request
env:
INPUT_GITHUB_TOKEN: ${{ inputs.github_token }}
INPUT_REPOSITORY: ${{ inputs.repository }}
INPUT_RUN_ID: ${{ inputs.run_id }}
INPUT_JOB_NAME: ${{ inputs.job_name }}
INPUT_PER_PAGE: ${{ inputs.per_page }}
GITHUB_BASEURL: ${{ github.api_url }}
shell: bash
run: |
GITHUB_API="/repos/${INPUT_REPOSITORY:-${GITHUB_REPOSITORY}}/actions/runs/${INPUT_RUN_ID:-${GITHUB_RUN_ID}}/jobs"
JOBINFO="$(curl --get -Ss -H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "${GITHUB_BASEURL}${GITHUB_API}?per_page=${INPUT_PER_PAGE:-30}")"
echo "${JOBINFO}" | grep "Resource not accessible by integration" && exit 1
TOTAL_COUNT="$(echo "${JOBINFO}" | jq -r .total_count)"
eval "$(echo ${JOBINFO} | jq -r --arg job_name "${INPUT_JOB_NAME}" '.jobs | map(select(.name == $job_name)) | .[0] | @sh "JOB_ID=\(.id) HTML_URL=\(.html_url)"')"
echo ${JOB_ID} | grep "null" > /dev/null && echo "parse error, job_id is ${JOB_ID} and total_count is ${TOTAL_COUNT}. 'job_name' or 'per_page' might be wrong. See https://github.com/Tiryoh/gha-jobid-action/blob/main/README.md#inputs" && exit 1
echo "job_id=${JOB_ID}" >> $GITHUB_OUTPUT
echo "html_url=${HTML_URL}" >> $GITHUB_OUTPUT