Skip to content

Commit 3b29a62

Browse files
CCM-11942 Uses shared dispatch workflow with tracking
1 parent af2e7f4 commit 3b29a62

File tree

6 files changed

+304
-388
lines changed

6 files changed

+304
-388
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#!/bin/bash
2+
3+
# Triggers a remote GitHub workflow in nhs-notify-internal and waits for completion.
4+
5+
# Usage:
6+
# ./dispatch_internal_repo_workflow.sh \
7+
# --jobName <name> \
8+
# --infraRepoName <repo> \
9+
# --releaseVersion <version> \
10+
# --targetWorkflow <workflow.yaml> \
11+
# --targetEnvironment <env> \
12+
# --targetComponent <component> \
13+
# --targetAccountGroup <group> \
14+
# --terraformAction <action> \
15+
# --internalRef <ref>
16+
#
17+
# All arguments are required except jobName, terraformAction, and internalRef.
18+
# Example:
19+
# ./dispatch_internal_repo_workflow.sh \
20+
# --jobName "Deploy" \
21+
# --infraRepoName "nhs-notify-web-template-management" \
22+
# --releaseVersion "v1.2.3" \
23+
# --targetWorkflow "deploy.yaml" \
24+
# --targetEnvironment "prod" \
25+
# --targetComponent "web" \
26+
# --targetAccountGroup "core" \
27+
# --terraformAction "apply" \
28+
# --internalRef "main"
29+
30+
set -e
31+
32+
while [[ $# -gt 0 ]]; do
33+
case $1 in
34+
--jobName) # Name of the job triggering the remote workflow (optional)
35+
jobName="$2"
36+
shift 2
37+
;;
38+
--infraRepoName) # Name of the infrastructure repo in NHSDigital org (required)
39+
infraRepoName="$2"
40+
shift 2
41+
;;
42+
--releaseVersion) # Release version, commit, or tag to deploy (required)
43+
releaseVersion="$2"
44+
shift 2
45+
;;
46+
--targetWorkflow) # Name of the workflow file to call in nhs-notify-internal (required)
47+
targetWorkflow="$2"
48+
shift 2
49+
;;
50+
--targetEnvironment) # Terraform environment to deploy (required)
51+
targetEnvironment="$2"
52+
shift 2
53+
;;
54+
--targetComponent) # Terraform component to deploy (required)
55+
targetComponent="$2"
56+
shift 2
57+
;;
58+
--targetAccountGroup) # Terraform account group to deploy (required)
59+
targetAccountGroup="$2"
60+
shift 2
61+
;;
62+
--terraformAction) # Terraform action to run (optional, default: "plan")
63+
terraformAction="$2"
64+
shift 2
65+
;;
66+
--internalRef) # Internal repo reference branch or tag (optional, default: "main")
67+
internalRef="$2"
68+
shift 2
69+
;;
70+
*)
71+
echo "Unknown argument: $1"
72+
exit 1
73+
;;
74+
esac
75+
done
76+
77+
# Set default values if not provided
78+
if [[ -z "$PR_TRIGGER_PAT" ]]; then
79+
echo "Error: PR_TRIGGER_PAT environment variable is not set or is empty."
80+
exit 1
81+
fi
82+
83+
if [[ -z "$jobName" ]]; then
84+
jobName="${infraRepoName}-${targetComponent}-${terraformAction}"
85+
fi
86+
87+
if [[ -z "$terraformAction" ]]; then
88+
terraformAction="plan"
89+
fi
90+
91+
if [[ -z "$internalRef" ]]; then
92+
internalRef="main"
93+
fi
94+
95+
96+
callerRunId="${GITHUB_RUN_ID}-${jobName}-${GITHUB_RUN_ATTEMPT}"
97+
98+
DISPATCH_EVENT=$(jq -ncM \
99+
--arg infraRepoName "$infraRepoName" \
100+
--arg releaseVersion "$releaseVersion" \
101+
--arg targetEnvironment "$targetEnvironment" \
102+
--arg targetAccountGroup "$targetAccountGroup" \
103+
--arg targetComponent "$targetComponent" \
104+
--arg terraformAction "$terraformAction" \
105+
--arg callerRunId "$callerRunId" \
106+
--arg targetWorkflow "$targetWorkflow" \
107+
'{
108+
"ref": "'"$internalRef"'",
109+
"inputs": (
110+
(if $infraRepoName != "" then { "infraRepoName": $infraRepoName } else {} end) +
111+
(if $terraformAction != "" then { "terraformAction": $terraformAction } else {} end) +
112+
{
113+
"releaseVersion": $releaseVersion,
114+
"targetEnvironment": $targetEnvironment,
115+
"targetAccountGroup": $targetAccountGroup,
116+
"targetComponent": $targetComponent
117+
} +
118+
(if ($targetWorkflow | test("dispatch-(acceptance|contextual|product|security)-tests-.*\\.yaml"))
119+
then { "callerRunId": $callerRunId } else {} end)
120+
)
121+
}')
122+
123+
# Trigger the workflow
124+
curl -L \
125+
--fail \
126+
--silent \
127+
-X POST \
128+
-H "Accept: application/vnd.github+json" \
129+
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
130+
-H "X-GitHub-Api-Version: 2022-11-28" \
131+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/$targetWorkflow/dispatches" \
132+
-d "$DISPATCH_EVENT"
133+
134+
echo "Workflow triggered. Waiting for the workflow to complete.."
135+
136+
# Poll GitHub API to check the workflow status
137+
workflow_run_url=""
138+
for _ in {1..18}; do
139+
workflow_run_url=$(curl -s \
140+
-H "Accept: application/vnd.github+json" \
141+
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
142+
-H "X-GitHub-Api-Version: 2022-11-28" \
143+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch" \
144+
| jq -r \
145+
--arg callerRunId "$callerRunId" \
146+
--arg targetWorkflow "$targetWorkflow" \
147+
--arg targetEnvironment "$targetEnvironment" \
148+
--arg targetAccountGroup "$targetAccountGroup" \
149+
--arg targetComponent "$targetComponent" \
150+
--arg terraformAction "$terraformAction" \
151+
'.workflow_runs[]
152+
| select(.path == ".github/workflows/" + $targetWorkflow)
153+
| select(.name
154+
| contains($targetEnvironment)
155+
and contains($targetAccountGroup)
156+
and contains($targetComponent)
157+
and contains($terraformAction)
158+
)
159+
| if ($targetWorkflow | test("dispatch-(acceptance|contextual|product|security)-tests-.*\\.yaml"))
160+
then select(.name | contains("caller:" + $callerRunId))
161+
else .
162+
end
163+
| .url')
164+
165+
if [[ -n "$workflow_run_url" && "$workflow_run_url" != null ]]; then
166+
ui_url=${workflow_run_url/api./}
167+
ui_url=${ui_url/\/repos/}
168+
echo "Found workflow run url: $ui_url"
169+
echo "workflow_run_url=$workflow_run_url" >> "$GITHUB_ENV"
170+
break
171+
fi
172+
173+
echo "Waiting for workflow to start..."
174+
sleep 10
175+
done
176+
177+
if [[ -z "$workflow_run_url" || "$workflow_run_url" == null ]]; then
178+
echo "Failed to get the workflow run url. Exiting."
179+
exit 1
180+
fi
181+
182+
# Wait for workflow completion
183+
while true; do
184+
sleep 10
185+
response=$(curl -s -L \
186+
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
187+
-H "Accept: application/vnd.github+json" \
188+
"$workflow_run_url")
189+
190+
status=$(echo "$response" | jq -r '.status')
191+
conclusion=$(echo "$response" | jq -r '.conclusion')
192+
193+
if [ "$status" == "completed" ]; then
194+
if [ -z "$conclusion" ] || [ "$conclusion" == "null" ]; then
195+
echo "Workflow marked completed but conclusion not yet available, retrying..."
196+
sleep 5
197+
continue
198+
fi
199+
200+
if [ "$conclusion" == "success" ]; then
201+
echo "Workflow completed successfully."
202+
exit 0
203+
else
204+
echo "Workflow failed with conclusion: $conclusion"
205+
exit 1
206+
fi
207+
fi
208+
209+
echo "Workflow still running..."
210+
sleep 20
211+
done

.github/workflows/dispatch_internal_repo_workflow.yaml

Lines changed: 0 additions & 171 deletions
This file was deleted.

0 commit comments

Comments
 (0)