Skip to content

chore: ci

chore: ci #7

Workflow file for this run

name: Deploy to Amazon ECS
on:
push:
branches: [main]
paths:
- 'api/**'
- '.github/workflows/**'
workflow_dispatch:
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: bz-code-push-production
ECS_CLUSTER: bz-code-push-production
ECS_SERVICE: bz-code-push-production
ECS_TASK_DEFINITION: bz-code-push-production
CONTAINER_NAME: bz-code-push-production
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate Build Info
id: build_info
run: |
echo "APP_BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_ENV
echo "APP_COMMIT_SHA=${GITHUB_SHA}" >> $GITHUB_ENV
- name: Generate Tag Name
id: generate_tag_name
run: |
export TZ='Asia/Seoul'
TAG_PREFIX="release-$(date +"%y%m%d-%H%M")-"
MINOR_RELEASE_VERSION=$(git ls-remote --tags origin | cut -f2 | awk -F/ '{print substr($0, index($0,$3))}' | grep "^${TAG_PREFIX}" | wc -l | awk '{print $1+1}' | xargs printf '%02d')
TAG_NAME="${TAG_PREFIX}${MINOR_RELEASE_VERSION}"
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_ENV
echo "TAG_NAME=${TAG_NAME}" >> "$GITHUB_OUTPUT"
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./api/Dockerfile
platforms: linux/arm64
push: true
build-args: |
APP_BUILD_TIME=${{ env.APP_BUILD_TIME }}
APP_COMMIT_SHA=${{ env.APP_COMMIT_SHA }}
APP_VERSION=${{ env.TAG_NAME }}
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ env.TAG_NAME }}
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest
cache-from: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest
cache-to: type=inline
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} \
--query taskDefinition > task-definition.json
- 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: task-definition.json
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ env.TAG_NAME }}
- 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
# - name: Notify Slack
# uses: 8398a7/action-slack@v3
# if: always()
# with:
# status: ${{ job.status }}
# fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
# env:
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}