-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlatest_artifact.sh
More file actions
executable file
·89 lines (70 loc) · 2.82 KB
/
latest_artifact.sh
File metadata and controls
executable file
·89 lines (70 loc) · 2.82 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Adapted from: https://gist.github.com/Willsr71/e4884be88f98b4c298692975c0ec8edb
github_token=$1
# NOTE: repository is the full name, e.g. owner/repo
repository=$2
pr_number=$3
branch_name=$4
workflow_name=$5
artifact_name=$6
echoerr() { echo "$@" 1>&2; }
workflows_url="https://api.github.com/repos/${repository}/actions/workflows"
workflow_page=1
workflow_count=0
workflow_ids=()
while true; do
workflows=$(curl -sS -H "Authorization: Bearer ${github_token}" "${workflows_url}?page=${workflow_page}")
workflow_ids+=($(
jq \
--arg workflow_name "$workflow_name" \
'.workflows[] | select(.name==$workflow_name).id' <<< ${workflows}
))
(( workflow_count += $(jq '.workflows | length' <<< ${workflows}) ))
(( ++workflow_page ))
if [[ $workflow_count -ge $(jq .total_count <<< ${workflows}) ]]; then
break
fi
done
if [[ ${#workflow_ids[@]} -eq 0 ]]; then
echoerr "No workflow found with name ${workflow_name}"
exit 0
fi
latest_workflow_id=${workflow_ids[${#workflow_ids[@]}-1]}
echoerr "Latest workflow ID: ${latest_workflow_id}"
workflow_runs_url="https://api.github.com/repos/${repository}/actions/workflows/${latest_workflow_id}/runs?status=success&branch=${branch_name}"
workflow_runs=$(curl -sS -H "Authorization: Bearer ${github_token}" "${workflow_runs_url}")
latest_workflow_run_id=$(jq \
--argjson pr_number "${pr_number}" \
'([.workflow_runs[] | select(.pull_requests | any(.number == $pr_number))] | max_by(.run_number)) | .id' <<< ${workflow_runs} || echo "ERROR")
if [ "${latest_workflow_run_id}" = "ERROR" ]; then
echoerr 'Failed to parse GitHub response with jq:'
echoerr "(url: ${workflow_runs_url})"
echoerr "${workflow_runs}"
exit 0
fi
if [ "${latest_workflow_run_id}" = "null" ]; then
echoerr "No successful workflow run found for PR ${pr_number} on branch ${branch_name}"
exit 0
fi
echoerr "Latest workflow run ID: ${latest_workflow_run_id}"
artifacts_url="https://api.github.com/repos/${repository}/actions/runs/${latest_workflow_run_id}/artifacts"
artifacts=$(curl -sS -H "Authorization: Bearer ${github_token}" "${artifacts_url}")
latest_artifact_id=$(echo ${artifacts} \
| jq \
--arg artifact_name "$artifact_name" \
'.artifacts[] | select(.name==$artifact_name).id' || echo "ERROR")
if [ "${latest_artifact_id}" = "ERROR" ]; then
echoerr 'Failed to parse GitHub response with jq:'
echoerr "(url: ${artifacts_url})"
echoerr "${artifacts}"
exit 0
fi
if [ "${latest_artifact_id}" = "null" ]; then
echoerr "No artifacts found for workflow run ${latest_workflow_run_id}"
exit 0
fi
echoerr "Latest artifact ID: ${latest_artifact_id}"
curl -sSL -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${github_token}" \
-o ${artifact_name}.zip https://api.github.com/repos/${repository}/actions/artifacts/${latest_artifact_id}/zip
unzip -q ${artifact_name}.zip