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