Skip to content

Build Docker Image & Deploy to ECS #43

Build Docker Image & Deploy to ECS

Build Docker Image & Deploy to ECS #43

name: Build Docker Image & Deploy to ECS
on:
workflow_run:
workflows: ['Publish NPM Packages']
types:
- completed
env:
SERVER_REGISTRY: ghcr.io
SERVER_IMAGE_NAME: ${{ github.repository }}/server
ECS_SERVICE: lecca-io-server-core-service
ECS_CLUSTER: lecca-io-server-core
ECS_TASK_DEFINITION: .aws/task-definition.json
CONTAINER_NAME: lecca-io-server-core
AWS_REGION: us-east-2
jobs:
build-and-push:
name: Build and Push Image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image: ${{ steps.build-image.outputs.image }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.SERVER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
id: build-image
env:
VERSION: ${{ github.ref_name }}
run: |
docker build -t ${{ env.SERVER_REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:$VERSION -f packages/server/Dockerfile .
docker tag ${{ env.SERVER_REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:$VERSION ${{ env.SERVER_REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:latest
docker push ${{ env.SERVER_REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:$VERSION
docker push ${{ env.SERVER_REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:latest
echo "image=${{ env.SERVER_REGISTRY }}/${{ env.SERVER_IMAGE_NAME }}:$VERSION" >> $GITHUB_OUTPUT
deploy-to-ecs:
name: Deploy to ECS
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Replace placeholders in task definition
run: |
sed -i 's|{{ DATABASE_URL_ARN }}|'"${{ secrets.DATABASE_URL_ARN }}"'|g' ${{ env.ECS_TASK_DEFINITION }}
sed -i 's|{{ PARAM_STORE_ACCESS_KEY_ARN }}|'"${{ secrets.PARAM_STORE_ACCESS_KEY_ARN }}"'|g' ${{ env.ECS_TASK_DEFINITION }}
sed -i 's|{{ PARAM_STORE_SECRET_ACCESS_KEY_ARN }}|'"${{ secrets.PARAM_STORE_SECRET_ACCESS_KEY_ARN }}"'|g' ${{ env.ECS_TASK_DEFINITION }}
sed -i 's|{{ PARAM_STORE_REGION_ARN }}|'"${{ secrets.PARAM_STORE_REGION_ARN }}"'|g' ${{ env.ECS_TASK_DEFINITION }}
sed -i 's|{{ TASK_ROLE_ARN }}|'"${{ secrets.TASK_ROLE_ARN }}"'|g' ${{ env.ECS_TASK_DEFINITION }}
sed -i 's|{{ EXECUTION_ROLE_ARN }}|'"${{ secrets.EXECUTION_ROLE_ARN }}"'|g' ${{ env.ECS_TASK_DEFINITION }}
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ needs.build-and-push.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true