Skip to content

CD

CD #64

Workflow file for this run

name: CD
on:
workflow_run:
workflows: ["CI with Gradle"]
types: [completed]
branches: [main]
permissions:
contents: read
env:
IMAGE: ${{ secrets.DOCKER_USERNAME }}/tokbaro
DOMAIN: tokbaro.com
EMAIL: tokbaro.connect@gmail.com
jobs:
deploy:
name: Deploy to Server
runs-on: ubuntu-latest
steps:
- name: Deploy to Server
uses: appleboy/ssh-action@v1.2.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
port: 22
script: |
# 1. 서버에 미리 클론해둔 프로젝트 디렉토리로 이동합니다.
cd /home/${{ secrets.SERVER_USER }}/BackEnd
# 2. 최신 소스 코드를 pull 받습니다.
echo "Pulling latest source code..."
git pull origin main
# 3. GitHub Secret으로부터 .env 파일을 생성합니다.
echo "Creating .env file..."
echo "${{ secrets.ENV_FILE }}" > .env
# 4. docker-compose.yml에 정의된 최신 이미지들을 pull 받습니다.
echo "Pulling latest docker images from registry..."
docker-compose pull
# 5. 인증서 존재 여부 확인 및 발급/갱신 처리
CERT_DIR="/home/${{ secrets.SERVER_USER }}/BackEnd/nginx/letsencrypt/live/${{ env.DOMAIN }}"
if [ -d "$CERT_DIR" ]; then
# --- 인증서가 이미 있는 경우 ---
echo "Certificate found. Starting services and renewing..."
docker-compose up -d --build
docker-compose exec certbot renew --quiet
else
# --- 인증서가 없는 경우 (최초 배포) ---
echo "Certificate not found. Issuing a new one..."
# Certbot 단독 모드로 인증서 발급 (80번 포트를 직접 사용)
docker-compose run --rm certbot certonly --standalone \
--email ${{ env.EMAIL }} -d ${{ env.DOMAIN }} \
--rsa-key-size 4096 --agree-tos --non-interactive
# 모든 서비스를 시작
docker-compose up -d --build
fi
# 6. 불필요한 Docker 이미지를 정리합니다.
echo "Pruning unused docker images..."
docker image prune -f