|
| 1 | +name: Prod CI/CD - Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: |
| 6 | + - published # Release가 published 될 때만 실행 |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 10 | + cancel-in-progress: false |
| 11 | + |
| 12 | +env: |
| 13 | + REGISTRY: docker.io |
| 14 | + IMAGE_NAME: ninecraft0523/ninecraft-server |
| 15 | + |
| 16 | +jobs: |
| 17 | + build-and-push: |
| 18 | + runs-on: ubuntu-24.04 |
| 19 | + timeout-minutes: 25 |
| 20 | + outputs: |
| 21 | + image-digest: ${{ steps.build.outputs.digest }} |
| 22 | + version: ${{ steps.meta.outputs.version }} |
| 23 | + tags: ${{ steps.meta.outputs.tags }} |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Inject application-secret.properties from Secrets |
| 30 | + run: | |
| 31 | + mkdir ./secret |
| 32 | + echo "${{ secrets.DEV_SECRET_PROPERTIES }}" > ./secret/application-dev-secret.properties |
| 33 | + echo "${{ secrets.PROD_SECRET_PROPERTIES }}" > ./secret/application-prod-secret.properties |
| 34 | + echo "${{ secrets.TEST_SECRET_PROPERTIES }}" > ./secret/application-test-secret.properties |
| 35 | + chmod 600 ./secret/* |
| 36 | +
|
| 37 | + - name: Set up JDK 21 |
| 38 | + uses: actions/setup-java@v4 |
| 39 | + with: |
| 40 | + java-version: '21' |
| 41 | + distribution: 'temurin' |
| 42 | + cache: gradle |
| 43 | + |
| 44 | + - name: Setup Gradle |
| 45 | + uses: gradle/actions/setup-gradle@v4 |
| 46 | + with: |
| 47 | + gradle-home-cache-cleanup: true |
| 48 | + |
| 49 | + - name: Grant execute permission for gradlew |
| 50 | + run: chmod +x gradlew |
| 51 | + |
| 52 | + - name: Run full Gradle build with strict validation |
| 53 | + run: ./gradlew build --parallel --build-cache --warning-mode all |
| 54 | + |
| 55 | + - name: Extract metadata for Docker |
| 56 | + id: meta |
| 57 | + uses: docker/metadata-action@v5 |
| 58 | + with: |
| 59 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 60 | + tags: | |
| 61 | + type=semver,pattern={{version}} |
| 62 | + type=raw,value=production-latest |
| 63 | +
|
| 64 | + - name: Set up Docker Buildx |
| 65 | + uses: docker/setup-buildx-action@v3 |
| 66 | + |
| 67 | + - name: Log in to Docker Hub |
| 68 | + uses: docker/login-action@v3 |
| 69 | + with: |
| 70 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 71 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 72 | + |
| 73 | + - name: Build and push Docker image |
| 74 | + id: build |
| 75 | + uses: docker/build-push-action@v6 |
| 76 | + with: |
| 77 | + context: . |
| 78 | + platforms: linux/amd64,linux/arm64 |
| 79 | + push: true |
| 80 | + tags: ${{ steps.meta.outputs.tags }} |
| 81 | + cache-from: type=gha |
| 82 | + cache-to: type=gha,mode=max |
| 83 | + |
| 84 | + deploy-prod: |
| 85 | + needs: build-and-push |
| 86 | + runs-on: ubuntu-24.04 |
| 87 | + timeout-minutes: 20 |
| 88 | + environment: production |
| 89 | + |
| 90 | + steps: |
| 91 | + - name: Deploy to Production Server |
| 92 | + |
| 93 | + with: |
| 94 | + host: ${{ secrets.PROD_HOST }} |
| 95 | + username: ${{ secrets.PROD_USERNAME }} |
| 96 | + key: ${{ secrets.PROD_SSH_KEY }} |
| 97 | + port: ${{ secrets.PROD_PORT }} |
| 98 | + script: | |
| 99 | + cd /opt/app |
| 100 | + export DOCKERHUB_USERNAME="${{ secrets.DOCKERHUB_USERNAME }}" |
| 101 | + export DOCKERHUB_TOKEN="${{ secrets.DOCKERHUB_TOKEN }}" |
| 102 | + export IMAGE_TAG="$(echo "${{ needs.build-and-push.outputs.tags }}" | head -n1)" |
| 103 | + export VERSION_TAG="${{ needs.build-and-push.outputs.version }}" |
| 104 | + export RELEASE_VERSION="${{ github.event.release.tag_name }}" |
| 105 | + cd ~/deploy |
| 106 | + chmod +x ./deploy.sh |
| 107 | + ./deploy.sh |
0 commit comments