Skip to content

feat: DockerHub 이미지 업로드 기능 추가 #1

feat: DockerHub 이미지 업로드 기능 추가

feat: DockerHub 이미지 업로드 기능 추가 #1

name: Release
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true
on:
workflow_dispatch:
inputs:
WORKFLOW_PHASE:
description: "zappa env setup"
required: true
default: dev
type: choice
options:
- dev
- prod
push:
branches:
- "main"
jobs:
BuildAndDeploy:
runs-on: ubuntu-latest
env:
API_STAGE: ${{ github.event_name == 'workflow_dispatch' && inputs.WORKFLOW_PHASE || 'dev' }}
BUMP_RULE: ${{ (github.event_name == 'workflow_dispatch' && inputs.WORKFLOW_PHASE || 'dev') == 'dev' && '--stage' || '' }}
AWS_ECR_REGISTRY: ${{ github.event_name == 'workflow_dispatch' && secrets.AWS_ECR_PROD_URL || secrets.AWS_ECR_DEV_URL }}
steps:
# Checkout source codes
- name: Checkout source codes
uses: actions/checkout@v4
with:
fetch-depth: 0
# Setup AWS Credentials, Python, uv, docker buildx, and login to ECR.
- name: Setup AWS Credentials
uses: aws-actions/configure-aws-credentials@master
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
ignore-nothing-to-cache: true
- name: Install dependencies
run: uv sync --only-group=deployment
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to ECR
uses: docker/login-action@v3
with:
registry: ${{ env.AWS_ECR_REGISTRY }}
- name: Get current date, repo name and release tag version
id: info
run: |
LATEST_TAG=$(git tag -l --sort=-creatordate | head -n 1)
echo "::set-output name=TAG::$(python ./.github/scripts/get_new_version.py --current=$LATEST_TAG ${{ env.BUMP_RULE }})"
echo "::set-output name=date::$(date +'%Y-%m-%d_%H:%M:%S')"
echo "::set-output name=repository_name::$(echo ${{ github.repository }} | sed -e 's/${{ github.repository_owner }}\///')"
# Build and Push Docker image to ECR
- name: Build and Push Docker image to ECR
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ env.AWS_ECR_REGISTRY }}:${{ steps.info.outputs.TAG }},${{ env.AWS_ECR_REGISTRY }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
context: .
file: ./infra/lambda.Dockerfile
platforms: linux/amd64
provenance: false
build-args: |
RELEASE_VERSION=${{ steps.info.outputs.TAG }}
GIT_HASH=${{ github.sha }}
IMAGE_BUILD_DATETIME=${{ steps.info.outputs.date }}
# Create git tag
- name: Create and push git tag
run: |
git tag ${{ steps.info.outputs.TAG }}
git push origin ${{ steps.info.outputs.TAG }}
# Checkout and import zappa config & environment variables from pyconkr-secrets repo
- name: Checkout secrets repo
uses: actions/checkout@v4
with:
repository: ${{ secrets.PYCONKR_SECRET_REPOSITORY }}
ssh-key: ${{ secrets.PYCONKR_SECRET_REPOSITORY_DEPLOY_KEY }}
path: secret_envs
clean: false
sparse-checkout-cone-mode: false
sparse-checkout: |
${{ steps.info.outputs.repository_name }}/zappa_settings.json
${{ steps.info.outputs.repository_name }}/environment_variables.json
- run: mv secret_envs/${{ steps.info.outputs.repository_name }}/*.json ./ && rm -rf secret_envs
# Apply environment variables in environment_variables.json to AWS SSM Parameter Store.
- run: |
python .github/scripts/update_ssm_parameter_store.py \
--project_name ${{ steps.info.outputs.repository_name }} \
--stage ${{ env.API_STAGE }} \
--json_file environment_variables.json
# Zappa update
- name: Zappa Update
run: uv run zappa update ${{ env.API_STAGE }} --docker-image-uri ${{ env.AWS_ECR_REGISTRY }}:${{ steps.info.outputs.TAG }}
- name: Django collect staticfiles
run: uv run zappa manage ${{ env.API_STAGE }} "collectstatic --no-input"
# Django migrate
- name: Django migrate
run: uv run zappa manage ${{ env.API_STAGE }} "migrate --noinput"
# Zappa certify
- name: Zappa Certify
run: uv run zappa certify ${{ env.API_STAGE }} --yes
# Notify to Slack (Success)
- name: Notify deployment to Slack
if: failure() || cancelled()
uses: slackapi/slack-github-action@v1.26.0
with:
channel-id: ${{ vars.SLACK_DEPLOYMENT_ALERT_CHANNEL }}
payload: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ steps.info.outputs.repository_name }} ${{ steps.info.outputs.TAG }} 버전 배포 실패 :rotating_light: (${{ job.status }})",
"emoji": true
}
},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "GitHub Action 바로가기"},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": "${{ github.run_id }}"},
"value": "github_action",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
# Notify to Slack (Failure)
- name: Notify deployment to Slack
uses: slackapi/slack-github-action@v1.26.0
with:
channel-id: ${{ vars.SLACK_DEPLOYMENT_ALERT_CHANNEL }}
payload: |
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ steps.info.outputs.repository_name }} ${{ steps.info.outputs.TAG }} 버전 배포 성공 :tada:",
"emoji": true
}
},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "GitHub Action 바로가기"},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": "${{ github.run_id }}"},
"value": "github_action",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}