chore: tweak #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Application Deployment | |
on: | |
push: | |
branches: | |
- 'dev' | |
- 'staging' | |
- 'production' | |
env: | |
REGISTRY: ghcr.io | |
CONTAINER_NAME: dapp | |
APPLICATION_NAME: application | |
TASK_DEFINITION: .github/workflows/task-definition.json | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
environment: ${{ github.ref_name }} | |
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: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
build-args: APP_ENV=${{ github.ref_name }} | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ github.repository }}:${{ github.ref_name }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
deploy: | |
needs: build-and-push | |
runs-on: ubuntu-latest | |
environment: ${{ github.ref_name }} | |
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: ${{ secrets.AWS_REGION }} | |
- name: Update ECS task definition | |
env: | |
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
APP_ENV: ${{ github.ref_name }} | |
run: | | |
sed -i 's|${AWS_ACCOUNT_ID}|'"$AWS_ACCOUNT_ID"'|g' ${{ env.TASK_DEFINITION }} | |
sed -i 's|${AWS_REGION}|'"$AWS_REGION"'|g' ${{ env.TASK_DEFINITION }} | |
sed -i 's|${APP_ENV}|'"$APP_ENV"'|g' ${{ env.TASK_DEFINITION }} | |
sed -i 's|${CONTAINER_NAME}|'"${{ env.CONTAINER_NAME }}"'|g' ${{ env.TASK_DEFINITION }} | |
sed -i 's|${APPLICATION_NAME}|'"${{ env.APPLICATION_NAME }}"'|g' ${{ env.TASK_DEFINITION }} | |
- name: Update image of ECS task definition | |
id: render-task-def | |
uses: aws-actions/amazon-ecs-render-task-definition@v1 | |
with: | |
task-definition: ${{ env.TASK_DEFINITION }} | |
container-name: ${{ env.CONTAINER_NAME }} | |
image: ${{ env.REGISTRY }}/${{ github.repository }}:${{ github.ref_name }} | |
- name: Deploy Amazon ECS task definition | |
uses: aws-actions/amazon-ecs-deploy-task-definition@v1 | |
with: | |
task-definition: ${{ env.TASK_DEFINITION }} | |
service: ${{ env.CONTAINER_NAME }}-ecs-service | |
cluster: ${{ env.APPLICATION_NAME }}-${{ github.ref_name }}-ecs-cluster | |
wait-for-service-stability: true |