|
| 1 | +name: Deploy Backend Services to Amazon ECS |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ 'main' ] |
| 6 | + |
| 7 | + # Allows you to run this workflow manually from the Actions tab |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + id-token: write # This is required for requesting the JWT |
| 12 | + contents: read # This is required for actions/checkout |
| 13 | + |
| 14 | +env: |
| 15 | + AWS_REGION: ap-southeast-1 |
| 16 | + ECS_CLUSTER: backend-cluster |
| 17 | + |
| 18 | +jobs: |
| 19 | + deploy: |
| 20 | + name: Deploy Backend Service |
| 21 | + runs-on: ubuntu-latest |
| 22 | + environment: production |
| 23 | + |
| 24 | + strategy: |
| 25 | + matrix: |
| 26 | + service: [ 'match', 'question', 'user' ] |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Configure AWS credentials |
| 33 | + id: aws-configure |
| 34 | + uses: aws-actions/[email protected] |
| 35 | + with: |
| 36 | + role-to-assume: arn:aws:iam::491085383176:role/github_ecs |
| 37 | + role-session-name: GitHub_to_AWS_via_FederatedOIDC |
| 38 | + aws-region: ${{ env.AWS_REGION }} |
| 39 | + |
| 40 | + - name: Login to AWS ECR |
| 41 | + id: login-ecr |
| 42 | + uses: aws-actions/[email protected] |
| 43 | + |
| 44 | + - name: Build and push ${{ matrix.service }} image to Amazon ECR |
| 45 | + id: build-image |
| 46 | + env: |
| 47 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 48 | + ECR_REPOSITORY: ${{ matrix.service }} |
| 49 | + IMAGE_TAG: latest |
| 50 | + run: | |
| 51 | + echo "Building $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" |
| 52 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./services/${{ matrix.service }} |
| 53 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 54 | +
|
| 55 | + - name: Update AWS Service (${{ matrix.service }}) # Trigger re-deployment with latest image |
| 56 | + id: update-service |
| 57 | + env: |
| 58 | + ECS_SERVICE: ${{ matrix.service }}-service |
| 59 | + run: | |
| 60 | + echo "Updating $ECS_SERVICE for $ECS_CLUSTER" |
| 61 | + aws ecs update-service \ |
| 62 | + --cluster $ECS_CLUSTER \ |
| 63 | + --service $ECS_SERVICE \ |
| 64 | + --force-new-deployment \ |
| 65 | + --region $AWS_REGION |
0 commit comments