Skip to content

Commit

Permalink
build(ecs): merge server and worker ECS deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleynguyen committed Oct 25, 2023
1 parent 261491b commit 086916c
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 267 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
cicd-role: 'arn:aws:iam::964715690079:role/plumber-prod-github-oidc-role'
ecr-repository: 'plumber-prod'
ecs-cluster-name: 'plumber-prod-ecs'
ecs-service-name: 'plumber-prod-ecs-service'
ecs-worker-service-name: 'plumber-prod-ecs-worker-service'
ecs-container-name: 'app'
codedeploy-application: 'plumber-prod-ecs-app'
codedeploy-deployment-group: 'plumber-prod-ecs-dg'
release-version: ${{ github.sha }}
release-version: ${{ github.sha }}
3 changes: 0 additions & 3 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
cicd-role: 'arn:aws:iam::964715690079:role/plumber-staging-github-oidc-role'
ecr-repository: 'plumber-staging'
ecs-cluster-name: 'plumber-staging-ecs'
ecs-service-name: 'plumber-staging-ecs-service'
ecs-worker-service-name: 'plumber-staging-ecs-worker-service'
ecs-container-name: 'app'
codedeploy-application: 'plumber-staging-ecs-app'
codedeploy-deployment-group: 'plumber-staging-ecs-dg'
release-version: ${{ github.sha }}
24 changes: 24 additions & 0 deletions .github/workflows/deploy-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Deploy to test

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

on:
push:
branches:
- build/ecs-deployment

jobs:
deploy:
name: Deploy
uses: ./.github/workflows/deploy.yml
with:
environment: 'test'
aws-account-id: '394714924170'
cicd-role: 'arn:aws:iam::394714924170:role/oidc-admin'
ecr-repository: 'plumber-test'
ecs-cluster-name: 'plumber-test-ecs'
codedeploy-application: 'plumber-test-ecs-app'
codedeploy-deployment-group: 'plumber-test-ecs-dg'
release-version: ${{ github.sha }}
61 changes: 20 additions & 41 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ on:
description: 'ECS cluster to deploy to'
required: true
type: string
ecs-service-name:
description: 'ECS service for server'
required: true
type: string
ecs-worker-service-name:
description: 'ECS service for worker'
required: true
type: string
ecs-container-name:
description: 'Name of container in ECS task definition'
required: true
type: string
codedeploy-application:
description: 'CodeDeploy application to use'
required: true
Expand Down Expand Up @@ -89,11 +77,6 @@ jobs:
docker push ${{ steps.ecr-image-uri.outputs.image }}
deploy:
strategy:
matrix:
service:
- server
- worker
needs: build
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -121,50 +104,46 @@ jobs:
IMAGE_TAG: ${{ inputs.environment }}-${{ inputs.release-version }}
run: echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

- name: Merge ECS task definition templates
id: merge-ecs-templates
run: |
cp ecs/task-definition.json ecs-task-definition.json
jq --argjson environment "$(jq '.environment' ecs/env.json)" '.containerDefinitions[0].environment += $environment' ecs-task-definition.json
jq --argjson environment "$(jq '.secrets' ecs/env.json)" '.containerDefinitions[0].environment += $environment' ecs-task-definition.json
jq --argjson environment "$(jq '.environment' ecs/env.json)" '.containerDefinitions[1].environment += $environment' ecs-task-definition.json
jq --argjson environment "$(jq '.secrets' ecs/env.json)" '.containerDefinitions[1].environment += $environment' ecs-task-definition.json
cp ecs/appspec.json appspec.json
- name: Replace variables in task definition file
run: |
sed -i 's/<AWS_ACCOUNT_ID>/${{ inputs.aws-account-id }}/g' ecs-task-definition.json
sed -i 's/<ENVIRONMENT>/${{ inputs.environment }}/g' ecs-task-definition.json
sed -i 's/<SERVICE>/${{ matrix.service }}/g' ecs-task-definition.json
sed -i 's/<AWS_ACCOUNT_ID>/${{ inputs.aws-account-id }}/g' appspec.json
sed -i 's/<ENVIRONMENT>/${{ inputs.environment }}/g' appspec.json
sed -i 's/<SERVICE>/${{ matrix.service }}/g' appspec.json
- name: Replace start command in task definition file (server)
if: matrix.service == 'server'
run: |
sed -i 's/<START_COMMAND>/start/g' ecs-task-definition.json
- name: Replace start command in task definition file (worker)
if: matrix.service == 'worker'
run: |
sed -i 's/<START_COMMAND>/start:worker/g' ecs-task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: server-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ecs-task-definition.json
container-name: ${{ inputs.ecs-container-name }}
container-name: server
image: ${{ steps.ecr-image-uri.outputs.image }}

- name: Fill in the new image ID in the Amazon ECS task definition
id: full-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.server-task-def.outputs.task-definition }}
container-name: worker
image: ${{ steps.ecr-image-uri.outputs.image }}

- name: Deploy Amazon ECS task definition to server
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
if: matrix.service == 'server'
with:
task-definition: ${{ steps.server-task-def.outputs.task-definition }}
task-definition: ${{ steps.full-task-def.outputs.task-definition }}
cluster: ${{ inputs.ecs-cluster-name }}
service: ${{ inputs.ecs-service-name }}
wait-for-service-stability: false
codedeploy-appspec: appspec.json
codedeploy-application: ${{ inputs.codedeploy-application }}
codedeploy-deployment-group: ${{ inputs.codedeploy-deployment-group }}

- name: Deploy Amazon ECS task definition to worker
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
if: matrix.service == 'worker'
with:
task-definition: ${{ steps.server-task-def.outputs.task-definition }}
cluster: ${{ inputs.ecs-cluster-name }}
service: ${{ inputs.ecs-worker-service-name }}
wait-for-service-stability: false
218 changes: 0 additions & 218 deletions ecs-task-definition.json

This file was deleted.

2 changes: 1 addition & 1 deletion appspec.json → ecs/appspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"TargetService": {
"Type": "AWS::ECS::Service",
"Properties": {
"TaskDefinition": "arn:aws:ecs:ap-southeast-1:<AWS_ACCOUNT_ID>:task-definition/plumber-<ENVIRONMENT>-ecs-<SERVICE>:1",
"TaskDefinition": "arn:aws:ecs:ap-southeast-1:<AWS_ACCOUNT_ID>:task-definition/plumber-<ENVIRONMENT>-ecs-server:1",
"LoadBalancerInfo": {
"ContainerName": "app",
"ContainerPort": 8080
Expand Down
Loading

0 comments on commit 086916c

Please sign in to comment.