-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdispatch_internal_repo_workflow.sh
More file actions
executable file
·345 lines (301 loc) · 10.5 KB
/
dispatch_internal_repo_workflow.sh
File metadata and controls
executable file
·345 lines (301 loc) · 10.5 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/bash
# Triggers a remote GitHub workflow in nhs-notify-internal and waits for completion.
# Usage:
# ./dispatch_internal_repo_workflow.sh \
# --infraRepoName <repo> \
# --releaseVersion <version> \
# --targetWorkflow <workflow.yaml> \
# --targetEnvironment <env> \
# --targetComponent <component> \
# --targetAccountGroup <group> \
# --terraformAction <action> \
# --internalRef <ref> \
# --overrides <overrides> \
# --overrideProjectName <name> \
# --overrideRoleName <name>
#
# All arguments are required except terraformAction, and internalRef.
# Example:
# ./dispatch_internal_repo_workflow.sh \
# --infraRepoName "nhs-notify-dns" \
# --releaseVersion "v1.2.3" \
# --targetWorkflow "deploy.yaml" \
# --targetEnvironment "prod" \
# --targetComponent "web" \
# --targetAccountGroup "core" \
# --terraformAction "apply" \
# --internalRef "main" \
# --overrides "tf_var=someString" \
# --overrideProjectName nhs \
# --overrideRoleName nhs-service-iam-role
set -e
usage() {
cat >&2 <<'EOF'
Usage:
./dispatch_internal_repo_workflow.sh \
--infraRepoName <repo> \
--releaseVersion <version> \
--targetWorkflow <workflow.yaml> \
--targetEnvironment <env> \
--targetComponent <component> \
--targetAccountGroup <group> \
[--terraformAction <action>] \
[--internalRef <ref>] \
[--overrides <overrides>] \
[--overrideProjectName <name>] \
[--overrideRoleName <name>]
EOF
return 0
}
require_arg() {
local name="$1"
local value="$2"
if [[ -z "$value" ]]; then
echo "[ERROR] Missing required argument: $name" >&2
usage
exit 1
fi
return 0
}
while [[ $# -gt 0 ]]; do
case $1 in
--infraRepoName) # Name of the infrastructure repo in NHSDigital org (required)
infraRepoName="$2"
shift 2
;;
--releaseVersion) # Release version, commit, or tag to deploy (required)
releaseVersion="$2"
shift 2
;;
--targetWorkflow) # Name of the workflow file to call in nhs-notify-internal (required)
targetWorkflow="$2"
shift 2
;;
--targetEnvironment) # Terraform environment to deploy (required)
targetEnvironment="$2"
shift 2
;;
--targetComponent) # Terraform component to deploy (required)
targetComponent="$2"
shift 2
;;
--targetAccountGroup) # Terraform account group to deploy (required)
targetAccountGroup="$2"
shift 2
;;
--terraformAction) # Terraform action to run (optional)
terraformAction="$2"
shift 2
;;
--internalRef) # Internal repo reference branch or tag (optional, default: "main")
internalRef="$2"
shift 2
;;
--overrides) # Terraform overrides for passing in extra variables (optional)
overrides="$2"
shift 2
;;
--overrideProjectName) # Override the project name (optional)
overrideProjectName="$2"
shift 2
;;
--overrideRoleName) # Override the role name (optional)
overrideRoleName="$2"
shift 2
;;
*)
echo "[ERROR] Unknown argument: $1" >&2
exit 1
;;
esac
done
require_arg "--infraRepoName" "${infraRepoName:-}"
require_arg "--releaseVersion" "${releaseVersion:-}"
require_arg "--targetWorkflow" "${targetWorkflow:-}"
require_arg "--targetEnvironment" "${targetEnvironment:-}"
require_arg "--targetComponent" "${targetComponent:-}"
require_arg "--targetAccountGroup" "${targetAccountGroup:-}"
if [[ -z "$APP_PEM_FILE" ]]; then
echo "[ERROR] PEM_FILE environment variable is not set or is empty." >&2
exit 1
fi
if [[ -z "$APP_CLIENT_ID" ]]; then
echo "[ERROR] CLIENT_ID environment variable is not set or is empty." >&2
exit 1
fi
now=$(date +%s)
iat=$((${now} - 60)) # Issues 60 seconds in the past
exp=$((${now} + 600)) # Expires 10 minutes in the future
b64enc() {
openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'
return 0
}
header_json='{
"typ":"JWT",
"alg":"RS256"
}'
# Header encode
header=$( echo -n "${header_json}" | b64enc )
payload_json="{
\"iat\":${iat},
\"exp\":${exp},
\"iss\":\"${APP_CLIENT_ID}\"
}"
# Payload encode
payload=$( echo -n "${payload_json}" | b64enc )
# Signature
header_payload="${header}"."${payload}"
signature=$(
openssl dgst -sha256 -sign <(echo -n "${APP_PEM_FILE}") \
<(echo -n "${header_payload}") | b64enc
)
# Create JWT
JWT="${header_payload}"."${signature}"
INSTALLATION_ID=$(curl -X GET \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${JWT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--url "https://api.github.com/app/installations" | jq -r '.[0].id')
PR_TRIGGER_PAT=$(curl --request POST \
--url "https://api.github.com/app/installations/${INSTALLATION_ID}/access_tokens" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${JWT}" \
-H "X-GitHub-Api-Version: 2022-11-28" | jq -r '.token')
# Set default values if not provided
if [[ -z "$PR_TRIGGER_PAT" ]]; then
echo "[ERROR] PR_TRIGGER_PAT environment variable is not set or is empty." >&2
exit 1
fi
if [[ -z "$overrides" ]]; then
overrides=""
fi
if [[ -z "$internalRef" ]]; then
internalRef="main"
fi
echo "==================== Workflow Dispatch Parameters ===================="
echo " infraRepoName: $infraRepoName"
echo " releaseVersion: $releaseVersion"
echo " targetWorkflow: $targetWorkflow"
echo " targetEnvironment: $targetEnvironment"
echo " targetComponent: $targetComponent"
echo " targetAccountGroup: $targetAccountGroup"
echo " terraformAction: $terraformAction"
echo " internalRef: $internalRef"
echo " overrides: $overrides"
echo " overrideProjectName: $overrideProjectName"
echo " overrideRoleName: $overrideRoleName"
DISPATCH_EVENT=$(jq -ncM \
--arg internalRef "$internalRef" \
--arg infraRepoName "$infraRepoName" \
--arg releaseVersion "$releaseVersion" \
--arg targetEnvironment "$targetEnvironment" \
--arg targetAccountGroup "$targetAccountGroup" \
--arg targetComponent "$targetComponent" \
--arg terraformAction "$terraformAction" \
--arg targetWorkflow "$targetWorkflow" \
--arg overrides "$overrides" \
--arg overrideProjectName "$overrideProjectName" \
--arg overrideRoleName "$overrideRoleName" \
'{
"ref": $internalRef,
"inputs": (
(if $infraRepoName != "" then { "infraRepoName": $infraRepoName } else {} end) +
(if $terraformAction != "" then { "terraformAction": $terraformAction } else {} end) +
(if $overrideProjectName != "" then { "overrideProjectName": $overrideProjectName } else {} end) +
(if $overrideRoleName != "" then { "overrideRoleName": $overrideRoleName } else {} end) +
{
"releaseVersion": $releaseVersion,
"targetEnvironment": $targetEnvironment,
"targetAccountGroup": $targetAccountGroup,
"targetComponent": $targetComponent,
"overrides": $overrides
}
)
}')
echo "[INFO] Triggering workflow '$targetWorkflow' in nhs-notify-internal..."
trigger_response=$(curl -s -L \
--fail \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/$targetWorkflow/dispatches" \
-d "$DISPATCH_EVENT" 2>&1)
if [[ $? -ne 0 ]]; then
echo "[ERROR] Failed to trigger workflow. Response: $trigger_response" >&2
exit 1
fi
echo "[INFO] Workflow trigger request sent successfully, waiting for completion..."
sleep 10 # Wait a few seconds before checking for the presence of the api to account for GitHub updating
# Poll GitHub API to check the workflow status
workflow_run_url=""
for _ in {1..18}; do
response=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch")
if ! echo "$response" | jq empty 2>/dev/null; then
echo "[ERROR] Invalid JSON response from GitHub API during workflow polling:" >&2
echo "$response" >&2
exit 1
fi
workflow_run_url=$(echo "$response" | jq -r \
--arg targetWorkflow "$targetWorkflow" \
--arg targetEnvironment "$targetEnvironment" \
--arg targetAccountGroup "$targetAccountGroup" \
--arg targetComponent "$targetComponent" \
--arg terraformAction "$terraformAction" \
'.workflow_runs[]
| select(.path == ".github/workflows/" + $targetWorkflow)
| select(.name
| contains($targetEnvironment)
and contains($targetAccountGroup)
and contains($targetComponent)
and contains($terraformAction)
)
| .url')
if [[ -n "$workflow_run_url" && "$workflow_run_url" != null ]]; then
# Workflow_run_url is a list of all workflows which were run for this combination of inputs, but are the API uri
workflow_run_url=$(echo "$workflow_run_url" | head -n 1)
# Take the first and strip it back to being an accessible url
# Example https://api.github.com/repos/MyOrg/my-repo/actions/runs/12346789 becomes
# becomes https://github.com/MyOrg/my-repo/actions/runs/12346789
workflow_run_ui_url=${workflow_run_url/api./} # Strips the api. prefix
workflow_run_ui_url=${workflow_run_ui_url/\/repos/} # Strips the repos/ uri
echo "[INFO] Found workflow run url: $workflow_run_ui_url"
break
fi
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Waiting for workflow to start..."
sleep 10
done
if [[ -z "$workflow_run_url" || "$workflow_run_url" == null ]]; then
echo "[ERROR] Failed to get the workflow run url. Exiting." >&2
exit 1
fi
# Wait for workflow completion
while true; do
sleep 10
response=$(curl -s -L \
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
-H "Accept: application/vnd.github+json" \
"$workflow_run_url")
status=$(echo "$response" | jq -r '.status')
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Workflow status: $status"
if [[ "$status" == "completed" ]]; then
conclusion=$(echo "$response" | jq -r '.conclusion')
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Workflow conclusion: $conclusion"
if [[ -z "$conclusion" || "$conclusion" == "null" ]]; then
echo "[WARN] Workflow marked completed but conclusion not yet available, retrying..."
sleep 5
continue
fi
if [[ "$conclusion" == "success" ]]; then
echo "[SUCCESS] Workflow completed successfully!"
exit 0
else
echo "[FAIL] Workflow failed with conclusion: $conclusion" >&2
exit 1
fi
fi
done