CD #54
This file contains hidden or 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: CD | |
| on: | |
| workflow_run: | |
| workflows: ["CI"] | |
| types: [completed] | |
| workflow_dispatch: | |
| inputs: | |
| image_tag: | |
| description: "Docker image tag to deploy (e.g., commit SHA)" | |
| required: false | |
| default: "latest" | |
| concurrency: | |
| group: cd-production | |
| cancel-in-progress: false | |
| jobs: | |
| deploy: | |
| if: > | |
| github.event_name == 'workflow_dispatch' || | |
| ( | |
| github.event_name == 'workflow_run' && | |
| github.event.workflow_run.conclusion == 'success' && | |
| github.event.workflow_run.event == 'push' && | |
| github.event.workflow_run.head_branch == 'main' | |
| ) | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| env: | |
| DEPLOY_PATH: /opt/mohaeng-ai | |
| steps: | |
| - name: Resolve deploy tag | |
| id: vars | |
| shell: bash | |
| env: | |
| EVENT_NAME: ${{ github.event_name }} | |
| INPUT_IMAGE_TAG: ${{ github.event.inputs.image_tag }} | |
| WORKFLOW_RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | |
| run: | | |
| if [ "${EVENT_NAME}" = "workflow_dispatch" ]; then | |
| if ! [[ "${INPUT_IMAGE_TAG}" =~ ^[a-zA-Z0-9._-]+$ ]]; then | |
| echo "::error::Invalid image_tag format: only alphanumeric, dots, hyphens, underscores allowed" | |
| exit 1 | |
| fi | |
| echo "image_tag=${INPUT_IMAGE_TAG}" >> "${GITHUB_OUTPUT}" | |
| else | |
| if ! [[ "${WORKFLOW_RUN_HEAD_SHA}" =~ ^[a-f0-9]{40}$ ]]; then | |
| echo "::error::Invalid workflow_run.head_sha value: ${WORKFLOW_RUN_HEAD_SHA}" | |
| exit 1 | |
| fi | |
| echo "image_tag=${WORKFLOW_RUN_HEAD_SHA}" >> "${GITHUB_OUTPUT}" | |
| fi | |
| - name: Deploy on EC2 (pull only) | |
| uses: appleboy/ssh-action@7eaf76671a0d7eec5d98ee897acda4f968735a17 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| set -e | |
| cd '${{ env.DEPLOY_PATH }}' | |
| export DOCKERHUB_USERNAME='${{ secrets.DOCKERHUB_USERNAME }}' | |
| export IMAGE_TAG='${{ steps.vars.outputs.image_tag }}' | |
| docker compose pull | |
| docker compose up -d --remove-orphans |