Skip to content

Commit a759f56

Browse files
Update workflows
- Make matrix functionality respect server selection - Update data replication to use new ECS service/deployment flow - Cleanup unnecessary components
1 parent 1c16c63 commit a759f56

File tree

8 files changed

+214
-202
lines changed

8 files changed

+214
-202
lines changed
Lines changed: 31 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Data replication pipeline
1+
name: Deploy Data Replication
22
run-name: ${{ inputs.deployment_type }} for data replication resources for ${{ inputs.environment }}
33

44
on:
@@ -15,26 +15,10 @@ on:
1515
- qa
1616
- sandbox-alpha
1717
- sandbox-beta
18-
deployment_type:
19-
description: Deployment type
20-
required: true
21-
type: choice
22-
options:
23-
- Deployment with DB recreation
24-
- Application only deployment
2518
image_tag:
2619
description: Docker image tag to deploy
2720
required: false
2821
type: string
29-
db_snapshot_arn:
30-
description: ARN of the DB snapshot to use (optional)
31-
required: false
32-
type: string
33-
egress_cidr:
34-
description: CIDR blocks to allow egress traffic.
35-
type: string
36-
required: true
37-
default: "[]"
3822

3923
env:
4024
aws_role: ${{ inputs.environment == 'production'
@@ -49,171 +33,46 @@ concurrency:
4933
group: deploy-data-replica-${{ inputs.environment }}
5034

5135
jobs:
52-
prepare-db-replica:
53-
if: ${{ inputs.deployment_type == 'Deployment with DB recreation' }}
54-
name: Prepare data replica
36+
validate-inputs:
5537
runs-on: ubuntu-latest
56-
permissions:
57-
id-token: write
38+
permissions: { }
5839
steps:
59-
- name: Checkout code
60-
uses: actions/checkout@v5
61-
- name: Configure AWS Credentials
62-
uses: aws-actions/configure-aws-credentials@v4
63-
with:
64-
role-to-assume: ${{ env.aws_role }}
65-
aws-region: eu-west-2
66-
- name: get latest snapshot
67-
id: get-latest-snapshot
40+
- name: Validate inputs
6841
run: |
69-
set -e
70-
if [ -z "${{ inputs.db_snapshot_arn }}" ]; then
71-
echo "No snapshot ARN provided, fetching the latest snapshot"
72-
SNAPSHOT_ARN=$(aws rds describe-db-cluster-snapshots \
73-
--query "DBClusterSnapshots[?DBClusterIdentifier=='mavis-${{ inputs.environment }}'].[DBClusterSnapshotArn, SnapshotCreateTime]" \
74-
--output text | sort -k2 -r | head -n 1 | cut -f1)
75-
76-
if [ -z "$SNAPSHOT_ARN" ]; then
77-
echo "No snapshots found for mavis-${{ inputs.environment }}"
78-
exit 1
79-
fi
80-
else
81-
echo "Using provided snapshot ARN: ${{ inputs.db_snapshot_arn }}"
82-
SNAPSHOT_ARN="${{ inputs.db_snapshot_arn }}"
42+
if [[ "${{ inputs.environment }}" == "preview" || "${{ inputs.environment }}" == "production" ]]; then
43+
if [[ -z "${{ inputs.git_ref_to_deploy }}" ]]; then
44+
echo "Error: git_ref_to_deploy is required for preview and production environments."
45+
exit 1
46+
fi
8347
fi
84-
echo "Using snapshot ARN: $SNAPSHOT_ARN"
85-
echo "SNAPSHOT_ARN=$SNAPSHOT_ARN" >> $GITHUB_OUTPUT
86-
- name: Install terraform
87-
uses: hashicorp/setup-terraform@v3
88-
with:
89-
terraform_version: 1.11.4
90-
outputs:
91-
SNAPSHOT_ARN: ${{ steps.get-latest-snapshot.outputs.SNAPSHOT_ARN }}
92-
93-
prepare-webapp:
94-
name: Prepare webapp
48+
determine-git-sha:
9549
runs-on: ubuntu-latest
96-
permissions:
97-
id-token: write
50+
permissions: { }
51+
needs: validate-inputs
52+
outputs:
53+
git-sha: ${{ steps.get-git-sha.outputs.git-sha }}
9854
steps:
9955
- name: Checkout code
10056
uses: actions/checkout@v5
101-
- name: Configure AWS Credentials
102-
uses: aws-actions/configure-aws-credentials@v4
10357
with:
104-
role-to-assume: ${{ env.aws_role }}
105-
aws-region: eu-west-2
106-
- name: ECR login
107-
id: login-ecr
108-
uses: aws-actions/amazon-ecr-login@v2
109-
- name: Get docker image digest
110-
id: get-docker-image-digest
111-
run: |
112-
set -e
113-
DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ inputs.image_tag || github.sha }}"
114-
docker pull "$DOCKER_IMAGE"
115-
DOCKER_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$DOCKER_IMAGE")
116-
DIGEST="${DOCKER_DIGEST#*@}"
117-
echo "DIGEST=$DIGEST" >> $GITHUB_OUTPUT
118-
outputs:
119-
DOCKER_DIGEST: ${{ steps.get-docker-image-digest.outputs.DIGEST }}
120-
121-
plan:
122-
name: Terraform plan
123-
runs-on: ubuntu-latest
124-
needs:
125-
- prepare-db-replica
126-
- prepare-webapp
127-
if: ${{ !cancelled() &&
128-
(needs.prepare-db-replica.result == 'success' || needs.prepare-db-replica.result == 'skipped') &&
129-
needs.prepare-webapp.result == 'success' }}
130-
env:
131-
SNAPSHOT_ARN: ${{ needs.prepare-db-replica.outputs.SNAPSHOT_ARN }}
132-
DB_SECRET_ARN: ${{ needs.prepare-db-replica.outputs.DB_SECRET_ARN || 'arn:aws:secretsmanager:eu-west-2:000000000000:secret:placeholder' }}
133-
DOCKER_DIGEST: ${{ needs.prepare-webapp.outputs.DOCKER_DIGEST }}
134-
REPLACE_DB_CLUSTER: ${{ inputs.deployment_type == 'Deployment with DB recreation' }}
58+
ref: ${{ inputs.git_ref_to_deploy || github.sha }}
59+
- name: Get git sha
60+
id: get-git-sha
61+
run: echo "git-sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
62+
build-and-push-image:
13563
permissions:
13664
id-token: write
137-
steps:
138-
- name: Checkout code
139-
uses: actions/checkout@v5
140-
- name: Configure AWS Credentials
141-
uses: aws-actions/configure-aws-credentials@v4
142-
with:
143-
role-to-assume: ${{ env.aws_role }}
144-
aws-region: eu-west-2
145-
- name: Install terraform
146-
uses: hashicorp/setup-terraform@v3
147-
with:
148-
terraform_version: 1.11.4
149-
- name: Get db secret arn
150-
id: get-db-secret-arn
151-
working-directory: terraform/app
152-
run: |
153-
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
154-
DB_SECRET_ARN=$(terraform output --raw db_secret_arn)
155-
echo "DB_SECRET_ARN=$DB_SECRET_ARN" >> $GITHUB_OUTPUT
156-
- name: Terraform Plan
157-
id: plan
158-
run: |
159-
set -eo pipefail
160-
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
161-
162-
CIDR_BLOCKS='${{ inputs.egress_cidr }}'
163-
PLAN_ARGS=(
164-
"plan"
165-
"-var=image_digest=${{ env.DOCKER_DIGEST }}"
166-
"-var=db_secret_arn=${{ steps.get-db-secret-arn.outputs.DB_SECRET_ARN }}"
167-
"-var=imported_snapshot=${{ env.SNAPSHOT_ARN }}"
168-
"-var-file=env/${{ inputs.environment }}.tfvars"
169-
"-var=allowed_egress_cidr_blocks=$CIDR_BLOCKS"
170-
"-out=${{ runner.temp }}/tfplan"
171-
)
172-
173-
if [ "${{ env.REPLACE_DB_CLUSTER }}" = "true" ]; then
174-
PLAN_ARGS+=("-replace" "aws_rds_cluster.cluster")
175-
fi
176-
terraform "${PLAN_ARGS[@]}" | tee ${{ runner.temp }}/tf_stdout
177-
- name: Upload artifact
178-
uses: actions/upload-artifact@v4
179-
with:
180-
name: tfplan_infrastructure-${{ inputs.environment }}
181-
path: ${{ runner.temp }}/tfplan
182-
183-
apply:
184-
name: Terraform apply
185-
runs-on: ubuntu-latest
186-
needs: plan
187-
if: ${{ !cancelled() && needs.plan.result == 'success' }}
188-
environment: ${{ inputs.environment }}
65+
needs: determine-git-sha
66+
uses: ./.github/workflows/build-and-push-image.yml
67+
with:
68+
git-sha: ${{ needs.determine-git-sha.outputs.git-sha }}
69+
deploy-application:
18970
permissions:
19071
id-token: write
191-
steps:
192-
- name: Checkout code
193-
uses: actions/checkout@v5
194-
- name: Configure AWS Credentials
195-
uses: aws-actions/configure-aws-credentials@v4
196-
with:
197-
role-to-assume: ${{ env.aws_role }}
198-
aws-region: eu-west-2
199-
- name: Download artifact
200-
uses: actions/download-artifact@v5
201-
with:
202-
name: tfplan_infrastructure-${{ inputs.environment }}
203-
path: ${{ runner.temp }}
204-
- name: Install terraform
205-
uses: hashicorp/setup-terraform@v3
206-
with:
207-
terraform_version: 1.11.4
208-
- name: Apply the changes
209-
run: |
210-
set -e
211-
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
212-
terraform apply ${{ runner.temp }}/tfplan
213-
- name: Deploy db-access-service
214-
run: |
215-
task_definition_arn=$(terraform output -raw task_definition_arn)
216-
aws ecs update-service \
217-
--cluster mavis-${{ inputs.environment }}-data-replication \
218-
--service mavis-${{ inputs.environment }}-data-replication \
219-
--task-definition $task_definition_arn
72+
needs: determine-git-sha
73+
uses: ./.github/workflows/deploy-application.yml
74+
with:
75+
environment: ${{ inputs.environment }}
76+
server_types: data-replication
77+
git_sha_to_deploy: ${{ needs.determine-git-sha.outputs.git-sha }}
78+
app_version: ${{ inputs.git_ref_to_deploy }}

.github/workflows/deploy-application.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
strategy:
7070
fail-fast: true
7171
matrix:
72-
service: [ web, good-job, sidekiq ]
72+
service: ${{ inputs.server_types == 'all' && fromJSON('["web", "good-job", "sidekiq"]') || fromJSON(format('["{0}"]', inputs.server_types)) }}
7373
steps:
7474
- name: Checkout code
7575
uses: actions/checkout@v5
@@ -165,7 +165,7 @@ jobs:
165165
id: deploy-web-service
166166
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
167167
with:
168-
task-definition: ${{ runner.temp }}
168+
task-definition: ${{ runner.temp }}/web-task-definition.json
169169
codedeploy-appspec: config/templates/appspec.yaml
170170
cluster: ${{ env.cluster_name }}
171171
service: mavis-${{ inputs.environment }}-web
@@ -197,13 +197,13 @@ jobs:
197197
name: ${{ inputs.environment }}-good-job-task-definition
198198
- name: Change family of task definition
199199
run: |
200-
file_path="${{ runner.temp }}/web-task-definition.json"
200+
file_path="${{ runner.temp }}/good-job-task-definition.json"
201201
family_name="mavis-good-job-task-definition-${{ inputs.environment }}"
202202
echo "$(jq --arg f "$family_name" '.family = $f' "$file_path")" > "$file_path"
203203
- name: Deploy good-job service
204204
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
205205
with:
206-
task-definition: ${{ runner.temp }}/web-task-definition.json
206+
task-definition: ${{ runner.temp }}/good-job-task-definition.json
207207
cluster: ${{ env.cluster_name }}
208208
service: mavis-${{ inputs.environment }}-good-job
209209
force-new-deployment: true

.github/workflows/deploy-infrastructure.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ on:
88
description: Deployment environment
99
required: true
1010
type: string
11-
image_tag:
12-
required: false
13-
type: string
1411
git_ref_to_deploy:
1512
required: true
1613
type: string
14+
workflow_dispatch:
15+
inputs:
16+
environment:
17+
description: Deployment environment
18+
required: true
19+
type: string
20+
git_ref_to_deploy:
21+
description: The git commit SHA to deploy.
22+
required: false
23+
type: string
1724

1825
permissions: {}
1926

.github/workflows/deploy.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
update-permissions:
110110
runs-on: ubuntu-latest
111111
needs: validate-permissions
112-
if: always() && (inputs.environment == 'production' || inputs.environment == 'preview') && needs.validate-permissions.result == 'failure'
112+
if: ${{ !cancelled() && (inputs.environment == 'production' || inputs.environment == 'preview') && needs.validate-permissions.result == 'failure' }}
113113
environment: ${{ inputs.environment }}
114114
defaults:
115115
run:
@@ -138,19 +138,18 @@ jobs:
138138
validate-permissions,
139139
update-permissions,
140140
]
141-
if: always() &&
141+
if: ${{ !cancelled() &&
142142
((inputs.environment != 'production' && inputs.environment != 'preview') ||
143-
needs.validate-permissions.result == 'success' || needs.update-permissions.result == 'success')
143+
needs.validate-permissions.result == 'success' || needs.update-permissions.result == 'success') }}
144144
uses: ./.github/workflows/deploy-infrastructure.yml
145145
with:
146146
environment: ${{ inputs.environment }}
147-
image_tag: ${{ needs.determine-git-sha.outputs.git-sha }}
148147
git_ref_to_deploy: ${{ inputs.git_ref_to_deploy || github.ref_name }}
149148
deploy-application:
150149
permissions:
151150
id-token: write
152151
needs: [deploy-infrastructure, determine-git-sha]
153-
if: always() && inputs.server_types != 'none' && needs.deploy-infrastructure.result == 'success'
152+
if: ${{ !cancelled() && inputs.server_types != 'none' && needs.deploy-infrastructure.result == 'success' }}
154153
uses: ./.github/workflows/deploy-application.yml
155154
with:
156155
environment: ${{ inputs.environment }}

0 commit comments

Comments
 (0)