|
| 1 | +name: CD |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + # push: |
| 6 | + # Uncomment and add the necessary branches to enable automatic deployment on AWS |
| 7 | + # branches: |
| 8 | + # - main |
| 9 | + |
| 10 | +jobs: |
| 11 | + deploy: |
| 12 | + name: Deploy on AWS ECS |
| 13 | + runs-on: ubuntu-latest |
| 14 | + environment: |
| 15 | + name: ${{ github.ref == 'refs/heads/main' && 'production' || github.ref_name }} |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Configure AWS credentials |
| 21 | + uses: aws-actions/configure-aws-credentials@v1 |
| 22 | + with: |
| 23 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 24 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 25 | + aws-region: ${{ vars.AWS_REGION }} |
| 26 | + |
| 27 | + - name: Set up Docker Buildx |
| 28 | + uses: docker/setup-buildx-action@v2 |
| 29 | + |
| 30 | + - name: Login to Amazon ECR |
| 31 | + uses: aws-actions/amazon-ecr-login@v1 |
| 32 | + |
| 33 | + - name: Build and push Docker image |
| 34 | + id: build-and-push |
| 35 | + uses: docker/build-push-action@v4 |
| 36 | + with: |
| 37 | + context: . |
| 38 | + push: true |
| 39 | + tags: ${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.sha }} |
| 40 | + |
| 41 | + - name: Get the image digest |
| 42 | + id: image-digest |
| 43 | + run: echo "image=${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT |
| 44 | + |
| 45 | + - name: Download task definition |
| 46 | + run: | |
| 47 | + aws ecs describe-task-definition --task-definition ${{ vars.ECS_TASK_DEFINITION }} --query taskDefinition > ${{ vars.ECS_TASK_DEFINITION_PATH }} |
| 48 | +
|
| 49 | + - name: Fill in the new image ID in the Amazon ECS task definition |
| 50 | + id: task-def |
| 51 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 52 | + with: |
| 53 | + task-definition: ${{ vars.ECS_TASK_DEFINITION_PATH }} |
| 54 | + container-name: ${{ vars.CONTAINER_NAME }} |
| 55 | + image: ${{ steps.image-digest.outputs.image }} |
| 56 | + |
| 57 | + - name: Deploy Amazon ECS task definition |
| 58 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
| 59 | + with: |
| 60 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 61 | + service: ${{ vars.ECS_SERVICE }} |
| 62 | + cluster: ${{ vars.ECS_CLUSTER }} |
| 63 | + wait-for-service-stability: true |
0 commit comments