Skip to content

Create GitHub Actions for auto deployment to AWS #6

Create GitHub Actions for auto deployment to AWS

Create GitHub Actions for auto deployment to AWS #6

Workflow file for this run

name: Deploy Backend Services to Amazon ECS
on:
push:
branches: [ 'main' ]
pull_request:
branches: [ 'main' ] # Temporary for testing
types: [ 'opened', 'reopened', 'synchronize' ]
env:
AWS_REGION: ap-southeast-1
ECR_REPOSITORY: question
ECS_SERVICE: question-service
ECS_CLUSTER: backend-cluster
ECS_TASK_DEFINITION: question
CONTAINER_NAME: question
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
deploy:
name: Deploy Question Service
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/[email protected]
with:
role-to-assume: arn:aws:iam::491085383176:role/github_ecs
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ env.AWS_REGION }}
- name: Sts GetCallerIdentity
run: |
aws sts get-caller-identity
- name: Login to AWS ECR
uses: aws-actions/[email protected]
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: latest
run: |
docker build -t $ECR_REPOSITORY ./services/question
docker tag question:latest $ECR_REGISTRY/$ECR_REPOSITORY:latest
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: task-def
# uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.build-image.outputs.image }}
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
# with:
# task-definition: ${{ steps.task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: true