Skip to content

Commit a96ae2d

Browse files
committed
Enhance deployment workflow with Terraform planning step
- Introduced a new `terraform-plan` job in the deployment workflow to manage infrastructure changes before applying them. - Added steps for AWS connection, Terraform initialization, and planning, ensuring a structured approach to infrastructure management. - Updated the `build-and-push-recordprocessor` job to depend on the `terraform-apply` job, streamlining the deployment process. - Removed redundant image tag output handling, simplifying the workflow logic.
1 parent e0538c4 commit a96ae2d

1 file changed

Lines changed: 44 additions & 58 deletions

File tree

.github/workflows/deploy-backend.yml

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,54 @@ env: # Sonarcloud - do not allow direct usage of untrusted data
5151
run-name: Deploy Backend - ${{ inputs.environment }} ${{ inputs.sub_environment }}
5252

5353
jobs:
54+
terraform-plan:
55+
permissions:
56+
id-token: write
57+
contents: read
58+
runs-on: ubuntu-latest
59+
env:
60+
TF_VAR_recordprocessor_image_tag: ${{ github.sha }}
61+
environment:
62+
name: ${{ inputs.environment }}
63+
steps:
64+
- name: Checkout
65+
uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98
66+
67+
- name: Connect to AWS
68+
uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7
69+
with:
70+
aws-region: eu-west-2
71+
role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops
72+
role-session-name: github-actions
73+
74+
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85
75+
with:
76+
terraform_version: "1.12.2"
77+
78+
- name: Terraform Init
79+
working-directory: infrastructure/instance
80+
run: make init
81+
82+
- name: Terraform Plan
83+
# Ignore cancellations to prevent Terraform from being killed while it holds a state lock
84+
# A stuck process can still be killed with the force-cancel API operation
85+
if: ${{ !failure() }}
86+
working-directory: infrastructure/instance
87+
run: make plan-ci
88+
89+
- name: Save Terraform Plan
90+
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
91+
with:
92+
name: ${{ env.ENVIRONMENT }}-${{ env.SUB_ENVIRONMENT }}-tfplan
93+
path: infrastructure/instance/tfplan
94+
5495
build-and-push-recordprocessor:
5596
permissions:
5697
id-token: write
5798
contents: read
5899
name: Build and push recordprocessor image
100+
needs: terraform-apply
59101
runs-on: ubuntu-latest
60-
outputs:
61-
image_tag: ${{ steps.build-and-push.outputs.image_tag }}
62102

63103
environment:
64104
name: ${{ inputs.environment }}
@@ -83,20 +123,12 @@ jobs:
83123
uses: aws-actions/amazon-ecr-login@062b18b96a7aff071d4dc91bc00c4c1a7945b076
84124

85125
- name: Build and push Docker image
86-
id: build-and-push
87126
env:
88127
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
89-
SUB_ENVIRONMENT: ${{ env.SUB_ENVIRONMENT }}
90128
working-directory: lambdas
91129
run: |
92130
IMAGE_TAG="${GITHUB_SHA}"
93131
REPOSITORY_NAME="imms-${SUB_ENVIRONMENT}-processing-repo"
94-
95-
if ! aws ecr describe-repositories --repository-names "${REPOSITORY_NAME}" --region "${AWS_REGION}" >/dev/null 2>&1; then
96-
echo "ECR repository ${REPOSITORY_NAME} does not exist; creating now..."
97-
aws ecr create-repository --repository-name "${REPOSITORY_NAME}" --region "${AWS_REGION}"
98-
fi
99-
100132
IMAGE_URI="${ECR_REGISTRY}/${REPOSITORY_NAME}:${IMAGE_TAG}"
101133
102134
if aws ecr describe-images --repository-name "${REPOSITORY_NAME}" --image-ids imageTag="${IMAGE_TAG}" --region "${AWS_REGION}" >/dev/null 2>&1; then
@@ -106,60 +138,14 @@ jobs:
106138
docker push "${IMAGE_URI}"
107139
fi
108140
109-
echo "image_tag=${IMAGE_TAG}" >> "$GITHUB_OUTPUT"
110-
111-
terraform-plan:
112-
permissions:
113-
id-token: write
114-
contents: read
115-
needs: build-and-push-recordprocessor
116-
runs-on: ubuntu-latest
117-
env:
118-
TF_VAR_recordprocessor_image_tag: ${{ needs.build-and-push-recordprocessor.outputs.image_tag }}
119-
environment:
120-
name: ${{ inputs.environment }}
121-
steps:
122-
- name: Checkout
123-
uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98
124-
125-
- name: Connect to AWS
126-
uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7
127-
with:
128-
aws-region: eu-west-2
129-
role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops
130-
role-session-name: github-actions
131-
132-
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85
133-
with:
134-
terraform_version: "1.12.2"
135-
136-
- name: Terraform Init
137-
working-directory: infrastructure/instance
138-
run: make init
139-
140-
- name: Terraform Plan
141-
# Ignore cancellations to prevent Terraform from being killed while it holds a state lock
142-
# A stuck process can still be killed with the force-cancel API operation
143-
if: ${{ !failure() }}
144-
working-directory: infrastructure/instance
145-
run: make plan-ci
146-
147-
- name: Save Terraform Plan
148-
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
149-
with:
150-
name: ${{ env.ENVIRONMENT }}-${{ env.SUB_ENVIRONMENT }}-tfplan
151-
path: infrastructure/instance/tfplan
152-
153141
terraform-apply:
154142
permissions:
155143
id-token: write
156144
contents: read
157-
needs:
158-
- terraform-plan
159-
- build-and-push-recordprocessor
145+
needs: terraform-plan
160146
runs-on: ubuntu-latest
161147
env:
162-
TF_VAR_recordprocessor_image_tag: ${{ needs.build-and-push-recordprocessor.outputs.image_tag }}
148+
TF_VAR_recordprocessor_image_tag: ${{ github.sha }}
163149
environment:
164150
name: ${{ inputs.environment }}
165151
steps:

0 commit comments

Comments
 (0)