Skip to content

Merge pull request #7 from mystist/main #12

Merge pull request #7 from mystist/main

Merge pull request #7 from mystist/main #12

Workflow file for this run

name: Production Deployment
on:
push:
branches:
- 'latest'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
AWS_REGION: ap-east-1
ECS_CLUSTER: todo-cluster
ECS_SERVICE: todo-service
ECS_TASK_DEFINITION: .github/workflows/task-definition.json
CONTAINER_NAME: todo
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
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: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
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 AWS account ID in task definition
env:
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
AWS_GITHUB_PAT_ARN_ID: ${{ secrets.AWS_GITHUB_PAT_ARN_ID }}
ECS_TASK_DEFINITION: ${{ env.ECS_TASK_DEFINITION }}
run: |
sed -i 's|${AWS_ACCOUNT_ID}|'"$AWS_ACCOUNT_ID"'|' $ECS_TASK_DEFINITION
sed -i 's|${AWS_GITHUB_PAT_ARN_ID}|'"$AWS_GITHUB_PAT_ARN_ID"'|' $ECS_TASK_DEFINITION
- name: Render Amazon ECS task definition
id: render-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true