-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create github workflow to deploy backend services to AWS
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
name: Deploy Backend Services to Amazon ECS | ||
|
||
on: | ||
push: | ||
branches: [ 'main' ] | ||
|
||
workflow_dispatch: | ||
|
||
permissions: | ||
id-token: write # This is required for requesting the JWT | ||
contents: read # This is required for actions/checkout | ||
|
||
env: | ||
AWS_REGION: ap-southeast-1 | ||
ECS_CLUSTER: backend-cluster | ||
|
||
jobs: | ||
deploy: | ||
name: Deploy Backend Service | ||
runs-on: ubuntu-latest | ||
environment: production | ||
|
||
strategy: | ||
matrix: | ||
service: [ 'match', 'question', 'user' ] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Configure AWS credentials | ||
id: aws-configure | ||
uses: aws-actions/configure-aws-credentials@v4.0.2 | ||
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: Login to AWS ECR | ||
id: login-ecr | ||
uses: aws-actions/amazon-ecr-login@v2.0.1 | ||
|
||
- name: Build and push ${{ matrix.service }} image to Amazon ECR | ||
id: build-image | ||
env: | ||
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
ECR_REPOSITORY: ${{ matrix.service }} | ||
IMAGE_TAG: latest | ||
run: | | ||
echo "Building $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | ||
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./services/${{ matrix.service }} | ||
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | ||
- name: Update AWS Service (${{ matrix.service }}) # Trigger re-deployment with latest image | ||
id: update-service | ||
env: | ||
ECS_SERVICE: ${{ matrix.service }}-service | ||
run: | | ||
echo "Updating $ECS_SERVICE for $ECS_CLUSTER" | ||
aws ecs update-service \ | ||
--cluster $ECS_CLUSTER \ | ||
--service $ECS_SERVICE \ | ||
--force-new-deployment \ | ||
--region $AWS_REGION |