fix: 스크립트 수정 #3
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: Deploy to OCI (Docker Hub) | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - dev | |
| workflow_dispatch: | |
| env: | |
| PROJECT_NAME: madii-server | |
| DOCKER_REPO: 9uttery # ← Docker Hub 유저명 하드코딩 | |
| IMAGE_NAME: 9uttery/madii-server # ← 풀 이미지 이름 | |
| jobs: | |
| build_and_push: | |
| name: Build and Push to Docker Hub | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: 17 | |
| distribution: temurin | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| # ===== application-*.yml / Firebase 키 생성 (기존 로직 그대로) ===== | |
| - name: Make application-secret.yml | |
| run: echo "${{ secrets.APPLICATION_SECRET_BASE64 }}" | base64 --decode > ./src/main/resources/application-secret.yml | |
| - name: Make application-dev.yml | |
| run: echo "${{ secrets.APPLICATION_DEV_BASE64 }}" | base64 --decode > ./src/main/resources/application-dev.yml | |
| - name: Make application-prod.yml | |
| run: echo "${{ secrets.APPLICATION_PROD_BASE64 }}" | base64 --decode > ./src/main/resources/application-prod.yml | |
| - name: Create Firebase Admin SDK JSON | |
| uses: jsdaniell/[email protected] | |
| with: | |
| name: "madii-app-firebase-adminsdk-uriyk-c04677456f.json" | |
| json: ${{ secrets.FIREBASE_KEY_JSON }} | |
| dir: "src/main/resources/" | |
| # ===== Gradle 캐시 & 빌드 ===== | |
| - name: Gradle Caching | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| - name: Build with Gradle | |
| run: ./gradlew build | |
| # ===== Docker Hub 로그인 ===== | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: 9uttery # ← 하드코딩 | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} # ← Secret 하나만 준비하면 됨 | |
| # ===== Docker 이미지 빌드 & 푸시 ===== | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| tags: | | |
| ${{ env.IMAGE_NAME }}:${{ github.sha }} | |
| ${{ env.IMAGE_NAME }}:latest | |
| cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:latest | |
| cache-to: type=inline | |
| deploy: | |
| name: Deploy to OCI server | |
| needs: build_and_push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Connect SSH & Deploy | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.OCI_HOST }} # 예: 158.180.89.26 | |
| username: ${{ secrets.OCI_SSH_USER }} # 예: ubuntu | |
| key: ${{ secrets.OCI_SSH_KEY }} # 로컬에서 쓰는 키 파일 내용 | |
| port: 22 | |
| script: | | |
| set -e | |
| IMAGE="${{ env.IMAGE_NAME }}:latest" | |
| CONTAINER_NAME="${{ env.PROJECT_NAME }}" | |
| echo "Deploying image: $IMAGE" | |
| # 1) Docker 없으면 설치 (서버에 따로 준비 안 해도 되게) | |
| if ! command -v docker >/dev/null 2>&1; then | |
| echo "Docker not found. Installing..." | |
| curl -fsSL https://get.docker.com | sudo sh | |
| fi | |
| # 2) Docker Hub 로그인 | |
| echo "${{ secrets.DOCKERHUB_TOKEN }}" | sudo docker login -u "9uttery" --password-stdin | |
| # 3) 최신 이미지 pull | |
| sudo docker pull "$IMAGE" | |
| # 4) 기존 컨테이너 정리 | |
| if sudo docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then | |
| echo "Stopping old container..." | |
| sudo docker stop "$CONTAINER_NAME" || true | |
| sudo docker rm "$CONTAINER_NAME" || true | |
| fi | |
| # 5) 새 컨테이너 실행 | |
| # - 포트 8080 기준 (이전 NCP 스크립트와 동일하게) | |
| # - 프로필은 main/dev 공통으로 prod. dev 나누고 싶으면 여기 조건 분기 추가 가능 | |
| sudo docker run -d \ | |
| --name "$CONTAINER_NAME" \ | |
| --restart unless-stopped \ | |
| -p 8080:8080 \ | |
| -e SPRING_PROFILES_ACTIVE=prod \ | |
| "$IMAGE" | |
| # 6) 오래된 dangling 이미지 정리 | |
| sudo docker image prune -f | |
| echo "Deploy finished." |