๐ง config(gha): ์๋งจํฑ ๋ฆด๋ฆฌ์ฆ ์ํฌํ๋ก ์ถ๊ฐ #12
Workflow file for this run
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: EC2 - Deploy (dispatch) | ||
|
Check failure on line 1 in .github/workflows/ec2.yml
|
||
| on: | ||
| repository_dispatch: | ||
| types: [deploy-ec2] | ||
| workflow_dispatch: | ||
| concurrency: | ||
| group: ec2-deploy | ||
| cancel-in-progress: false | ||
| permissions: | ||
| contents: read | ||
| env: | ||
| AWS_REGION: ap-northeast-2 | ||
| # โ ๋ฐฐํฌ ๋์ EC2 ๋ฆฌ์คํธ(JSON ๋ฐฐ์ด). ํ์์ ๋ง๊ฒ ๊ต์ฒดํ์ธ์. | ||
| INSTANCE_IDS_JSON: '["i-08f17d18d83292c07","i-08316767f2bd4a5ed"]' | ||
| # โ ์ฑ/์ปจํ ์ด๋ ์ค์ | ||
| APP_NAME: kickytime # ๊ธฐ์กด ์ปจํ ์ด๋ ๊ธฐ๋ณธ ์ด๋ฆ (์: kickytime) | ||
| CONTAINER_PORT: "8080" # ์ปจํ ์ด๋ ๋ด๋ถ ํฌํธ | ||
| HOST_PORT_BASE: "8080" # ๊ธฐ์กด ์ปจํ ์ด๋๊ฐ ๋ฐ์ธ๋ํ๋ ํธ์คํธ ํฌํธ | ||
| HEALTH_PATH: "/actuator/health" # ํฌ์ค์ฒดํฌ ๊ฒฝ๋ก | ||
| HEALTH_TIMEOUT_SEC: "180" # ํฌ์ค์ฒดํฌ ์ต๋ ๋๊ธฐ์๊ฐ | ||
| ENV_FILE_PATH: "" # ์: /opt/kickytime/.env (์์ผ๋ฉด ๋น ๊ฐ) | ||
| jobs: | ||
| deploy: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| max-parallel: 1 | ||
| matrix: | ||
| instance_id: ${{ fromJson(env.INSTANCE_IDS_JSON) }} | ||
| env: | ||
| PAYLOAD_IMAGE_URI: ${{ github.event.client_payload.image_uri }} | ||
| PAYLOAD_TAG: ${{ github.event.client_payload.tag }} | ||
| PAYLOAD_BRANCH: ${{ github.event.client_payload.branch }} | ||
| PAYLOAD_SHA: ${{ github.event.client_payload.sha }} | ||
| steps: | ||
| - name: Gate - only deploy for main | ||
| id: gate | ||
| run: | | ||
| if [ "${PAYLOAD_BRANCH}" = "main" ]; then | ||
| echo "GO=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "GO=false" >> $GITHUB_OUTPUT | ||
| echo "Non-deploy branch: ${PAYLOAD_BRANCH}" | ||
| fi | ||
| - name: Checkout (same commit as build) | ||
| if: steps.gate.outputs.GO == 'true' | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ env.PAYLOAD_SHA }} | ||
| - name: Configure AWS credentials | ||
| if: steps.gate.outputs.GO == 'true' | ||
| 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: ${{ env.AWS_REGION }} | ||
| - name: Send rolling deploy command via SSM | ||
| if: steps.gate.outputs.GO == 'true' | ||
| id: ssm | ||
| run: | | ||
| set -e | ||
| TARGET_ID="${{ matrix.instance_id }}" | ||
| echo "Deploying to instance: $TARGET_ID" | ||
| # ์ ์ปจํ ์ด๋๊ฐ ๋ฐ์ธ๋ํ ์์ ํฌํธ(ํ์ฌ HOST_PORT_BASE+1) | ||
| NEXT_PORT=$(( ${HOST_PORT_BASE} + 1 )) | ||
| # SSM์ ๋ณด๋ผ ์คํฌ๋ฆฝํธ๋ฅผ JSON ์์ ํ๊ฒ ๋ณํ | ||
| read -r -d '' SCRIPT <<'EOSCRIPT' | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| APP_NAME="${APP_NAME}" | ||
| IMAGE_URI="${PAYLOAD_IMAGE_URI}" | ||
| CONTAINER_PORT="${CONTAINER_PORT}" | ||
| HOST_PORT_BASE="${HOST_PORT_BASE}" | ||
| NEXT_PORT=$(( HOST_PORT_BASE + 1 )) | ||
| HEALTH_PATH="${HEALTH_PATH}" | ||
| HEALTH_TIMEOUT="${HEALTH_TIMEOUT_SEC}" | ||
| ENV_FILE_PATH="${ENV_FILE_PATH}" | ||
| OLD_NAME="${APP_NAME}" | ||
| NEW_NAME="${APP_NAME}_new" | ||
| echo "[1/7] ECR ๋ก๊ทธ์ธ" | ||
| REGISTRY_HOST="$(echo "$IMAGE_URI" | awk -F'/' '{print $1}')" | ||
| aws ecr get-login-password --region "${AWS_REGION}" | docker login --username AWS --password-stdin "${REGISTRY_HOST}" | ||
| echo "[2/7] ์ ์ด๋ฏธ์ง Pull: ${IMAGE_URI}" | ||
| docker pull "${IMAGE_URI}" | ||
| echo "[3/7] ์ด์ NEW ์ปจํ ์ด๋ ์ ๋ฆฌ(์์ผ๋ฉด)" | ||
| if docker ps -a --format '{{.Names}}' | grep -q "^${NEW_NAME}$"; then | ||
| docker rm -f "${NEW_NAME}" || true | ||
| fi | ||
| echo "[4/7] ์ ์ปจํ ์ด๋ ๊ธฐ๋: ${NEW_NAME} (host ${NEXT_PORT} -> container ${CONTAINER_PORT})" | ||
| RUN_ENV_OPTS=() | ||
| if [ -n "${ENV_FILE_PATH}" ]; then | ||
| RUN_ENV_OPTS+=( --env-file "${ENV_FILE_PATH}" ) | ||
| fi | ||
| docker run -d \ | ||
| --name "${NEW_NAME}" \ | ||
| -p "${NEXT_PORT}:${CONTAINER_PORT}" \ | ||
| "${RUN_ENV_OPTS[@]}" \ | ||
| "${IMAGE_URI}" | ||
| echo "[5/7] ํฌ์ค์ฒดํฌ: http://127.0.0.1:${NEXT_PORT}${HEALTH_PATH} (timeout: ${HEALTH_TIMEOUT}s)" | ||
| SECS=0 | ||
| until curl -fsS "http://127.0.0.1:${NEXT_PORT}${HEALTH_PATH}" >/dev/null 2>&1; do | ||
| sleep 3 | ||
| SECS=$(( SECS + 3 )) | ||
| if [ "${SECS}" -ge "${HEALTH_TIMEOUT}" ]; then | ||
| echo "โ ์ ์ปจํ ์ด๋ ํฌ์ค์ฒดํฌ ์คํจ. ๋ก๊ทธ:" | ||
| docker logs --tail 200 "${NEW_NAME}" || true | ||
| exit 1 | ||
| fi | ||
| done | ||
| echo "โ ์ ์ปจํ ์ด๋ Healthy" | ||
| echo "[6/7] ๊ธฐ์กด ์ปจํ ์ด๋ ${OLD_NAME} ์ข ๋ฃ/์ญ์ ๋ฐ ํฌํธ ์ค์์นญ" | ||
| if docker ps -a --format '{{.Names}}' | grep -q "^${OLD_NAME}$"; then | ||
| docker rm -f "${OLD_NAME}" || true | ||
| fi | ||
| # ์ ์ปจํ ์ด๋๋ฅผ ๊ธฐ๋ณธ ์ด๋ฆ์ผ๋ก ์น๊ฒฉ | ||
| docker rename "${NEW_NAME}" "${OLD_NAME}" | ||
| echo "[7/7] ์ฒญ์ (Dangling Images/Containers)" | ||
| docker system prune -f || true | ||
| echo "๐ ๋กค๋ง ์๋ฃ: $(docker ps --filter "name=${OLD_NAME}" --format 'table {{.Names}}\t{{.Image}}\t{{.Ports}}')" | ||
| EOSCRIPT | ||
| # jq๋ก ์ด์ค์ผ์ดํํ์ฌ commands์ ๋ฃ๊ธฐ | ||
| JSON_SCRIPT=$(jq -Rn --arg s "$SCRIPT" '$s') | ||
| CMD_ID=$(aws ssm send-command \ | ||
| --document-name "AWS-RunShellScript" \ | ||
| --instance-ids "$TARGET_ID" \ | ||
| --parameters commands="[$JSON_SCRIPT]" \ | ||
| --comment "Rolling deploy ${APP_NAME} -> ${PAYLOAD_IMAGE_URI}" \ | ||
| --timeout-seconds 1800 \ | ||
| --query "Command.CommandId" \ | ||
| --output text) | ||
| echo "SSM CommandId: ${CMD_ID}" | ||
| aws ssm wait command-executed --command-id "$CMD_ID" --instance-id "$TARGET_ID" | ||
| # ์คํจ ์ ๋ก๊ทธ ์ถ๋ ฅ | ||
| STATUS=$(aws ssm list-command-invocations --command-id "$CMD_ID" --instance-id "$TARGET_ID" --details \ | ||
| --query "CommandInvocations[0].Status" --output text) | ||
| echo "SSM Status: $STATUS" | ||
| if [ "$STATUS" != "Success" ]; then | ||
| echo "---- SSM Output ----" | ||
| aws ssm list-command-invocations --command-id "$CMD_ID" --instance-id "$TARGET_ID" --details \ | ||
| --query "CommandInvocations[0].CommandPlugins[0].{StdOut:Output,StdErr:StandardErrorContent}" --output json | ||
| exit 1 | ||
| fi | ||
| - name: Summary | ||
| if: steps.gate.outputs.GO == 'true' | ||
| run: | | ||
| echo "### โ EC2 Rolling Deploy Completed" >> $GITHUB_STEP_SUMMARY | ||
| echo "- Instance: \`${{ matrix.instance_id }}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "- Image: \`${PAYLOAD_IMAGE_URI}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "- Branch: \`${PAYLOAD_BRANCH}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "- Commit: \`${PAYLOAD_SHA}\`" >> $GITHUB_STEP_SUMMARY | ||