|
| 1 | +name: Deploy to Amazon ECS |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +env: |
| 10 | + AWS_REGION: us-west-2 # Set your AWS region |
| 11 | + ECR_REPOSITORY: fiesta-api # Set your ECR repository name |
| 12 | + ECS_SERVICE: fiesta-api-service # Set your ECS service name |
| 13 | + ECS_CLUSTER: fiesta-api-cluster # Set your ECS cluster name |
| 14 | + ECS_TASK_DEFINITION: .aws/task-definition.json # Path to task definition |
| 15 | + CONTAINER_NAME: fiesta-api # Container name in task definition |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | + |
| 20 | +jobs: |
| 21 | + deploy: |
| 22 | + name: Deploy |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Configure AWS credentials |
| 30 | + uses: aws-actions/configure-aws-credentials@v4 |
| 31 | + with: |
| 32 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 33 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 34 | + aws-region: ${{ env.AWS_REGION }} |
| 35 | + |
| 36 | + - name: Login to Amazon ECR |
| 37 | + id: login-ecr |
| 38 | + uses: aws-actions/amazon-ecr-login@v2 |
| 39 | + |
| 40 | + - name: Build, tag, and push image to Amazon ECR |
| 41 | + id: build-image |
| 42 | + env: |
| 43 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 44 | + IMAGE_TAG: ${{ github.sha }} |
| 45 | + run: | |
| 46 | + # Build a docker container and push it to ECR |
| 47 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 48 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 49 | + docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 50 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 51 | + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - name: Fill in the new image ID in the Amazon ECS task definition |
| 54 | + id: task-def |
| 55 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 56 | + with: |
| 57 | + task-definition: ${{ env.ECS_TASK_DEFINITION }} |
| 58 | + container-name: ${{ env.CONTAINER_NAME }} |
| 59 | + image: ${{ steps.build-image.outputs.image }} |
| 60 | + |
| 61 | + - name: Deploy Amazon ECS task definition |
| 62 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v2 |
| 63 | + with: |
| 64 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 65 | + service: ${{ env.ECS_SERVICE }} |
| 66 | + cluster: ${{ env.ECS_CLUSTER }} |
| 67 | + wait-for-service-stability: true |
0 commit comments