Hotfix : ci cd 파일 수정 #182
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 | |
| on: | |
| push: | |
| branches: ["main"] | |
| permissions: | |
| contents: read | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # -------------------------- | |
| # 1) Checkout | |
| # -------------------------- | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| # -------------------------- | |
| # 2) JDK 설정 | |
| # -------------------------- | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # -------------------------- | |
| # 3) Secrets 정제 (CR/LF 제거) | |
| # -------------------------- | |
| - name: Sanitize secrets | |
| id: sanitize | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| SSH_HOST_CLEAN="$(printf '%s' "${{ secrets.SSH_HOST }}" | tr -d '\r\n' | sed -E 's#^https?://##')" | |
| OCIR_HOST_CLEAN="$(printf '%s' "${{ secrets.OCIR_HOST }}" | tr -d '\r\n')" | |
| OCIR_NS_CLEAN="$(printf '%s' "${{ secrets.OCIR_TENANCY_NAMESPACE }}" | tr -d '\r\n')" | |
| OCIR_REPO_CLEAN="$(printf '%s' "${{ secrets.OCIR_REPO }}" | tr -d '\r\n')" | |
| OCIR_USER_CLEAN="$(printf '%s' "${{ secrets.OCIR_USERNAME }}" | tr -d '\r\n')" | |
| if [[ -z "$SSH_HOST_CLEAN" || "$SSH_HOST_CLEAN" == *"/"* ]]; then | |
| echo "SSH_HOST 값이 잘못되었습니다: $SSH_HOST_CLEAN" >&2 | |
| exit 1 | |
| fi | |
| if [[ -z "$OCIR_HOST_CLEAN" || -z "$OCIR_NS_CLEAN" || -z "$OCIR_REPO_CLEAN" || -z "$OCIR_USER_CLEAN" ]]; then | |
| echo "OCIR secrets 중 하나가 비어 있습니다" >&2 | |
| exit 1 | |
| fi | |
| echo "ssh_host=$SSH_HOST_CLEAN" >> "$GITHUB_OUTPUT" | |
| echo "ocir_host=$OCIR_HOST_CLEAN" >> "$GITHUB_OUTPUT" | |
| echo "ocir_ns=$OCIR_NS_CLEAN" >> "$GITHUB_OUTPUT" | |
| echo "ocir_repo=$OCIR_REPO_CLEAN" >> "$GITHUB_OUTPUT" | |
| echo "ocir_user=$OCIR_USER_CLEAN" >> "$GITHUB_OUTPUT" | |
| # -------------------------- | |
| # 4) 빌드 전 리소스 파일 생성 | |
| # -------------------------- | |
| - name: Create FCM key file | |
| run: | | |
| mkdir -p src/main/resources | |
| echo '${{ secrets.FCM_SERVICE_ACCOUNT }}' > src/main/resources/firebase-adminsdk.json | |
| - name: Create .env file | |
| shell: bash | |
| run: printf "%s" "${{ secrets.ENV_FILE }}" > .env | |
| # -------------------------- | |
| # 5) Gradle 빌드 (JAR) | |
| # -------------------------- | |
| - name: Build JAR | |
| run: | | |
| chmod +x ./gradlew | |
| ./gradlew clean bootJar -x test | |
| JAR=$(ls build/libs/*.jar | grep -v plain | head -n 1) | |
| cp "$JAR" app.jar | |
| echo "빌드된 JAR: $JAR" | |
| # -------------------------- | |
| # 6) OCIR 이미지 이름 계산 | |
| # -------------------------- | |
| - name: Compute image name | |
| id: img | |
| run: | | |
| IMAGE="${{ steps.sanitize.outputs.ocir_host }}/${{ steps.sanitize.outputs.ocir_ns }}/${{ steps.sanitize.outputs.ocir_repo }}" | |
| echo "name=$IMAGE" >> "$GITHUB_OUTPUT" | |
| echo "full=$IMAGE:latest" >> "$GITHUB_OUTPUT" | |
| # -------------------------- | |
| # 7) Docker 빌드 & OCIR 푸시 | |
| # -------------------------- | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to OCIR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ steps.sanitize.outputs.ocir_host }} | |
| username: ${{ steps.sanitize.outputs.ocir_user }} | |
| password: ${{ secrets.OCIR_AUTH_TOKEN }} | |
| - name: Build and push to OCIR | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| tags: | | |
| ${{ steps.img.outputs.name }}:latest | |
| ${{ steps.img.outputs.name }}:${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| # -------------------------- | |
| # 8) 서버로 파일 전송 (SCP) | |
| # -------------------------- | |
| - name: Copy files to OCI server | |
| uses: appleboy/scp-action@master | |
| with: | |
| host: ${{ steps.sanitize.outputs.ssh_host }} | |
| username: ${{ secrets.SSH_USERNAME }} | |
| key: ${{ secrets.SSH_KEY }} | |
| port: 22 | |
| source: ".env,docker-compose.yml,nginx/" | |
| target: "/home/ubuntu/tinybite/" | |
| # -------------------------- | |
| # 9) OCI 서버 배포 (SSH) | |
| # -------------------------- | |
| - name: Deploy to OCI server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ steps.sanitize.outputs.ssh_host }} | |
| username: ${{ secrets.SSH_USERNAME }} | |
| key: ${{ secrets.SSH_KEY }} | |
| port: 22 | |
| script: | | |
| set -e | |
| cd /home/ubuntu/tinybite | |
| # OCIR 로그인 | |
| echo "${{ secrets.OCIR_AUTH_TOKEN }}" \ | |
| | docker login "${{ steps.sanitize.outputs.ocir_host }}" \ | |
| -u "${{ steps.sanitize.outputs.ocir_user }}" \ | |
| --password-stdin | |
| # 최신 이미지 pull | |
| export APP_IMAGE="${{ steps.img.outputs.full }}" | |
| docker compose pull | |
| # 무중단: 새 컨테이너 먼저 올리고 기존 종료 | |
| docker compose up -d --remove-orphans | |
| # 사용하지 않는 이미지 정리 | |
| docker image prune -f | |
| echo "✅ 배포 완료: $APP_IMAGE" |