Skip to content

Commit e1be408

Browse files
authored
CCM-11942: Update workflows to use common dispatch workflow script (#82)
* CCM-11942: Update workflows to use common dispatch workflow script * CCM-11942: Update workflows to use common dispatch workflow script * CCM-11942: Update workflows to use common dispatch workflow script
1 parent fa843e6 commit e1be408

File tree

4 files changed

+284
-162
lines changed

4 files changed

+284
-162
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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+
# --infraRepoName <repo> \
8+
# --releaseVersion <version> \
9+
# --targetWorkflow <workflow.yaml> \
10+
# --targetEnvironment <env> \
11+
# --targetComponent <component> \
12+
# --targetAccountGroup <group> \
13+
# --terraformAction <action> \
14+
# --internalRef <ref> \
15+
# --overrides <overrides> \
16+
# --overrideProjectName <name> \
17+
# --overrideRoleName <name>
18+
19+
#
20+
# All arguments are required except terraformAction, and internalRef.
21+
# Example:
22+
# ./dispatch_internal_repo_workflow.sh \
23+
# --infraRepoName "nhs-notify-dns" \
24+
# --releaseVersion "v1.2.3" \
25+
# --targetWorkflow "deploy.yaml" \
26+
# --targetEnvironment "prod" \
27+
# --targetComponent "web" \
28+
# --targetAccountGroup "core" \
29+
# --terraformAction "apply" \
30+
# --internalRef "main" \
31+
# --overrides "tf_var=someString" \
32+
# --overrideProjectName nhs \
33+
# --overrideRoleName nhs-service-iam-role
34+
35+
set -e
36+
37+
while [[ $# -gt 0 ]]; do
38+
case $1 in
39+
--infraRepoName) # Name of the infrastructure repo in NHSDigital org (required)
40+
infraRepoName="$2"
41+
shift 2
42+
;;
43+
--releaseVersion) # Release version, commit, or tag to deploy (required)
44+
releaseVersion="$2"
45+
shift 2
46+
;;
47+
--targetWorkflow) # Name of the workflow file to call in nhs-notify-internal (required)
48+
targetWorkflow="$2"
49+
shift 2
50+
;;
51+
--targetEnvironment) # Terraform environment to deploy (required)
52+
targetEnvironment="$2"
53+
shift 2
54+
;;
55+
--targetComponent) # Terraform component to deploy (required)
56+
targetComponent="$2"
57+
shift 2
58+
;;
59+
--targetAccountGroup) # Terraform account group to deploy (required)
60+
targetAccountGroup="$2"
61+
shift 2
62+
;;
63+
--terraformAction) # Terraform action to run (optional)
64+
terraformAction="$2"
65+
shift 2
66+
;;
67+
--internalRef) # Internal repo reference branch or tag (optional, default: "main")
68+
internalRef="$2"
69+
shift 2
70+
;;
71+
--overrides) # Terraform overrides for passing in extra variables (optional)
72+
overrides="$2"
73+
shift 2
74+
;;
75+
--overrideProjectName) # Override the project name (optional)
76+
overrideProjectName="$2"
77+
shift 2
78+
;;
79+
--overrideRoleName) # Override the role name (optional)
80+
overrideRoleName="$2"
81+
shift 2
82+
;;
83+
*)
84+
echo "[ERROR] Unknown argument: $1"
85+
exit 1
86+
;;
87+
esac
88+
done
89+
90+
# Set default values if not provided
91+
if [[ -z "$PR_TRIGGER_PAT" ]]; then
92+
echo "[ERROR] PR_TRIGGER_PAT environment variable is not set or is empty."
93+
exit 1
94+
fi
95+
96+
if [[ -z "$overrides" ]]; then
97+
overrides=""
98+
fi
99+
100+
if [[ -z "$internalRef" ]]; then
101+
internalRef="main"
102+
fi
103+
104+
echo "==================== Workflow Dispatch Parameters ===================="
105+
echo " infraRepoName: $infraRepoName"
106+
echo " releaseVersion: $releaseVersion"
107+
echo " targetWorkflow: $targetWorkflow"
108+
echo " targetEnvironment: $targetEnvironment"
109+
echo " targetComponent: $targetComponent"
110+
echo " targetAccountGroup: $targetAccountGroup"
111+
echo " terraformAction: $terraformAction"
112+
echo " internalRef: $internalRef"
113+
echo " overrides: $overrides"
114+
echo " overrideProjectName: $overrideProjectName"
115+
echo " overrideRoleName: $overrideRoleName"
116+
echo " targetProject: $targetProject"
117+
118+
DISPATCH_EVENT=$(jq -ncM \
119+
--arg infraRepoName "$infraRepoName" \
120+
--arg releaseVersion "$releaseVersion" \
121+
--arg targetEnvironment "$targetEnvironment" \
122+
--arg targetAccountGroup "$targetAccountGroup" \
123+
--arg targetComponent "$targetComponent" \
124+
--arg terraformAction "$terraformAction" \
125+
--arg targetWorkflow "$targetWorkflow" \
126+
--arg overrides "$overrides" \
127+
--arg overrideProjectName "$overrideProjectName" \
128+
--arg overrideRoleName "$overrideRoleName" \
129+
--arg targetProject "$targetProject" \
130+
'{
131+
"ref": "'"$internalRef"'",
132+
"inputs": (
133+
(if $infraRepoName != "" then { "infraRepoName": $infraRepoName } else {} end) +
134+
(if $terraformAction != "" then { "terraformAction": $terraformAction } else {} end) +
135+
(if $overrideProjectName != "" then { "overrideProjectName": $overrideProjectName } else {} end) +
136+
(if $overrideRoleName != "" then { "overrideRoleName": $overrideRoleName } else {} end) +
137+
(if $targetProject != "" then { "targetProject": $targetProject } else {} end) +
138+
{
139+
"releaseVersion": $releaseVersion,
140+
"targetEnvironment": $targetEnvironment,
141+
"targetAccountGroup": $targetAccountGroup,
142+
"targetComponent": $targetComponent,
143+
"overrides": $overrides,
144+
}
145+
)
146+
}')
147+
148+
echo "[INFO] Triggering workflow '$targetWorkflow' in nhs-notify-internal..."
149+
150+
trigger_response=$(curl -s -L \
151+
--fail \
152+
-X POST \
153+
-H "Accept: application/vnd.github+json" \
154+
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
155+
-H "X-GitHub-Api-Version: 2022-11-28" \
156+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/$targetWorkflow/dispatches" \
157+
-d "$DISPATCH_EVENT" 2>&1)
158+
159+
if [[ $? -ne 0 ]]; then
160+
echo "[ERROR] Failed to trigger workflow. Response: $trigger_response"
161+
exit 1
162+
fi
163+
164+
echo "[INFO] Workflow trigger request sent successfully, waiting for completion..."
165+
166+
sleep 10 # Wait a few seconds before checking for the presence of the api to account for GitHub updating
167+
168+
# Poll GitHub API to check the workflow status
169+
workflow_run_url=""
170+
171+
for _ in {1..18}; do
172+
173+
response=$(curl -s -L \
174+
-H "Accept: application/vnd.github+json" \
175+
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
176+
-H "X-GitHub-Api-Version: 2022-11-28" \
177+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch")
178+
179+
if ! echo "$response" | jq empty 2>/dev/null; then
180+
echo "[ERROR] Invalid JSON response from GitHub API during workflow polling:"
181+
echo "$response"
182+
exit 1
183+
fi
184+
185+
workflow_run_url=$(echo "$response" | jq -r \
186+
--arg targetWorkflow "$targetWorkflow" \
187+
--arg targetEnvironment "$targetEnvironment" \
188+
--arg targetAccountGroup "$targetAccountGroup" \
189+
--arg targetComponent "$targetComponent" \
190+
--arg terraformAction "$terraformAction" \
191+
'.workflow_runs[]
192+
| select(.path == ".github/workflows/" + $targetWorkflow)
193+
| select(.name
194+
| contains($targetEnvironment)
195+
and contains($targetAccountGroup)
196+
and contains($targetComponent)
197+
and contains($terraformAction)
198+
)
199+
| .url')
200+
201+
if [[ -n "$workflow_run_url" && "$workflow_run_url" != null ]]; then
202+
# Workflow_run_url is a list of all workflows which were run for this combination of inputs, but are the API uri
203+
workflow_run_url=$(echo "$workflow_run_url" | head -n 1)
204+
205+
# Take the first and strip it back to being an accessible url
206+
# Example https://api.github.com/repos/MyOrg/my-repo/actions/runs/12346789 becomes
207+
# becomes https://github.com/MyOrg/my-repo/actions/runs/12346789
208+
workflow_run_ui_url=${workflow_run_url/api./} # Strips the api. prefix
209+
workflow_run_ui_url=${workflow_run_ui_url/\/repos/} # Strips the repos/ uri
210+
echo "[INFO] Found workflow run url: $workflow_run_ui_url"
211+
break
212+
fi
213+
214+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Waiting for workflow to start..."
215+
sleep 10
216+
done
217+
218+
if [[ -z "$workflow_run_url" || "$workflow_run_url" == null ]]; then
219+
echo "[ERROR] Failed to get the workflow run url. Exiting."
220+
exit 1
221+
fi
222+
223+
# Wait for workflow completion
224+
while true; do
225+
sleep 10
226+
response=$(curl -s -L \
227+
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
228+
-H "Accept: application/vnd.github+json" \
229+
"$workflow_run_url")
230+
231+
status=$(echo "$response" | jq -r '.status')
232+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Workflow status: $status"
233+
234+
if [ "$status" == "completed" ]; then
235+
conclusion=$(echo "$response" | jq -r '.conclusion')
236+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Workflow conclusion: $conclusion"
237+
238+
if [ -z "$conclusion" ] || [ "$conclusion" == "null" ]; then
239+
echo "[WARN] Workflow marked completed but conclusion not yet available, retrying..."
240+
sleep 5
241+
continue
242+
fi
243+
244+
if [ "$conclusion" == "success" ]; then
245+
echo "[SUCCESS] Workflow completed successfully!"
246+
exit 0
247+
else
248+
echo "[FAIL] Workflow failed with conclusion: $conclusion"
249+
exit 1
250+
fi
251+
fi
252+
done

.github/workflows/pr_closed.disabled

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
deploy-main:
3737
needs: check-merge-or-workflow-dispatch
3838
name: Deploy changes to main in dev AWS account
39+
runs-on: ubuntu-latest
3940
if: needs.check-merge-or-workflow-dispatch.outputs.deploy == 'true'
4041

4142
permissions:
@@ -47,12 +48,18 @@ jobs:
4748
matrix:
4849
component: [acct, app]
4950

50-
uses: ./.github/workflows/reusable_internal_repo_build.yaml
51-
secrets: inherit
52-
with:
53-
releaseVersion: main
54-
targetWorkflow: "dispatch-deploy-static-notify-bounded-context-env.yaml" ## Replace with correct targetWorkflow
55-
targetEnvironment: "main"
56-
targetAccountGroup: "nhs-notify-bounded-context-dev" ## Replace with correct targetAccountGroup
57-
targetComponent: ${{ matrix.component }}
58-
terraformAction: "apply"
51+
steps:
52+
- name: Checkout repository
53+
uses: actions/[email protected]
54+
55+
- name: Updating Main Environment
56+
env:
57+
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
58+
run: |
59+
bash .github/scripts/dispatch_internal_repo_workflow.sh \
60+
--releaseVersion "main" \
61+
--targetWorkflow "dispatch-deploy-static-notify-bounded-context-env.yaml" ## Replace with correct targetWorkflow \
62+
--targetEnvironment "main" \
63+
--targetAccountGroup "nhs-notify-bounded-context-dev" ## Replace with correct targetAccountGroup \
64+
--targetComponent "${{ matrix.component }}" \
65+
--terraformAction "apply"

.github/workflows/release_created.disabled

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ concurrency:
1313
jobs:
1414
deploy-main:
1515
name: Deploy changes to main in nonprod AWS Account
16+
runs-on: ubuntu-latest
1617

1718
permissions:
1819
id-token: write
@@ -23,12 +24,18 @@ jobs:
2324
matrix:
2425
component: [component1, component2] ## Replace with correct components
2526

26-
uses: ./.github/workflows/reusable_internal_repo_build.yaml
27-
secrets: inherit
28-
with:
29-
releaseVersion: ${{ github.event.release.tag_name }}
30-
targetWorkflow: "dispatch-deploy-static-notify-bounded-context-env.yaml" ## Replace with correct targetWorkflow
31-
targetEnvironment: "main"
32-
targetAccountGroup: "nhs-notify-bounded-context-nonprod" ## Replace with correct targetAccountGroup
33-
targetComponent: ${{ matrix.component }}
34-
terraformAction: "apply"
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/[email protected]
30+
31+
- name: Updating Main Environment
32+
env:
33+
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
34+
run: |
35+
bash .github/scripts/dispatch_internal_repo_workflow.sh \
36+
--releaseVersion "${{ github.event.release.tag_name }}" \
37+
--targetWorkflow "dispatch-deploy-static-notify-bounded-context-env.yaml" ## Replace with correct targetWorkflow \
38+
--targetEnvironment "main" \
39+
--targetAccountGroup "nhs-notify-bounded-context-nonprod" ## Replace with correct targetAccountGroup \
40+
--targetComponent "${{ matrix.component }}" \
41+
--terraformAction "apply"

0 commit comments

Comments
 (0)