|
| 1 | +# This workflow will build and push a new container image to Amazon ECR, |
| 2 | +# and then will deploy a new task definition to Amazon ECS. |
| 3 | +# It is triggered manually via workflow_dispatch. |
| 4 | +# |
| 5 | +# To use this workflow, you will need to complete the following set-up steps: |
| 6 | +# |
| 7 | +# 1. Create an ECR repository to store your images. |
| 8 | +# For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`. |
| 9 | +# Set the `AWS_REGION` and `ECR_REPOSITORY` variables in your GitHub repository/environment settings. |
| 10 | +# |
| 11 | +# 2. Create an ECS task definition, an ECS cluster, and an ECS service. |
| 12 | +# For example, follow the Getting Started guide on the ECS console: |
| 13 | +# https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun |
| 14 | +# Set the `ECS_SERVICE` and `ECS_CLUSTER` variables in your GitHub repository/environment settings. |
| 15 | +# |
| 16 | +# 3. Store your ECS task definition as a JSON file in your repository. |
| 17 | +# The format should follow the output of `aws ecs register-task-definition --generate-cli-skeleton`. |
| 18 | +# Set the `ECS_TASK_DEFINITION` and `CONTAINER_NAME` variables in your GitHub repository/environment settings. |
| 19 | +# |
| 20 | +# 4. Configure GitHub OIDC for AWS and create an IAM role that GitHub Actions can assume. |
| 21 | +# Store the IAM role ARN in a GitHub Actions secret named `AWS_ROLE_ARN`. |
| 22 | +# See https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services |
| 23 | +# for setup instructions and recommended IAM policies. |
| 24 | + |
| 25 | +name: Deploy to Amazon ECS |
| 26 | + |
| 27 | +on: |
| 28 | + workflow_dispatch: |
| 29 | + |
| 30 | +env: |
| 31 | + AWS_REGION: ${{ vars.AWS_REGION }} |
| 32 | + ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} |
| 33 | + ECS_SERVICE: ${{ vars.ECS_SERVICE }} |
| 34 | + ECS_CLUSTER: ${{ vars.ECS_CLUSTER }} |
| 35 | + ECS_TASK_DEFINITION: ${{ vars.ECS_TASK_DEFINITION }} |
| 36 | + CONTAINER_NAME: ${{ vars.CONTAINER_NAME }} |
| 37 | + |
| 38 | +permissions: |
| 39 | + contents: read |
| 40 | + id-token: write |
| 41 | + |
| 42 | +jobs: |
| 43 | + deploy: |
| 44 | + name: Deploy |
| 45 | + runs-on: ubuntu-latest |
| 46 | + environment: production |
| 47 | + |
| 48 | + steps: |
| 49 | + - name: Checkout |
| 50 | + uses: actions/checkout@v4 |
| 51 | + |
| 52 | + - name: Configure AWS credentials |
| 53 | + uses: aws-actions/configure-aws-credentials@v1 |
| 54 | + with: |
| 55 | + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} |
| 56 | + aws-region: ${{ env.AWS_REGION }} |
| 57 | + |
| 58 | + - name: Login to Amazon ECR |
| 59 | + id: login-ecr |
| 60 | + uses: aws-actions/amazon-ecr-login@v1 |
| 61 | + |
| 62 | + - name: Build, tag, and push image to Amazon ECR |
| 63 | + id: build-image |
| 64 | + env: |
| 65 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 66 | + IMAGE_TAG: ${{ github.sha }} |
| 67 | + run: | |
| 68 | + # Build a docker container and |
| 69 | + # push it to ECR so that it can |
| 70 | + # be deployed to ECS. |
| 71 | + docker build -f backend/Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG backend |
| 72 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 73 | + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 74 | +
|
| 75 | + - name: Fill in the new image ID in the Amazon ECS task definition |
| 76 | + id: task-def |
| 77 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 78 | + with: |
| 79 | + task-definition: ${{ env.ECS_TASK_DEFINITION }} |
| 80 | + container-name: ${{ env.CONTAINER_NAME }} |
| 81 | + image: ${{ steps.build-image.outputs.image }} |
| 82 | + |
| 83 | + - name: Deploy Amazon ECS task definition |
| 84 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
| 85 | + with: |
| 86 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 87 | + service: ${{ env.ECS_SERVICE }} |
| 88 | + cluster: ${{ env.ECS_CLUSTER }} |
| 89 | + wait-for-service-stability: true |
0 commit comments