Skip to content

Commit 2c410d6

Browse files
CCM-11942 Fixing cross repo workflows
1 parent 3bcd228 commit 2c410d6

File tree

8 files changed

+302
-298
lines changed

8 files changed

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

.github/workflows/cicd-1-pull-request.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ jobs:
5454
run: |
5555
branch_name=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
5656
echo "Current branch is '$branch_name'"
57-
if gh pr list --head $branch_name | grep -q .; then
58-
echo "Pull request exists"
57+
58+
pr_json=$(gh pr list --head "$branch_name" --state open --json number --limit 1)
59+
pr_number=$(echo "$pr_json" | jq -r '.[0].number // empty')
60+
61+
if [[ -n "$pr_number" ]]; then
62+
echo "Pull request exists: #$pr_number"
5963
echo "does_pull_request_exist=true" >> $GITHUB_OUTPUT
60-
PR_NUMBER=$(gh pr list --head "$branch_name" --state open --json number -q '.[0].number')
61-
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
64+
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
6265
else
6366
echo "Pull request doesn't exist"
6467
echo "does_pull_request_exist=false" >> $GITHUB_OUTPUT
68+
echo "pr_number=" >> $GITHUB_OUTPUT
6569
fi
6670
- name: "List variables"
6771
run: |
@@ -75,7 +79,6 @@ jobs:
7579
export VERSION="${{ steps.variables.outputs.version }}"
7680
export DOES_PULL_REQUEST_EXIST="${{ steps.pr_exists.outputs.does_pull_request_exist }}"
7781
export IS_VERSION_PRERELEASE="${{ steps.variables.outputs.is_version_prerelease }}"
78-
export PR_NUMBER="${{ steps.pr_exists.outputs.pr_number }}"
7982
make list-variables
8083
commit-stage: # Recommended maximum execution time is 2 minutes
8184
name: "Commit stage"
@@ -131,6 +134,7 @@ jobs:
131134
python_version: "${{ needs.metadata.outputs.python_version }}"
132135
terraform_version: "${{ needs.metadata.outputs.terraform_version }}"
133136
version: "${{ needs.metadata.outputs.version }}"
137+
pr_number: ${{ needs.metadata.outputs.pr_number }}
134138
secrets: inherit
135139
publish-stage: # Recommended maximum execution time is 10 minutes
136140
name: "Publish stage"

.github/workflows/pr_closed.yaml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
deploy-main:
3535
needs: check-merge-or-workflow-dispatch
3636
name: Deploy changes to main in dev AWS account
37+
runs-on: ubuntu-latest
3738
if: needs.check-merge-or-workflow-dispatch.outputs.deploy == 'true'
3839

3940
permissions:
@@ -45,12 +46,18 @@ jobs:
4546
matrix:
4647
component: [api]
4748

48-
uses: ./.github/workflows/reusable_internal_repo_build.yaml
49-
secrets: inherit
50-
with:
51-
releaseVersion: main
52-
targetWorkflow: "dispatch-deploy-static-notify-supplier-api-env.yaml"
53-
targetEnvironment: "main"
54-
targetAccountGroup: "nhs-notify-supplier-api-dev"
55-
targetComponent: ${{ matrix.component }}
56-
terraformAction: "apply"
49+
steps:
50+
- name: Checkout repository
51+
uses: actions/checkout@v4
52+
53+
- name: Updating Main Environment
54+
env:
55+
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
56+
run: |
57+
bash .github/scripts/dispatch_internal_repo_workflow.sh \
58+
--releaseVersion "main" \
59+
--targetWorkflow "dispatch-deploy-static-notify-supplier-api-env.yaml " \
60+
--targetEnvironment "main" \
61+
--targetAccountGroup "nhs-notify-supplier-api-dev" \
62+
--targetComponent "${{ matrix.component }}" \
63+
--terraformAction "apply"

0 commit comments

Comments
 (0)