Skip to content

Deploy to Amazon ECS #160

Deploy to Amazon ECS

Deploy to Amazon ECS #160

Workflow file for this run

name: Deploy to Amazon ECS
on:
push:
branches:
- tasking-manager-fastapi
paths:
- "backend/**"
workflow_dispatch:
env:
REGISTRY: ghcr.io
AWS_REGION: us-east-1
IMAGE_NAME: hotosm/tasking-manager-backend
TASK_DEFINITION: tasking-manager-hotosm-staging-fastapi
ECS_CLUSTER: tasking-manager-staging-cluster
ECS_SERVICE: tasking-manager-hotosm-staging-fastapi
CONTAINER_NAME: tasking-manager-hotosm-staging-fastapi
OIDC_ROLE_ARN: arn:aws:iam::670261699094:role/Github-AWS-OIDC
jobs:
get-deployment-meta:
# will be removed infuture with usage of ${{ github.ref_name }} directly in the workflow environment
name: Get Deployment Meta
runs-on: ubuntu-latest
outputs:
WF_ENVIRONMENT: ${{ steps.export_meta.outputs.WF_ENVIRONMENT }}
WF_ENVIRONMENT_URL: ${{ steps.export_meta.outputs.WF_ENVIRONMENT_URL }}
steps:
- name: Export Deployment Meta
id: export_meta
shell: bash
run: |
case "${{ github.ref }}" in
refs/heads/develop)
export WF_ENVIRONMENT=staging
export WF_ENVIRONMENT_URL=https://tasks-stage.hotosm.org
;;
refs/heads/tasking-manager-fastapi)
export WF_ENVIRONMENT=staging
export WF_ENVIRONMENT_URL=https://tasks-stage.hotosm.org
;;
refs/heads/ci/gh-workflows)
export WF_ENVIRONMENT=staging
export WF_ENVIRONMENT_URL=https://tasks-stage.hotosm.org
;;
esac
echo "WF_ENVIRONMENT=${WF_ENVIRONMENT}" >> $GITHUB_OUTPUT
echo "WF_ENVIRONMENT_URL=${WF_ENVIRONMENT_URL}" >> $GITHUB_OUTPUT
image-build-and-push:
name: Build Container Images
runs-on: ubuntu-latest
needs:
- get-deployment-meta
environment:
name: ${{ needs.get-deployment-meta.outputs.WF_ENVIRONMENT }}
url: ${{ needs.get-deployment-meta.outputs.WF_ENVIRONMENT_URL }}
permissions:
contents: read
packages: write
outputs:
image_tags: ${{ steps.meta.outputs.tags }}
steps:
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set container image metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=fastapi
- name: Build and push container image
id: build-push-image
uses: docker/build-push-action@v5
with:
context: "{{defaultContext}}"
target: prod
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
migration:
name: Run migration on ECS
runs-on: ubuntu-latest
needs:
- get-deployment-meta
- image-build-and-push
environment:
name: ${{ needs.get-deployment-meta.outputs.WF_ENVIRONMENT }}
url: ${{ needs.get-deployment-meta.outputs.WF_ENVIRONMENT_URL }}
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.OIDC_ROLE_ARN }}
role-session-name: gh-ci-ecs-deploy
- name: Run ECS Task with Migration Command
id: run-task
run: |
TASK_ARN=$(aws ecs run-task \
--cluster ${{ env.ECS_CLUSTER }} \
--launch-type FARGATE \
--task-definition ${{ env.TASK_DEFINITION }} \
--overrides '{
"containerOverrides": [
{
"name": "fastapi-migration",
"command": ["sh", "-c", "alembic -c migrations/alembic.ini upgrade head"]
}
]
}' \
--query "tasks[0].taskArn" \
--output text)
echo "TASK_ARN=$TASK_ARN" >> $GITHUB_OUTPUT
- name: Wait for Migration Completion
run: aws ecs wait tasks-stopped --cluster ${{ env.ECS_CLUSTER }} --tasks "${{ steps.run-task.outputs.TASK_ARN }}"
deploy:
name: Deploy to ECS
runs-on: ubuntu-latest
needs:
- get-deployment-meta
- image-build-and-push
- migration
environment:
name: ${{ needs.get-deployment-meta.outputs.WF_ENVIRONMENT }}
url: ${{ needs.get-deployment-meta.outputs.WF_ENVIRONMENT_URL }}
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.OIDC_ROLE_ARN }}
role-session-name: gh-ci-ecs-deploy
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition ${{ env.TASK_DEFINITION }} --query taskDefinition > task-definition.json
- name: Task definition rendition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ needs.image-build-and-push.outputs.image_tags }}
- name: Deploy task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true