|
| 1 | +name: Deploy to Amazon ECS staging and production |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +env: |
| 9 | + AWS_REGION: MY_AWS_REGION # set this to your preferred AWS region, e.g. us-west-1 |
| 10 | + ECR_REPOSITORY: MY_ECR_REPOSITORY # set this to your Amazon ECR repository name |
| 11 | + ECS_STAGING_SERVICE: MY_STAGING_ECS_SERVICE # set this to your Amazon ECS staging service name |
| 12 | + ECS_PRODUCTION_SERVICE: MY_PROD_ECS_SERVICE # set this to your Amazon ECS production service name |
| 13 | + ECS_CLUSTER: MY_ECS_CLUSTER # set this to your Amazon ECS cluster name |
| 14 | + ECS_STAGING_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS staging task definition |
| 15 | + # file, e.g. .aws/task-definition.json |
| 16 | + ECS_PRODUCTION_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS production task definition |
| 17 | + # file, e.g. .aws/task-definition.json |
| 18 | + CONTAINER_NAME: MY_CONTAINER_NAME # set this to the name of the container in the |
| 19 | + # containerDefinitions section of your task definition |
| 20 | + |
| 21 | +jobs: |
| 22 | + push-to-ecr: |
| 23 | + name: Push image to AWS ECR |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + outputs: |
| 27 | + image: ${{ steps.build-image.outputs.image }} |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout |
| 31 | + uses: actions/checkout@v3 |
| 32 | + |
| 33 | + - name: Configure AWS credentials |
| 34 | + uses: aws-actions/configure-aws-credentials@13d241b293754004c80624b5567555c4a39ffbe3 |
| 35 | + with: |
| 36 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 37 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 38 | + aws-region: ${{ env.AWS_REGION }} |
| 39 | + mask-aws-account-id: 'no' |
| 40 | + |
| 41 | + - name: Login to Amazon ECR |
| 42 | + id: login-ecr |
| 43 | + uses: aws-actions/amazon-ecr-login@aaf69d68aa3fb14c1d5a6be9ac61fe15b48453a2 |
| 44 | + |
| 45 | + - name: Build, tag, and push image to Amazon ECR |
| 46 | + id: build-image |
| 47 | + env: |
| 48 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 49 | + IMAGE_TAG: ${{ github.sha }} |
| 50 | + run: | |
| 51 | + # Build a docker container and |
| 52 | + # push it to ECR so that it can |
| 53 | + # be deployed to ECS. |
| 54 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 55 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 56 | + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" |
| 57 | +
|
| 58 | + deploy-staging: |
| 59 | + name: Deploy staging |
| 60 | + runs-on: ubuntu-latest |
| 61 | + environment: staging |
| 62 | + needs: push-to-ecr |
| 63 | + |
| 64 | + outputs: |
| 65 | + image: ${{ steps.build-image.outputs.image }} |
| 66 | + |
| 67 | + steps: |
| 68 | + - name: Configure AWS credentials |
| 69 | + uses: aws-actions/configure-aws-credentials@13d241b293754004c80624b5567555c4a39ffbe3 |
| 70 | + with: |
| 71 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 72 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 73 | + aws-region: ${{ env.AWS_REGION }} |
| 74 | + |
| 75 | + - name: Download task definition |
| 76 | + run: | |
| 77 | + aws ecs describe-task-definition --task-definition ${{ env.ECS_STAGING_TASK_DEFINITION }} \ |
| 78 | + --query taskDefinition > task-definition.json |
| 79 | +
|
| 80 | + - name: Fill in the new image ID in the Amazon ECS task definition |
| 81 | + id: task-def |
| 82 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 83 | + with: |
| 84 | + task-definition: task-definition.json |
| 85 | + container-name: ${{ env.CONTAINER_NAME }} |
| 86 | + image: ${{ needs.push-to-ecr.outputs.image }} |
| 87 | + |
| 88 | + - name: Deploy Amazon ECS staging task definition |
| 89 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
| 90 | + with: |
| 91 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 92 | + service: ${{ env.ECS_STAGING_SERVICE }} |
| 93 | + cluster: ${{ env.ECS_CLUSTER }} |
| 94 | + wait-for-service-stability: true |
| 95 | + |
| 96 | + deploy-production: |
| 97 | + name: Deploy production |
| 98 | + runs-on: ubuntu-latest |
| 99 | + environment: production |
| 100 | + needs: [push-to-ecr, deploy-staging] |
| 101 | + |
| 102 | + steps: |
| 103 | + - name: Configure AWS credentials |
| 104 | + uses: aws-actions/configure-aws-credentials@13d241b293754004c80624b5567555c4a39ffbe3 |
| 105 | + with: |
| 106 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 107 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 108 | + aws-region: ${{ env.AWS_REGION }} |
| 109 | + |
| 110 | + - name: Download task definition |
| 111 | + run: | |
| 112 | + aws ecs describe-task-definition --task-definition ${{ env.ECS_PRODUCTION_TASK_DEFINITION }} \ |
| 113 | + --query taskDefinition > task-definition.json |
| 114 | +
|
| 115 | + - name: Fill in the new image ID in the Amazon ECS task definition |
| 116 | + id: task-def |
| 117 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 118 | + with: |
| 119 | + task-definition: task-definition.json |
| 120 | + container-name: ${{ env.CONTAINER_NAME }} |
| 121 | + image: ${{ needs.push-to-ecr.outputs.image }} |
| 122 | + |
| 123 | + - name: Deploy Amazon ECS production task definition |
| 124 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
| 125 | + with: |
| 126 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 127 | + service: ${{ env.ECS_PRODUCTION_SERVICE }} |
| 128 | + cluster: ${{ env.ECS_CLUSTER }} |
| 129 | + wait-for-service-stability: true |
0 commit comments