Skip to content

Commit 967c9c0

Browse files
CCM-11942 Uses shared dispatch workflow with tracking
1 parent 17ddf0a commit 967c9c0

File tree

1 file changed

+143
-143
lines changed

1 file changed

+143
-143
lines changed

.github/scripts/dispatch_internal_repo_workflow.sh

Lines changed: 143 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -30,182 +30,182 @@
3030
set -e
3131

3232
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
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
7575
done
7676

7777
# Set default values if not provided
7878
if [[ -z "$PR_TRIGGER_PAT" ]]; then
79-
echo "Error: PR_TRIGGER_PAT environment variable is not set or is empty."
80-
exit 1
79+
echo "Error: PR_TRIGGER_PAT environment variable is not set or is empty."
80+
exit 1
8181
fi
8282

8383
if [[ -z "$jobName" ]]; then
84-
jobName="${infraRepoName}-${targetComponent}-${terraformAction}"
84+
jobName="${infraRepoName}-${targetComponent}-${terraformAction}"
8585
fi
8686

8787
if [[ -z "$terraformAction" ]]; then
88-
terraformAction="plan"
88+
terraformAction="plan"
8989
fi
9090

9191
if [[ -z "$internalRef" ]]; then
92-
internalRef="main"
92+
internalRef="main"
9393
fi
9494

9595

9696
callerRunId="${GITHUB_RUN_ID}-${jobName}-${GITHUB_RUN_ATTEMPT}"
9797

9898
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-
}')
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+
}')
122122

123123
# Trigger the workflow
124124
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"
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"
133133

134134
echo "Workflow triggered. Waiting for the workflow to complete.."
135135

136136
# Poll GitHub API to check the workflow status
137137
workflow_run_url=""
138138
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
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
175175
done
176176

177177
if [[ -z "$workflow_run_url" || "$workflow_run_url" == null ]]; then
178-
echo "Failed to get the workflow run url. Exiting."
179-
exit 1
178+
echo "Failed to get the workflow run url. Exiting."
179+
exit 1
180180
fi
181181

182182
# Wait for workflow completion
183183
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
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
211211
done

0 commit comments

Comments
 (0)