|
| 1 | +name: Build and Deploy Backend to ECR (on backend changes) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + paths: |
| 7 | + - 'backend/**' |
| 8 | + - '.github/workflows/backend-ecr-deploy.yml' |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-push-and-redeploy: |
| 12 | + name: Build, Push to ECR, and Redeploy ECS Service |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | + env: |
| 18 | + AWS_REGION: us-east-2 |
| 19 | + ECR_REGISTRY: 058264255918.dkr.ecr.us-east-2.amazonaws.com |
| 20 | + ECR_REPOSITORY: bug-free-octo-spork/backend |
| 21 | + IMAGE_TAG: latest |
| 22 | + ECS_SERVICE_NAME: bug-free-octo-spork-django-task-service-i1pnpewj |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Configure AWS credentials |
| 29 | + uses: aws-actions/configure-aws-credentials@v4 |
| 30 | + with: |
| 31 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 32 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 33 | + aws-region: ${{ env.AWS_REGION }} |
| 34 | + |
| 35 | + - name: Login to Amazon ECR |
| 36 | + id: login-ecr |
| 37 | + uses: aws-actions/amazon-ecr-login@v2 |
| 38 | + |
| 39 | + - name: Build Docker image |
| 40 | + run: | |
| 41 | + IMAGE_URI=${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG} |
| 42 | + echo "Building ${IMAGE_URI}" |
| 43 | + docker build -f backend/Dockerfile -t ${IMAGE_URI} backend |
| 44 | +
|
| 45 | + - name: Push Docker image |
| 46 | + run: | |
| 47 | + IMAGE_URI=${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG} |
| 48 | + docker push ${IMAGE_URI} |
| 49 | +
|
| 50 | + - name: Force new deployment on ECS service |
| 51 | + env: |
| 52 | + ECS_CLUSTER: ${{ secrets.AWS_ECS_CLUSTER }} |
| 53 | + run: | |
| 54 | + aws ecs update-service \ |
| 55 | + --cluster "${ECS_CLUSTER}" \ |
| 56 | + --service "${ECS_SERVICE_NAME}" \ |
| 57 | + --force-new-deployment \ |
| 58 | + --region "${AWS_REGION}" |
| 59 | +
|
| 60 | +
|
0 commit comments