Skip to content

Commit 4da65f7

Browse files
Add reusable and thin wrappers
1 parent 4388af2 commit 4da65f7

5 files changed

Lines changed: 378 additions & 218 deletions

File tree

.github/workflows/cicd-3-deploy.yaml

Lines changed: 41 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ name: "2. CD - Deploy"
33
on:
44
workflow_dispatch:
55
inputs:
6+
source_type:
7+
description: "Deployment source type"
8+
type: choice
9+
required: true
10+
default: release
11+
options:
12+
- release
13+
- branch
14+
source_value:
15+
description: "Release tag or branch name"
16+
type: string
17+
required: true
18+
deploy_backend:
19+
description: "Deploy backend infrastructure"
20+
type: boolean
21+
required: false
22+
default: true
623
backend_account_group:
724
description: "Target backend account group"
825
type: choice
@@ -12,11 +29,11 @@ on:
1229
- dev
1330
- nonprod
1431
- prod
15-
backend_environment:
16-
description: "Target backend environment"
17-
type: string
18-
required: true
19-
default: main
32+
deploy_proxy:
33+
description: "Deploy APIM proxy"
34+
type: boolean
35+
required: false
36+
default: true
2037
apim_environment:
2138
description: "Target APIM environment"
2239
type: choice
@@ -26,203 +43,31 @@ on:
2643
- internal-dev
2744
- int
2845
- prod
29-
source_type:
30-
description: "Deployment source type"
31-
type: choice
32-
required: true
33-
default: release
34-
options:
35-
- release
36-
- branch
37-
- pr
38-
source_value:
39-
description: "Release tag, branch name, or PR number"
40-
type: string
41-
required: true
46+
build_sandbox:
47+
description: "Build sandbox container"
48+
type: boolean
49+
required: false
50+
default: false
4251

4352
run-name: >-
44-
Deploy backend=${{ inputs.backend_account_group }}/${{ inputs.backend_environment }}
45-
apim=${{ inputs.apim_environment }}
46-
source=${{ inputs.source_type }}:${{ inputs.source_value }} by @${{ github.actor }}
53+
Deploy backend=${{ inputs.backend_account_group }} apim=${{
54+
inputs.apim_environment }} source=${{ inputs.source_type }}:${{
55+
inputs.source_value }} by @${{ github.actor }}
4756
4857
permissions:
4958
id-token: write
5059
contents: read
5160
packages: read
5261

5362
jobs:
54-
validate:
55-
name: Validate deployment request
56-
runs-on: ubuntu-latest
57-
timeout-minutes: 5
58-
outputs:
59-
release_version: ${{ steps.validate.outputs.release_version }}
60-
is_release: ${{ steps.validate.outputs.is_release }}
61-
build_artifact_version: ${{ steps.validate.outputs.build_artifact_version }}
62-
target_account_group: ${{ steps.validate.outputs.target_account_group }}
63-
target_environment: ${{ steps.validate.outputs.target_environment }}
64-
apim_environment: ${{ steps.validate.outputs.apim_environment }}
65-
steps:
66-
- name: Validate inputs and resolve source
67-
id: validate
68-
shell: bash
69-
env:
70-
GH_TOKEN: ${{ github.token }}
71-
run: |
72-
set -euo pipefail
73-
74-
backend_account_group="${{ inputs.backend_account_group }}"
75-
backend_environment="${{ inputs.backend_environment }}"
76-
apim_environment="${{ inputs.apim_environment }}"
77-
source_type="${{ inputs.source_type }}"
78-
source_value="${{ inputs.source_value }}"
79-
80-
if [[ -z "$source_value" ]]; then
81-
echo "[ERROR] source_value cannot be empty."
82-
exit 1
83-
fi
84-
85-
if [[ "$backend_account_group" == "prod" && "$apim_environment" != "prod" ]]; then
86-
echo "[ERROR] PROD backend and PROD APIM can only be deployed together."
87-
exit 1
88-
fi
89-
90-
if [[ "$apim_environment" == "prod" && "$backend_account_group" != "prod" ]]; then
91-
echo "[ERROR] PROD backend and PROD APIM can only be deployed together."
92-
exit 1
93-
fi
94-
95-
is_release="false"
96-
release_version="$source_value"
97-
98-
if [[ "$source_type" == "release" ]]; then
99-
if [[ ! "$source_value" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+([-.+][0-9A-Za-z.-]+)?$ ]]; then
100-
echo "[ERROR] Release tags must be semantic versions, for example v1.2.3."
101-
exit 1
102-
fi
103-
104-
gh release view "$source_value" --repo "$GITHUB_REPOSITORY" >/dev/null
105-
106-
oas_asset="api-oas-specification-${apim_environment}-${source_value}.zip"
107-
gh release view "$source_value" --repo "$GITHUB_REPOSITORY" --json assets \
108-
--jq '.assets[].name' | grep -x "$oas_asset" >/dev/null
109-
110-
is_release="true"
111-
elif [[ "$source_type" == "branch" ]]; then
112-
if [[ "$backend_account_group" != "dev" ]]; then
113-
echo "[ERROR] Branch deployments are only allowed for dev backend deployments."
114-
exit 1
115-
fi
116-
117-
branch_matches=$(gh api "repos/${GITHUB_REPOSITORY}/git/matching-refs/heads/${source_value}" --jq 'length')
118-
if [[ "$branch_matches" -eq 0 ]]; then
119-
echo "[ERROR] Branch '$source_value' not found in repository."
120-
exit 1
121-
fi
122-
elif [[ "$source_type" == "pr" ]]; then
123-
if [[ "$backend_account_group" != "dev" ]]; then
124-
echo "[ERROR] PR deployments are only allowed for dev backend deployments."
125-
exit 1
126-
fi
127-
128-
if [[ ! "$source_value" =~ ^[0-9]+$ ]]; then
129-
echo "[ERROR] PR source_value must be a numeric PR number."
130-
exit 1
131-
fi
132-
133-
release_version=$(gh pr view "$source_value" --repo "$GITHUB_REPOSITORY" --json headRefName --jq '.headRefName')
134-
if [[ -z "$release_version" || "$release_version" == "null" ]]; then
135-
echo "[ERROR] PR #$source_value was not found."
136-
exit 1
137-
fi
138-
else
139-
echo "[ERROR] Unsupported source type '$source_type'."
140-
exit 1
141-
fi
142-
143-
if [[ "$backend_account_group" == "nonprod" || "$backend_account_group" == "prod" ]]; then
144-
if [[ "$is_release" != "true" ]]; then
145-
echo "[ERROR] Only tagged releases can be deployed to NONPROD and PROD backends."
146-
exit 1
147-
fi
148-
fi
149-
150-
case "$backend_account_group" in
151-
dev)
152-
target_account_group="nhs-notify-supplier-api-dev"
153-
;;
154-
nonprod)
155-
target_account_group="nhs-notify-supplier-api-nonprod"
156-
;;
157-
prod)
158-
target_account_group="nhs-notify-supplier-api-prod"
159-
;;
160-
*)
161-
echo "[ERROR] Unsupported backend account group '$backend_account_group'."
162-
exit 1
163-
;;
164-
esac
165-
166-
build_artifact_version=""
167-
if [[ "$is_release" != "true" ]]; then
168-
build_artifact_version="manual"
169-
fi
170-
171-
echo "release_version=$release_version" >> "$GITHUB_OUTPUT"
172-
echo "is_release=$is_release" >> "$GITHUB_OUTPUT"
173-
echo "build_artifact_version=$build_artifact_version" >> "$GITHUB_OUTPUT"
174-
echo "target_account_group=$target_account_group" >> "$GITHUB_OUTPUT"
175-
echo "target_environment=$backend_environment" >> "$GITHUB_OUTPUT"
176-
echo "apim_environment=$apim_environment" >> "$GITHUB_OUTPUT"
177-
178-
deploy-backend:
179-
name: Deploy backend
180-
runs-on: ubuntu-latest
181-
timeout-minutes: 30
182-
needs: validate
183-
steps:
184-
- name: Checkout repository
185-
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
186-
187-
- name: Deploy backend environment
188-
env:
189-
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
190-
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
191-
run: |
192-
bash .github/scripts/dispatch_internal_repo_workflow.sh \
193-
--releaseVersion "${{ needs.validate.outputs.release_version }}" \
194-
--targetWorkflow "dispatch-deploy-static-notify-supplier-api-env.yaml" \
195-
--targetEnvironment "${{ needs.validate.outputs.target_environment }}" \
196-
--targetAccountGroup "${{ needs.validate.outputs.target_account_group }}" \
197-
--targetComponent "api" \
198-
--terraformAction "apply"
199-
200-
deploy-proxy:
201-
name: Deploy proxy
202-
runs-on: ubuntu-latest
203-
timeout-minutes: 30
204-
needs: [validate, deploy-backend]
205-
steps:
206-
- name: Build OAS spec for non-release source
207-
if: ${{ needs.validate.outputs.is_release != 'true' }}
208-
uses: ./.github/actions/build-oas-spec
209-
with:
210-
version: ${{ needs.validate.outputs.build_artifact_version }}
211-
apimEnv: ${{ needs.validate.outputs.apim_environment }}
212-
nodejs_version: "22.22.0"
213-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
214-
215-
- name: Deploy proxy
216-
env:
217-
PROXYGEN_API_NAME: nhs-notify-supplier
218-
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
219-
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
220-
uses: ./.github/actions/build-proxies
221-
with:
222-
targetComponent: api
223-
environment: ${{ needs.validate.outputs.target_environment }}
224-
apimEnv: ${{ needs.validate.outputs.apim_environment }}
225-
runId: "${{ github.run_id }}"
226-
releaseVersion: ${{ needs.validate.outputs.release_version }}
227-
isRelease: ${{ needs.validate.outputs.is_release }}
228-
version: ${{ needs.validate.outputs.build_artifact_version }}
63+
deploy:
64+
uses: ./.github/workflows/deploy-supplier-api.yaml
65+
secrets: inherit
66+
with:
67+
backend_account_group: ${{ inputs.backend_account_group }}
68+
apim_environment: ${{ inputs.apim_environment }}
69+
source_type: ${{ inputs.source_type }}
70+
source_value: ${{ inputs.source_value }}
71+
deploy_backend: ${{ inputs.deploy_backend }}
72+
deploy_proxy: ${{ inputs.deploy_proxy }}
73+
build_sandbox: ${{ inputs.build_sandbox }}

.github/workflows/manual-proxy-environment-deploy.yaml renamed to .github/workflows/deploy-dynamic-env-proxy.yaml

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
name: Deploy proxy to environment
2-
run-name: Proxygen Deployment for ${{ inputs.proxy_environment }}
2+
run-name: Proxygen Deployment for internal-dev
33

44
on:
55
workflow_dispatch:
66
inputs:
7-
proxy_environment:
8-
description: Name of the proxygen environment to deploy to
9-
required: true
10-
type: choice
11-
default: internal-dev
12-
options:
13-
- internal-dev
14-
- int
15-
- prod
167
build_sandbox:
178
description: Build sandbox container?
189
required: false
@@ -40,19 +31,16 @@ jobs:
4031
node-version: 22
4132
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4233

43-
- name: "Check if pull request exists for this branch and set ENVIRONMENT/APIM_ENV"
34+
- name: "Check if pull request exists for this branch and set
35+
ENVIRONMENT/APIM_ENV"
4436
id: pr_exists
4537
env:
4638
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4739
run: |
4840
branch_name=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
4941
echo "Current branch is '$branch_name'"
5042
51-
if [ -z "${{ inputs.proxy_environment }}" ]; then
52-
ENVIRONMENT="internal-dev"
53-
else
54-
ENVIRONMENT="${{ inputs.proxy_environment }}"
55-
fi
43+
ENVIRONMENT="internal-dev"
5644
5745
pr_json=$(gh pr list --head "$branch_name" --state open --json number --limit 1)
5846
pr_number=$(echo "$pr_json" | jq -r '.[0].number // empty')
@@ -62,14 +50,11 @@ jobs:
6250
echo "does_pull_request_exist=true" >> $GITHUB_OUTPUT
6351
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
6452
APIM_ENV="$ENVIRONMENT-pr"
65-
echo "changing environment variable so that PR number is used in proxy pipeline for setting env vars"
53+
# changing environment variable so that PR number is used in proxy pipeline for setting env vars
6654
ENVIRONMENT="pr$pr_number"
6755
else
68-
echo "Pull request doesn't exist, setting target env to main"
69-
echo "does_pull_request_exist=false" >> $GITHUB_OUTPUT
70-
echo "pr_number=" >> $GITHUB_OUTPUT
71-
APIM_ENV="$ENVIRONMENT"
72-
ENVIRONMENT="main"
56+
echo "[ERROR] Pull request $pr_number doesn't exist."
57+
exit 1
7358
fi
7459
7560
echo "ENVIRONMENT=$ENVIRONMENT" >> $GITHUB_ENV
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Deploy backend only
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
backend_account_group:
7+
description: "Target backend account group"
8+
type: choice
9+
required: true
10+
default: dev
11+
options:
12+
- dev
13+
- nonprod
14+
- prod
15+
source_type:
16+
description: "Deployment source type"
17+
type: choice
18+
required: true
19+
default: release
20+
options:
21+
- release
22+
- branch
23+
source_value:
24+
description: "Release tag or branch name"
25+
type: string
26+
required: true
27+
28+
run-name: >-
29+
Deploy backend=${{ inputs.backend_account_group }} source=${{
30+
inputs.source_type }}:${{ inputs.source_value }} by @${{ github.actor }}
31+
32+
permissions:
33+
id-token: write
34+
contents: read
35+
packages: read
36+
37+
jobs:
38+
deploy:
39+
uses: ./.github/workflows/deploy-supplier-api.yaml
40+
secrets: inherit
41+
with:
42+
backend_account_group: ${{ inputs.backend_account_group }}
43+
source_type: ${{ inputs.source_type }}
44+
source_value: ${{ inputs.source_value }}
45+
deploy_backend: true
46+
deploy_proxy: false

0 commit comments

Comments
 (0)