Skip to content

Commit cbe07af

Browse files
committed
Improve data replication deployment
* Add the options to deploy just the db snapshot or just the webapp * Taint the DB cluster to force recreation if necessary * Simplify workflow by removing Destroy option that was never used anyway and by doing the db recreation in a single step
1 parent 79a69c4 commit cbe07af

File tree

1 file changed

+40
-76
lines changed

1 file changed

+40
-76
lines changed

.github/workflows/data-replication-pipeline.yml

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Data replication pipeline
2-
run-name: ${{ inputs.action }} data replication resources for ${{ inputs.environment }}
2+
run-name: Deploy ${{ inputs.deployment_type }} data replication resources for ${{ inputs.environment }}
33

44
on:
55
workflow_dispatch:
@@ -15,18 +15,18 @@ on:
1515
- qa
1616
- sandbox-alpha
1717
- sandbox-beta
18+
deployment_type:
19+
description: Service to deploy
20+
required: true
21+
type: choice
22+
options:
23+
- Full deployment
24+
- DB snapshot only
25+
- Webapp only
1826
image_tag:
1927
description: Docker image tag to deploy
2028
required: false
2129
type: string
22-
action:
23-
description: Action to perform on data replication env
24-
required: true
25-
type: choice
26-
options:
27-
- Destroy
28-
- Recreate
29-
default: Recreate
3030
db_snapshot_arn:
3131
description: ARN of the DB snapshot to use (optional)
3232
required: false
@@ -41,6 +41,8 @@ env:
4141
aws_role: ${{ inputs.environment == 'production'
4242
&& 'arn:aws:iam::820242920762:role/GithubDeployDataReplicationInfrastructure'
4343
|| 'arn:aws:iam::393416225559:role/GithubDeployDataReplicationInfrastructure' }}
44+
recreate_db: ${{ inputs.deployment_type == 'Full deployment' || inputs.deployment_type == 'DB snapshot only' }}
45+
recreate_webapp: ${{ inputs.deployment_type == 'Full deployment' || inputs.deployment_type == 'Webapp only' }}
4446

4547
defaults:
4648
run:
@@ -50,8 +52,8 @@ concurrency:
5052
group: deploy-data-replica-${{ inputs.environment }}
5153

5254
jobs:
53-
prepare:
54-
if: ${{ inputs.action == 'Recreate' }}
55+
prepare-db-replica:
56+
if: env.recreate_db == 'true'
5557
name: Prepare data replica
5658
runs-on: ubuntu-latest
5759
permissions:
@@ -95,25 +97,13 @@ jobs:
9597
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
9698
DB_SECRET_ARN=$(terraform output --raw db_secret_arn)
9799
echo "DB_SECRET_ARN=$DB_SECRET_ARN" >> $GITHUB_OUTPUT
98-
- name: ECR login
99-
id: login-ecr
100-
uses: aws-actions/amazon-ecr-login@v2
101-
- name: Get docker image digest
102-
id: get-docker-image-digest
103-
run: |
104-
set -e
105-
DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ inputs.image_tag || github.sha }}"
106-
docker pull "$DOCKER_IMAGE"
107-
DOCKER_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$DOCKER_IMAGE")
108-
DIGEST="${DOCKER_DIGEST#*@}"
109-
echo "DIGEST=$DIGEST" >> $GITHUB_OUTPUT
110100
outputs:
111101
SNAPSHOT_ARN: ${{ steps.get-latest-snapshot.outputs.SNAPSHOT_ARN }}
112102
DB_SECRET_ARN: ${{ steps.get-db-secret-arn.outputs.DB_SECRET_ARN }}
113-
DOCKER_DIGEST: ${{ steps.get-docker-image-digest.outputs.DIGEST }}
114103

115-
plan-destroy:
116-
name: Plan destruction job
104+
prepare-webapp:
105+
if: env.recreate_webapp == 'true'
106+
name: Prepare webapp
117107
runs-on: ubuntu-latest
118108
permissions:
119109
id-token: write
@@ -125,64 +115,34 @@ jobs:
125115
with:
126116
role-to-assume: ${{ env.aws_role }}
127117
aws-region: eu-west-2
128-
- name: Install terraform
129-
uses: hashicorp/setup-terraform@v3
130-
with:
131-
terraform_version: 1.11.4
132-
- name: Terraform Plan
133-
run: |
134-
set -e
135-
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
136-
terraform plan -destroy -var-file="env/${{ inputs.environment }}.tfvars" -var="image_digest=filler_value" \
137-
-var="db_secret_arn=filler_value" -var="imported_snapshot=filler_value" \
138-
-out ${{ runner.temp }}/tfplan_destroy | tee ${{ runner.temp }}/tf_stdout
139-
- name: Upload artifact
140-
uses: actions/upload-artifact@v4
141-
with:
142-
name: tfplan_destroy_infrastructure-${{ inputs.environment }}
143-
path: ${{ runner.temp }}/tfplan_destroy
144-
145-
destroy:
146-
name: Destroy data replication infrastructure
147-
runs-on: ubuntu-latest
148-
needs: plan-destroy
149-
environment: ${{ inputs.environment }}
150-
permissions:
151-
id-token: write
152-
steps:
153-
- name: Checkout code
154-
uses: actions/checkout@v4
155-
- name: Configure AWS Credentials
156-
uses: aws-actions/configure-aws-credentials@v4
157-
with:
158-
role-to-assume: ${{ env.aws_role }}
159-
aws-region: eu-west-2
160-
- name: Install terraform
161-
uses: hashicorp/setup-terraform@v3
162-
with:
163-
terraform_version: 1.11.4
164-
- name: Download artifact
165-
uses: actions/download-artifact@v4
166-
with:
167-
name: tfplan_destroy_infrastructure-${{ inputs.environment }}
168-
path: ${{ runner.temp }}
169-
- name: Terraform Destroy
170-
id: destroy
118+
- name: ECR login
119+
id: login-ecr
120+
uses: aws-actions/amazon-ecr-login@v2
121+
- name: Get docker image digest
122+
id: get-docker-image-digest
171123
run: |
172124
set -e
173-
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
174-
terraform apply ${{ runner.temp }}/tfplan_destroy
125+
DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ inputs.image_tag || github.sha }}"
126+
docker pull "$DOCKER_IMAGE"
127+
DOCKER_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$DOCKER_IMAGE")
128+
DIGEST="${DOCKER_DIGEST#*@}"
129+
echo "DIGEST=$DIGEST" >> $GITHUB_OUTPUT
130+
outputs:
131+
DOCKER_DIGEST: ${{ steps.get-docker-image-digest.outputs.DIGEST }}
175132

176133
plan:
177134
name: Terraform plan
178135
runs-on: ubuntu-latest
179136
needs:
180-
- prepare
181-
- destroy
137+
- prepare-db-replica
138+
- prepare-webapp
139+
if: ${{ !cancelled() &&
140+
(needs.prepare-db-replica.result == 'success' || needs.prepare-db-replica.result == 'skipped') &&
141+
(needs.prepare-webapp.result == 'success' || needs.prepare-webapp.result == 'skipped') }}
182142
env:
183-
SNAPSHOT_ARN: ${{ needs.prepare.outputs.SNAPSHOT_ARN }}
184-
DB_SECRET_ARN: ${{ needs.prepare.outputs.DB_SECRET_ARN }}
185-
DOCKER_DIGEST: ${{ needs.prepare.outputs.DOCKER_DIGEST }}
143+
SNAPSHOT_ARN: ${{ needs.prepare-db-replica.outputs.SNAPSHOT_ARN }}
144+
DB_SECRET_ARN: ${{ needs.prepare-db-replica.outputs.DB_SECRET_ARN }}
145+
DOCKER_DIGEST: ${{ needs.prepare-webapp.outputs.DOCKER_DIGEST }}
186146
permissions:
187147
id-token: write
188148
steps:
@@ -202,6 +162,10 @@ jobs:
202162
run: |
203163
set -e
204164
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
165+
if [ "${{ env.recreate_db }}" == "true" ]; then
166+
echo "Tainting the database instance for recreation"
167+
terraform taint aws_rds_cluster.cluster
168+
fi
205169
terraform plan -var="image_digest=${{ env.DOCKER_DIGEST }}" -var="db_secret_arn=${{ env.DB_SECRET_ARN }}" \
206170
-var="imported_snapshot=${{ env.SNAPSHOT_ARN }}" -var-file="env/${{ inputs.environment }}.tfvars" \
207171
-var='allowed_egress_cidr_blocks=${{ inputs.egress_cidr }}' \

0 commit comments

Comments
 (0)