Deploy to Server #25
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 Server | |
| permissions: | |
| contents: read | |
| packages: write | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| name: gradle build and set docker image | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v5 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v5 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '21' | |
| - name: Gradle cache | |
| uses: actions/cache@v4 | |
| 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: | | |
| chmod +x gradlew | |
| ./gradlew clean build -x test | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Extract git commit hash | |
| id: version | |
| run: | | |
| echo "version=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6.18.0 | |
| with: | |
| context: . | |
| platforms: linux/amd64 | |
| push: true | |
| tags: | | |
| ghcr.io/simhani1/techmoa:${{ steps.version.outputs.version }} | |
| ghcr.io/simhani1/techmoa:latest | |
| build-args: | | |
| DB_URL=${{ secrets.DB_URL }} | |
| DB_USERNAME=${{ secrets.DB_USERNAME }} | |
| DB_PASSWORD=${{ secrets.DB_PASSWORD }} | |
| CORS_ORIGIN_LOCAL=${{ secrets.CORS_ORIGIN_LOCAL }} | |
| CORS_ORIGIN_DEV=${{ secrets.CORS_ORIGIN_DEV }} | |
| CORS_ORIGIN_PROD=${{ secrets.CORS_ORIGIN_PROD }} | |
| KAKAO_BASE_URL=${{ secrets.KAKAO_BASE_URL }} | |
| KAKAO_CLIENT_ID=${{ secrets.KAKAO_CLIENT_ID }} | |
| KAKAO_CLIENT_SECRET=${{ secrets.KAKAO_CLIENT_SECRET }} | |
| KAKAO_REDIRECT_URI=${{ secrets.KAKAO_REDIRECT_URI }} | |
| JWT_ISSUER=${{ secrets.JWT_ISSUER }} | |
| JWT_SECRET=${{ secrets.JWT_SECRET }} | |
| JWT_ATK_EXPIRATION_SECONDS=${{ secrets.JWT_ATK_EXPIRATION_SECONDS }} | |
| deploy: | |
| needs: build | |
| name: deploy | |
| runs-on: self-hosted | |
| steps: | |
| - name: Deploy | |
| run: | | |
| IMAGE=ghcr.io/simhani1/techmoa:latest | |
| CONTAINER=techmoa-app | |
| NETWORK=monitor-net | |
| docker pull $IMAGE | |
| docker stop $CONTAINER || true | |
| docker rm $CONTAINER || true | |
| docker run -d \ | |
| --name $CONTAINER \ | |
| --network $NETWORK \ | |
| --expose 8080 \ | |
| -e SPRING_PROFILES_ACTIVE=prod \ | |
| -e DB_URL=${{ secrets.DB_URL }} \ | |
| -e DB_USER=${{ secrets.DB_USER }} \ | |
| -e DB_PASSWORD=${{ secrets.DB_PASSWORD }} \ | |
| -e CORS_ORIGIN_LOCAL=${{ secrets.CORS_ORIGIN_LOCAL }} \ | |
| -e CORS_ORIGIN_DEV=${{ secrets.CORS_ORIGIN_DEV }} \ | |
| -e CORS_ORIGIN_PROD=${{ secrets.CORS_ORIGIN_PROD }} \ | |
| -e KAKAO_BASE_URL=${{ secrets.KAKAO_BASE_URL }} \ | |
| -e KAKAO_CLIENT_ID=${{ secrets.KAKAO_CLIENT_ID }} \ | |
| -e KAKAO_CLIENT_SECRET=${{ secrets.KAKAO_CLIENT_SECRET }} \ | |
| -e KAKAO_REDIRECT_URI=${{ secrets.KAKAO_REDIRECT_URI }} \ | |
| -e JWT_ISSUER=${{ secrets.JWT_ISSUER }} \ | |
| -e JWT_SECRET=${{ secrets.JWT_SECRET }} \ | |
| -e JWT_ATK_EXPIRATION_SECONDS=${{ secrets.JWT_ATK_EXPIRATION_SECONDS }} \ | |
| $IMAGE | |
| discord-notification: | |
| needs: deploy | |
| name: discord-notify | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Notify success | |
| if: success() | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: | | |
| ✅ 배포 성공 | |
| Author: ${{ github.actor }} | |
| Repo: ${{ github.repository }} | |
| Commit: ${{ github.sha }} | |
| Image: ghcr.io/simhani1/techmoa:latest | |
| - name: Notify failure | |
| if: failure() | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: | | |
| ❌ 배포 실패 | |
| Author: ${{ github.actor }} | |
| Repo: ${{ github.repository }} | |
| Commit: ${{ github.sha }} | |
| Logs: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |