Skip to content

Commit 2c56821

Browse files
committed
[NRL-793] Add version checks before updating perms
1 parent 50353af commit 2c56821

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

.github/workflows/persistent-environment.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ jobs:
234234

235235
- name: Update environment config version
236236
run: |
237-
short_commit_ref="$(echo ${{ github.sha }} | cut -c1-8)"
238-
deployed_version="${{ inputs.branch_name }}@${short_commit_ref}"
237+
deployed_version=$(terraform -chdir=terraform/infrastructure output --raw version)
239238
poetry run python ./scripts/set_env_config.py inactive-version ${deployed_version} ${{ inputs.environment }}
240239
241240
- name: Smoke Test

.github/workflows/update-lambda-permissions.yml

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,67 @@ permissions:
2525
actions: write
2626

2727
jobs:
28+
check-versions:
29+
name: Check versions
30+
runs-on: [self-hosted, ci]
31+
32+
steps:
33+
- name: Git clone - ${{ github.ref }}
34+
uses: actions/checkout@v4
35+
with:
36+
ref: ${{ github.ref }}
37+
38+
- name: Setup asdf cache
39+
uses: actions/cache@v4
40+
with:
41+
path: ~/.asdf
42+
key: ${{ runner.os }}-asdf-${{ hashFiles('**/.tool-versions') }}
43+
restore-keys: |
44+
${{ runner.os }}-asdf-
45+
46+
- name: Install asdf
47+
uses: asdf-vm/actions/[email protected]
48+
49+
- name: Install zip
50+
run: sudo apt-get install zip
51+
52+
- name: Setup Python environment
53+
run: |
54+
poetry install --no-root
55+
source $(poetry env info --path)/bin/activate
56+
57+
- name: Configure Management Credentials
58+
uses: aws-actions/configure-aws-credentials@v4
59+
with:
60+
aws-region: eu-west-2
61+
role-to-assume: ${{ secrets.MGMT_ROLE_ARN }}
62+
role-session-name: github-actions-ci-${{ inputs.environment }}-${{ github.run_id }}
63+
64+
- name: Terraform Init
65+
run: |
66+
terraform -chdir=terraform/infrastructure init
67+
terraform -chdir=terraform/infrastructure workspace new ${{ inputs.stack_name }} || \
68+
terraform -chdir=terraform/infrastructure workspace select ${{ inputs.stack_name }}
69+
70+
- name: Check deployed version matches build version
71+
run: |
72+
this_version="$(./scripts/get-current-info.sh) | jq -r .version)"
73+
deployed_version="$(terraform -chdir=terraform/infrastructure output --raw version)"
74+
75+
if [ "${deployed_version}" != "${this_version}" ]; then
76+
echo "Deployed version is ${deployed_version}, not ${this_version}"
77+
exit 1
78+
fi
79+
80+
echo "Deployed version matches this version: ${deployed_version}"
81+
2882
build-permissions:
2983
name: Build permissions for ${{ inputs.environment }}
3084
runs-on: [self-hosted, ci]
3185
environment: ${{ inputs.environment }}
3286

87+
needs: [check-versions]
88+
3389
steps:
3490
- name: Git clone - ${{ github.ref }}
3591
uses: actions/checkout@v4
@@ -74,10 +130,12 @@ jobs:
74130
path: dist/nrlf_permissions.zip
75131

76132
pull-deployed-lambdas:
77-
name: Pull deployed lambdas for ${{ inputs.environment }}
133+
name: Pull deployed lambdas from ${{ inputs.stack_name }}
78134
runs-on: [self-hosted, ci]
79135
environment: ${{ inputs.environment }}
80136

137+
needs: [check-versions]
138+
81139
steps:
82140
- name: Git clone - ${{ github.ref }}
83141
uses: actions/checkout@v4
@@ -111,7 +169,7 @@ jobs:
111169
path: dist/*.zip
112170

113171
terraform-plan:
114-
name: Plan changes to ${{ inputs.environment }}
172+
name: Plan changes to ${{ inputs.stack_name }} for ${{ inputs.environment }}
115173
runs-on: [self-hosted, ci]
116174
environment: ${{ inputs.environment }}
117175

@@ -165,7 +223,7 @@ jobs:
165223
aws s3 cp terraform/infrastructure/tfplan.txt s3://nhsd-nrlf--mgmt--github-ci-logging/${{ inputs.environment }}/${{ github.run_id }}/tfplan.txt
166224
167225
terraform-apply:
168-
name: Apply permissions to ${{ inputs.environment }}
226+
name: Apply permissions to ${{ inputs.stack_name }} for ${{ inputs.environment }}
169227
runs-on: [self-hosted, ci]
170228
environment: ${{ inputs.environment }}
171229

scripts/get_current_info.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
# Get the current info about the codebase
3+
set -o errexit -o nounset -o pipefail
4+
5+
BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)"
6+
SHORT_COMMIT_HASH="$(git rev-parse --short=8 HEAD)"
7+
8+
echo "{ \"version\": \"${BRANCH_NAME}@${SHORT_COMMIT_HASH}\" }"

terraform/infrastructure/data.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ data "aws_iam_policy" "pointers-kms-read-write" {
3434
count = var.use_shared_resources ? 1 : 0
3535
name = "${local.shared_prefix}-pointers-kms-read-write"
3636
}
37+
38+
data "external" "current-info" {
39+
program = [
40+
"bash",
41+
"../../scripts/get_current_info.sh",
42+
]
43+
}

terraform/infrastructure/output.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ output "certificate_domain_name" {
4646
output "auth_store" {
4747
value = local.auth_store_id
4848
}
49+
50+
output "version" {
51+
value = data.external.current-info.result.version
52+
}

0 commit comments

Comments
 (0)