Fix: [main] cicd 할때 md 파일 변화는 제외
All checks were successful
baekjoon-bot-cicd / build_push_deploy (push) Successful in 10m53s

This commit is contained in:
sm4640
2026-03-26 00:42:35 +09:00
parent bd045b43c2
commit 0076eefda2

View File

@@ -4,23 +4,46 @@ on:
push: push:
branches: ["main"] branches: ["main"]
paths-ignore: paths-ignore:
- "**.md" - "**/*.md"
- "docs/**"
jobs: jobs:
build_push_deploy: build_push_deploy:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Manual checkout (supports /gitea subpath) - name: Setup SSH for Gitea
env: env:
ACTOR: ${{ github.actor }} SSH_PRIVATE_KEY: ${{ secrets.NKEY_SSH_PRIVATE_KEY }}
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
TOKEN: ${{ secrets.NKEY_PAT }}
run: | run: |
set -euo pipefail set -euo pipefail
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -p 2222 -t rsa,ed25519 nkeystudy.site >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
cat >> ~/.ssh/config <<'EOF'
Host nkey-gitea
HostName nkeystudy.site
User git
Port 2222
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
EOF
chmod 600 ~/.ssh/config
- name: Manual checkout via SSH
env:
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
run: |
set -euo pipefail
git init . git init .
git remote add origin "https://${ACTOR}:${TOKEN}@nkeystudy.site/gitea/${REPO}.git" git remote add origin "nkey-gitea:${REPO}.git"
git fetch --no-tags --prune --depth=1 origin "${SHA}" git fetch --no-tags --prune --depth=1 origin "${SHA}"
git checkout -q FETCH_HEAD git checkout -q FETCH_HEAD
@@ -29,8 +52,8 @@ jobs:
set -euo pipefail set -euo pipefail
docker version docker version
if ! docker compose version >/dev/null 2>&1; then if ! docker compose version >/dev/null 2>&1; then
# docker:24-git(Alpine) 기준 sudo apt-get update
apk add --no-cache docker-cli-compose sudo apt-get install -y docker-compose-plugin
fi fi
docker compose version docker compose version
@@ -42,41 +65,74 @@ jobs:
set -euo pipefail set -euo pipefail
echo "${DOCKERHUB_TOKEN}" | docker login -u "${DOCKERHUB_USER}" --password-stdin echo "${DOCKERHUB_TOKEN}" | docker login -u "${DOCKERHUB_USER}" --password-stdin
- name: Build & push image - name: Extract image version from commit message
id: version
env:
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
VERSION_TAG=""
if printf '%s' "${COMMIT_MESSAGE}" | grep -Eq '\[[0-9]+\.[0-9]+\.[0-9]+\]'; then
VERSION_TAG="$(printf '%s' "${COMMIT_MESSAGE}" | sed -nE 's/.*\[([0-9]+\.[0-9]+\.[0-9]+)\].*/\1/p' | head -n1)"
fi
echo "version_tag=${VERSION_TAG}" >> "$GITHUB_OUTPUT"
- name: Build and push image
env: env:
DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USERNAME }} DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USERNAME }}
IMAGE_NAME: baekjoon-bot IMAGE_NAME: baekjoon-bot
VERSION_TAG: ${{ steps.version.outputs.version_tag }}
run: | run: |
set -euo pipefail set -euo pipefail
IMAGE="${DOCKERHUB_USER}/${IMAGE_NAME}:latest" IMAGE="${DOCKERHUB_USER}/${IMAGE_NAME}:latest"
if [ -n "${VERSION_TAG}" ]; then
VERSIONED_IMAGE="${DOCKERHUB_USER}/${IMAGE_NAME}:${VERSION_TAG}"
docker build -t "${IMAGE}" -t "${VERSIONED_IMAGE}" .
docker push "${VERSIONED_IMAGE}"
else
docker build -t "${IMAGE}" . docker build -t "${IMAGE}" .
fi
docker push "${IMAGE}" docker push "${IMAGE}"
- name: Deploy on server (compose pull/up) - name: Deploy on server (compose pull/up)
run: | run: |
docker compose -f /nkeysworld/compose.apps.yml pull baekjoon-bot set -euo pipefail
docker compose -f /nkeysworld/compose.apps.yml up -d baekjoon-bot docker compose -p nkeys-apps -f /nkeysworld/compose.apps.yml pull baekjoon-bot
docker compose -p nkeys-apps -f /nkeysworld/compose.apps.yml up -d baekjoon-bot
docker image prune -f docker image prune -f
- name: Discord Notification - name: Discord Notification
if: always() # 빌드 성공/실패 여부와 상관없이 항상 실행 if: always()
env: env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
run: | run: |
set -euo pipefail
if [ "${{ job.status }}" = "success" ]; then if [ "${{ job.status }}" = "success" ]; then
MESSAGE="🚀 **Build & Deploy Success!**" STATUS="SUCCESS"
COLOR=3066993 # 녹색 계열 COLOR=3066993
DESC="Baekjoon bot build/push/deploy succeeded."
else else
MESSAGE="⚠️ **Build or Deploy Failed!**" STATUS="FAILURE"
COLOR=15158332 # 빨간색 계열 COLOR=15158332
DESC="Baekjoon bot build or deploy failed."
fi fi
curl -X POST -H "Content-Type: application/json" \ curl -X POST -H "Content-Type: application/json" \
-d '{ -d '{
"embeds": [{ "embeds": [{
"title": "'"$MESSAGE"'", "title": "Baekjoon Bot CI/CD - '"$STATUS"'",
"description": "**Repo:** ${{ github.repository }}\n**Commit:** ${{ github.sha }}\n**Actor:** ${{ github.actor }}", "description": "'"$DESC"'",
"fields": [
{ "name": "Repo", "value": "${{ github.repository }}", "inline": true },
{ "name": "Commit", "value": "`${{ github.sha }}`", "inline": true },
{ "name": "Actor", "value": "${{ github.actor }}", "inline": true },
{ "name": "Image Version", "value": "`${{ steps.version.outputs.version_tag || 'latest only' }}`", "inline": true }
],
"color": '"$COLOR"', "color": '"$COLOR"',
"timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
}] }]
}' $DISCORD_WEBHOOK }' "${DISCORD_WEBHOOK}"