Skip to content

Commit 44fb2bc

Browse files
CCM-11067: Add Internal Workflows
1 parent a095170 commit 44fb2bc

File tree

5 files changed

+336
-0
lines changed

5 files changed

+336
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## This workflow is DISABLED.
2+
## To enable, rename from .disabled to .yaml and replace any references as per the comments.
3+
name: PR Closed
4+
5+
on:
6+
workflow_dispatch:
7+
pull_request:
8+
types: [closed]
9+
branches:
10+
- main
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: false
15+
16+
jobs:
17+
check-merge-or-workflow-dispatch:
18+
runs-on: ubuntu-latest
19+
outputs:
20+
deploy: ${{ steps.check.outputs.deploy }}
21+
steps:
22+
- name: Check if PR was merged or workflow is triggered by workflow_dispatch
23+
id: check
24+
run: |
25+
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
26+
echo "deploy=true" >> $GITHUB_OUTPUT
27+
echo "Job triggered by workflow_dispatch - running 'deploy-main'"
28+
elif [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.pull_request.merged }}" == "true" ]]; then
29+
echo "deploy=true" >> $GITHUB_OUTPUT
30+
echo "Job triggered by Merged PR - running 'deploy-main'"
31+
else
32+
echo "deploy=false" >> $GITHUB_OUTPUT
33+
echo "Job not triggered by workflow_dispatch or Merged PR - Skipping 'deploy-main'"
34+
fi
35+
36+
deploy-main:
37+
needs: check-merge-or-workflow-dispatch
38+
name: Deploy changes to main in dev AWS account
39+
if: needs.check-merge-or-workflow-dispatch.outputs.deploy == 'true'
40+
41+
permissions:
42+
id-token: write
43+
contents: read
44+
45+
strategy:
46+
max-parallel: 1
47+
matrix:
48+
component: [acct, app]
49+
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"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## This workflow is DISABLED.
2+
## To enable, rename from .disabled to .yaml and replace any references as per the comments.
3+
name: PR Create Environment
4+
5+
on:
6+
pull_request:
7+
types: [labeled, opened, synchronize, reopened, unlabeled, edited]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: false
12+
13+
jobs:
14+
create-dynamic-environment:
15+
name: Create Dynamic Environment
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Trigger nhs-notify-internal dynamic environment workflow
20+
shell: bash
21+
run: |
22+
set -x
23+
this_repo_name=$(echo ${{ github.repository }} | cut -d'/' -f2)
24+
25+
DISPATCH_EVENT=$(jq -ncM \
26+
--arg infraRepoName "${this_repo_name}" \
27+
--arg releaseVersion "${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" \
28+
--arg targetEnvironment "pr${{ github.event.number }}" \
29+
--arg targetAccountGroup "nhs-notify-bounded-context-dev" \ ## Replace with correct targetAccountGroup
30+
--arg targetComponent "component" \ ## Replace with correct targetComponent
31+
--arg terraformAction "apply" \
32+
--arg overrides "branch_name=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" \
33+
'{ "ref": "main",
34+
"inputs": {
35+
"infraRepoName": $infraRepoName,
36+
"releaseVersion", $releaseVersion,
37+
"targetEnvironment", $targetEnvironment,
38+
"targetAccountGroup", $targetAccountGroup,
39+
"targetComponent", $targetComponent,
40+
"terraformAction", $terraformAction,
41+
"overrides", $overrides,
42+
}
43+
}')
44+
45+
curl --fail -L \
46+
-X POST \
47+
-H "Accept: application/vnd.github+json" \
48+
-H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \
49+
-H "X-GitHub-Api-Version: 2022-11-28" \
50+
https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/dispatch-deploy-dynamic-env.yaml/dispatches \
51+
-d "${DISPATCH_EVENT}"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## This workflow is DISABLED.
2+
## To enable, rename from .disabled to .yaml and replace any references as per the comments.
3+
name: PR Destroy Environment
4+
5+
on:
6+
pull_request:
7+
types: [closed]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: false
12+
13+
jobs:
14+
create-dynamic-environment:
15+
name: Destroy Dynamic Environment
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Trigger nhs-notify-internal dynamic environment destruction
20+
shell: bash
21+
run: |
22+
set -x
23+
this_repo_name=$(echo ${{ github.repository }} | cut -d'/' -f2)
24+
25+
DISPATCH_EVENT=$(jq -ncM \
26+
--arg infraRepoName "${this_repo_name}" \
27+
--arg releaseVersion "main" \
28+
--arg targetEnvironment "pr${{ github.event.number }}" \
29+
--arg targetAccountGroup "nhs-notify-template-management-dev" \ ## Replace with correct targetAccountGroup
30+
--arg targetComponent "component" \ ## Replace with correct targetComponent
31+
--arg terraformAction "destroy" \
32+
'{ "ref": "main",
33+
"inputs": {
34+
"infraRepoName": $infraRepoName,
35+
"releaseVersion", $releaseVersion,
36+
"targetEnvironment", $targetEnvironment,
37+
"targetAccountGroup", $targetAccountGroup,
38+
"targetComponent", $targetComponent,
39+
"terraformAction", $terraformAction,
40+
}
41+
}')
42+
43+
curl --fail -L \
44+
-X POST \
45+
-H "Accept: application/vnd.github+json" \
46+
-H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \
47+
-H "X-GitHub-Api-Version: 2022-11-28" \
48+
https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/dispatch-deploy-dynamic-env.yaml/dispatches \
49+
-d "${DISPATCH_EVENT}"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## This workflow is DISABLED.
2+
## To enable, rename from .disabled to .yaml and replace any references as per the comments.
3+
name: Github Release Created
4+
5+
on:
6+
release:
7+
types: ["published"] # Inherits all input defaults
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: false
12+
13+
jobs:
14+
deploy-main:
15+
name: Deploy changes to main in nonprod AWS Account
16+
17+
permissions:
18+
id-token: write
19+
contents: read
20+
21+
strategy:
22+
max-parallel: 1
23+
matrix:
24+
component: [component1, component2] ## Replace with correct components
25+
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"
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
## This workflow is DISABLED.
2+
## To enable, rename from .disabled to .yaml and replace any references as per the comments.
3+
name: Call Notify Internal Infrastructure Deployment
4+
## Sub workflow which plans and deploys Notify components as part of the workflow.
5+
## Review Gates may be required to proceed on triggered builds.
6+
7+
on:
8+
workflow_call:
9+
inputs:
10+
releaseVersion:
11+
type: string
12+
description: The Github release version, commit, or tag.
13+
default: main
14+
targetWorkflow:
15+
type: string
16+
description: The name of the github workflow to call.
17+
default: main
18+
targetEnvironment:
19+
type: string
20+
description: The Terraform environment to deploy
21+
default: main
22+
targetComponent:
23+
type: string
24+
description: The Terraform component to deploy
25+
required: true
26+
targetAccountGroup:
27+
type: string
28+
description: The Terraform group to deploy
29+
required: true
30+
terraformAction:
31+
type: string
32+
description: The Terraform component to deploy
33+
default: plan
34+
35+
concurrency:
36+
group: ${{ inputs.targetEnvironment }}-${{ inputs.targetAccountGroup }}-${{ inputs.targetComponent }}-${{ inputs.terraformAction }}
37+
38+
jobs:
39+
trigger:
40+
runs-on: ubuntu-latest
41+
42+
permissions:
43+
id-token: write
44+
contents: read
45+
46+
steps:
47+
- uses: actions/checkout@v4
48+
49+
- name: Trigger nhs-notify-internal static environment workflow deployment
50+
shell: bash
51+
run: |
52+
set -x
53+
54+
DISPATCH_EVENT=$(jq -ncM \
55+
--arg releaseVersion ${{ inputs.releaseVersion }} \
56+
--arg targetEnvironment ${{ inputs.targetEnvironment }} \
57+
--arg targetAccountGroup ${{ inputs.targetAccountGroup }} \
58+
--arg targetComponent ${{ inputs.targetComponent }} \
59+
--arg terraformAction ${{ inputs.terraformAction }} \
60+
'{ "ref": "main",
61+
"inputs": {
62+
"releaseVersion", $releaseVersion,
63+
"targetEnvironment", $targetEnvironment,
64+
"targetAccountGroup", $targetAccountGroup,
65+
"targetComponent", $targetComponent,
66+
"terraformAction", $terraformAction
67+
}
68+
}')
69+
70+
# Trigger The workflow
71+
curl -L \
72+
--fail \
73+
--silent \
74+
-X POST \
75+
-H "Accept: application/vnd.github+json" \
76+
-H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \
77+
-H "X-GitHub-Api-Version: 2022-11-28" \
78+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/${{ inputs.targetWorkflow }}/dispatches" \
79+
-d "${DISPATCH_EVENT}"
80+
81+
echo "Workflow triggered successfully. HTTP response. Waiting for the workflow to complete.."
82+
83+
# Poll GitHub API to check the workflow status
84+
run_id=""
85+
for i in {1..12}; do
86+
in_progress=$(curl -s \
87+
-H "Accept: application/vnd.github+json" \
88+
-H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \
89+
-H "X-GitHub-Api-Version: 2022-11-28" \
90+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch&status=in_progress")
91+
92+
run_id=$(echo "$in_progress" | jq -r \
93+
--arg env "${{ inputs.targetEnvironment }}" \
94+
--arg component "${{ inputs.targetComponent }}" \
95+
--arg group "${{ inputs.targetAccountGroup }}" \
96+
--arg releaseVersion "${{ inputs.releaseVersion }}" \
97+
'.workflow_runs[]
98+
| select(.name | contains($env) and contains($component) and contains($group) and contains($releaseVersion))
99+
| .id' | head -n 1)
100+
101+
if [[ -n "$run_id" && "$run_id" != null ]]; then
102+
echo "Found workflow run with ID: $run_id"
103+
break
104+
fi
105+
106+
echo "Waiting for workflow to start..."
107+
sleep 10
108+
done
109+
110+
if [[ -z "$run_id" || "$run_id" == null ]]; then
111+
echo "Failed to get the workflow run ID. Exiting."
112+
exit 1
113+
fi
114+
115+
# Wait for workflow completion
116+
while true; do
117+
sleep 10
118+
status=$(curl -s \
119+
-H "Accept: application/vnd.github+json" \
120+
-H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \
121+
-H "X-GitHub-Api-Version: 2022-11-28" \
122+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs/$run_id" \
123+
| jq -r '.status')
124+
125+
conclusion=$(curl -s \
126+
-H "Accept: application/vnd.github+json" \
127+
-H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \
128+
-H "X-GitHub-Api-Version: 2022-11-28" \
129+
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs/$run_id" \
130+
| jq -r '.conclusion')
131+
132+
if [ "$status" == "completed" ]; then
133+
if [ "$conclusion" == "success" ]; then
134+
echo "Workflow completed successfully."
135+
exit 0
136+
else
137+
echo "Workflow failed with conclusion: $conclusion"
138+
exit 1
139+
fi
140+
fi
141+
142+
echo "Workflow still running..."
143+
sleep 20
144+
done

0 commit comments

Comments
 (0)