Update build.gradle #34
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: Java CI with Gradle | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| paths-ignore: | |
| - ".github/**" | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| if: github.event.repository.fork == false | |
| runs-on: ubuntu-latest | |
| permissions: | |
| id-token: write | |
| contents: read | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. | |
| # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md | |
| - name: Setup Gradle | |
| uses: gradle/actions/setup-gradle@v4 | |
| - name: Build with Gradle Wrapper | |
| run: ./gradlew build | |
| # 3. 도커허브 로그인 | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| # 4. 도커 허브에 이미지 푸시 | |
| - name: Push Docker Image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| push: true | |
| context: . | |
| tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest | |
| # 5. GCP 인증 | |
| - name: Authenticate to GCP | |
| id: 'auth' | |
| uses: 'google-github-actions/auth@v2' | |
| with: | |
| workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }} # 생성한 워크로드 아이덴티티 풀의 ID | |
| service_account: ${{ secrets.SERVICE_ACCOUNT_EMAIL }} # IAM 서비스 계정 | |
| # 6. GCP Compute Engine SSH로 배포 | |
| - name: Deploy to GCP Compute Engine | |
| id: 'compute-ssh' | |
| uses: 'google-github-actions/ssh-compute@v1' | |
| with: | |
| instance_name: ${{ secrets.GCP_INSTANCE_NAME }} | |
| zone: ${{ secrets.GCP_INSTANCE_ZONE }} | |
| ssh_private_key: ${{ secrets.GCP_SSH_PRIVATE_KEY }} | |
| command: | | |
| mkdir ./${{ github.repository }} | |
| cd ./${{ github.repository }} | |
| ls -al | |
| sudo curl -o docker-compose.yml https://raw.githubusercontent.com/${{ github.repository }}/main/docker-compose.yml | |
| sudo docker-compose down | |
| sudo docker-compose up -d ${{ secrets.DOCKER_IMAGE_NAME }} | |
| sudo docker image prune -a -f | |
| # Example of using the output | |
| - id: 'debug' | |
| run: |- | |
| echo '${{ steps.compute-ssh.outputs.stdout }}' | |
| echo '${{ steps.compute-ssh.outputs.stderr }}' |