forked from convictional/trigger-workflow-and-wait
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
211 lines (176 loc) · 5.06 KB
/
entrypoint.sh
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash
set -e
usage_docs() {
echo ""
echo "You can use this Github Action with:"
echo "- uses: BillKalaitzis/trigger-workflow-and-wait"
echo " with:"
echo " owner: keithconvictional"
echo " repo: myrepo"
echo " github_token: \${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}"
echo " workflow_file_name: main.yaml"
}
GITHUB_API_URL="${API_URL:-https://api.github.com}"
GITHUB_SERVER_URL="${SERVER_URL:-https://github.com}"
validate_args() {
wait_interval=10 # Waits for 10 seconds
if [ "${INPUT_WAIT_INTERVAL}" ]
then
wait_interval=${INPUT_WAIT_INTERVAL}
fi
propagate_failure=true
if [ -n "${INPUT_PROPAGATE_FAILURE}" ]
then
propagate_failure=${INPUT_PROPAGATE_FAILURE}
fi
trigger_workflow=true
if [ -n "${INPUT_TRIGGER_WORKFLOW}" ]
then
trigger_workflow=${INPUT_TRIGGER_WORKFLOW}
fi
wait_workflow=true
if [ -n "${INPUT_WAIT_WORKFLOW}" ]
then
wait_workflow=${INPUT_WAIT_WORKFLOW}
fi
last_workflow_interval=0
if [ -n "${INPUT_LAST_WORKFLOW_INTERVAL}" ]
then
last_workflow_interval=${INPUT_LAST_WORKFLOW_INTERVAL}
fi
if [ -z "${INPUT_OWNER}" ]
then
echo "Error: Owner is a required argument."
usage_docs
exit 1
fi
if [ -z "${INPUT_REPO}" ]
then
echo "Error: Repo is a required argument."
usage_docs
exit 1
fi
if [ -z "${INPUT_GITHUB_TOKEN}" ]
then
echo "Error: Github token is required. You can head over settings and"
echo "under developer, you can create a personal access tokens. The"
echo "token requires repo access."
usage_docs
exit 1
fi
if [ -z "${INPUT_WORKFLOW_FILE_NAME}" ]
then
echo "Error: Workflow File Name is required"
usage_docs
exit 1
fi
client_payload=$(echo '{}' | jq)
if [ "${INPUT_CLIENT_PAYLOAD}" ]
then
client_payload=$(echo "${INPUT_CLIENT_PAYLOAD}" | jq)
fi
ref="main"
if [ "$INPUT_REF" ]
then
ref="${INPUT_REF}"
fi
}
api() {
path=$1; shift
if response=$(curl --fail-with-body -sSL \
"${GITHUB_API_URL}/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/$path" \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
-H 'Accept: application/vnd.github.v3+json' \
-H 'Content-Type: application/json' \
"$@")
then
echo "$response"
else
echo >&2 "api failed:"
echo >&2 "path: $path"
echo >&2 "response: $response"
exit 1
fi
}
# Return the ids of the most recent workflow runs, optionally filtered by user
get_workflow_runs() {
since=${1:?}
query="per_page=100"
echo "Getting workflow runs using query: ${query}" >&2
api "workflows/${INPUT_WORKFLOW_FILE_NAME}/runs?${query}" |
jq ".workflow_runs[] | select((.event==\"workflow_dispatch\") and ((.created_at | fromdateiso8601) >= $since))" |
jq .id |
sort # Sort to ensure repeatable order, and lexicographically for compatibility with join
}
trigger_workflow() {
START_TIME=$(date -u +%s)
SINCE=$((START_TIME - 120)) # Two minutes ago, to overcome clock skew
OLD_RUNS=$(get_workflow_runs "$SINCE")
echo >&2 "Triggering workflow:"
echo >&2 " workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches"
echo >&2 " {\"ref\":\"${ref}\",\"inputs\":${client_payload}}"
api "workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches" \
--data "{\"ref\":\"${ref}\",\"inputs\":${client_payload}}"
NEW_RUNS=$OLD_RUNS
while [ "$NEW_RUNS" = "$OLD_RUNS" ]
do
echo >&2 "Sleeping for ${wait_interval} seconds"
sleep "$wait_interval"
NEW_RUNS=$(get_workflow_runs "$SINCE")
done
# Return new run ids
join -v2 <(echo "$OLD_RUNS") <(echo "$NEW_RUNS")
}
wait_for_workflow_to_finish() {
last_workflow_id=${1:?}
last_workflow_url="${GITHUB_SERVER_URL}/${INPUT_OWNER}/${INPUT_REPO}/actions/runs/${last_workflow_id}"
echo "Waiting for workflow to finish:"
echo "The workflow id is [${last_workflow_id}]."
echo "The workflow logs can be found at ${last_workflow_url}"
echo "::set-output name=workflow_id::${last_workflow_id}"
echo "::set-output name=workflow_url::${last_workflow_url}"
echo ""
conclusion=null
status=
while [[ "${conclusion}" == "null" && "${status}" != "completed" ]]
do
echo "Sleeping for \"${wait_interval}\" seconds"
sleep "${wait_interval}"
workflow=$(api "runs/$last_workflow_id")
conclusion=$(echo "${workflow}" | jq -r '.conclusion')
status=$(echo "${workflow}" | jq -r '.status')
echo "Checking conclusion [${conclusion}]"
echo "Checking status [${status}]"
done
if [[ "${conclusion}" == "success" && "${status}" == "completed" ]]
then
echo "Yes, success"
else
# Alternative "failure"
echo "Conclusion is not success, it's [${conclusion}]."
if [ "${propagate_failure}" = true ]
then
echo "Propagating failure to upstream job"
exit 1
fi
fi
}
main() {
validate_args
if [ "${trigger_workflow}" = true ]
then
run_ids=$(trigger_workflow)
else
echo "Skipping triggering the workflow."
fi
if [ "${wait_workflow}" = true ]
then
for run_id in $run_ids
do
wait_for_workflow_to_finish "$run_id"
done
else
echo "Skipping waiting for workflow."
fi
}
main