|
| 1 | +# Based on https://docs.github.com/en/actions/deployment/deploying-to-your-cloud-provider/deploying-to-amazon-elastic-container-service |
| 2 | + |
| 3 | +name: aws ecs deploy |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - main |
| 9 | + |
| 10 | +env: |
| 11 | + AWS_REGION: us-east-1 # set this to your preferred AWS region, e.g. us-west-1 |
| 12 | + ECR_REPOSITORY: pubpub-v7 # set this to your Amazon ECR repository name |
| 13 | + ECS_SERVICE: blake-core # set this to your Amazon ECS service name |
| 14 | + ECS_CLUSTER: blake-ecs-cluster-staging # set this to your Amazon ECS cluster name |
| 15 | + ECS_TASK_DEFINITION_TEMPLATE: blake-core # set this to the FAMILY of your task definition |
| 16 | + CONTAINER_NAME: core # set this to the name of the container in the |
| 17 | + # containerDefinitions section of your task definition |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + name: Build |
| 22 | + runs-on: ubuntu-latest |
| 23 | + environment: production |
| 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: Get short sha |
| 37 | + id: shortsha |
| 38 | + run: echo "sha_short=$(git describe --always --abbrev=40 --dirty)" >> $GITHUB_OUTPUT |
| 39 | + |
| 40 | + - name: Login to Amazon ECR |
| 41 | + id: login-ecr |
| 42 | + uses: aws-actions/amazon-ecr-login@v2 |
| 43 | + |
| 44 | + - name: Build, tag, and push image to Amazon ECR |
| 45 | + id: build-image |
| 46 | + env: |
| 47 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 48 | + IMAGE_TAG: ${{ steps.shortsha.outputs.sha_short }} |
| 49 | + run: | |
| 50 | + # Build a docker container and |
| 51 | + # push it to ECR so that it can |
| 52 | + # be deployed to ECS. |
| 53 | + docker build \ |
| 54 | + --platform linux/amd64 \ |
| 55 | + --build-arg PACKAGE=core \ |
| 56 | + --build-arg PACKAGE_DIR=core \ |
| 57 | + -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \ |
| 58 | + . |
| 59 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 60 | + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 61 | +
|
| 62 | + deploy: |
| 63 | + name: Deploy |
| 64 | + runs-on: ubuntu-latest |
| 65 | + environment: production |
| 66 | + needs: |
| 67 | + - build |
| 68 | + |
| 69 | + steps: |
| 70 | + - name: Checkout |
| 71 | + uses: actions/checkout@v4 |
| 72 | + |
| 73 | + - name: Configure AWS credentials |
| 74 | + uses: aws-actions/configure-aws-credentials@v4 |
| 75 | + with: |
| 76 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 77 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 78 | + aws-region: ${{ env.AWS_REGION }} |
| 79 | + |
| 80 | + - name: Get short sha |
| 81 | + id: shortsha |
| 82 | + run: echo "sha_short=$(git describe --always --abbrev=40 --dirty)" >> $GITHUB_OUTPUT |
| 83 | + |
| 84 | + - name: Login to Amazon ECR |
| 85 | + id: login-ecr |
| 86 | + uses: aws-actions/amazon-ecr-login@v2 |
| 87 | + |
| 88 | + - name: Retrieve Task Definition contents from template |
| 89 | + id: get-taskdef |
| 90 | + run: | |
| 91 | + aws ecs describe-task-definition \ |
| 92 | + --task-definition $ECS_TASK_DEFINITION_TEMPLATE \ |
| 93 | + --query taskDefinition >> template_task_def.json |
| 94 | +
|
| 95 | + - name: Interpolate image tag |
| 96 | + id: write-tag |
| 97 | + env: |
| 98 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 99 | + IMAGE_TAG: ${{ steps.shortsha.outputs.sha_short }} |
| 100 | + run: | |
| 101 | + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 102 | +
|
| 103 | + - name: Fill in the new image ID in the Amazon ECS task definition |
| 104 | + id: task-def |
| 105 | + uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc |
| 106 | + with: |
| 107 | + task-definition: template_task_def.json |
| 108 | + container-name: ${{ env.CONTAINER_NAME }} |
| 109 | + image: ${{ steps.write-tag.outputs.image }} |
| 110 | + |
| 111 | + - name: Deploy Amazon ECS task definition |
| 112 | + uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a |
| 113 | + with: |
| 114 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 115 | + service: ${{ env.ECS_SERVICE }} |
| 116 | + cluster: ${{ env.ECS_CLUSTER }} |
| 117 | + wait-for-service-stability: true |
0 commit comments