1+ # This workflow uses actions that are not certified by GitHub.
2+ # They are provided by a third-party and are governed by
3+ # separate terms of service, privacy policy, and support
4+ # documentation.
5+ # This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
6+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle
7+
8+ name : Java CI with Gradle
9+
10+ on :
11+ push :
12+ branches : [ "main" ]
13+
14+ jobs :
15+ build :
16+ if : github.event.pull_request.head.repo.full_name == github.repository
17+ if : github.event.repository.fork == false
18+ runs-on : ubuntu-latest
19+ permissions :
20+ id-token : write
21+ contents : read
22+
23+ steps :
24+ - uses : actions/checkout@v4
25+ - name : Set up JDK 21
26+ uses : actions/setup-java@v4
27+ with :
28+ java-version : ' 21'
29+ distribution : ' temurin'
30+
31+ # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies.
32+ # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
33+ - name : Setup Gradle
34+ uses : gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
35+
36+ - name : Build with Gradle Wrapper
37+ run : ./gradlew build
38+
39+ # 3. 도커허브 로그인
40+ - name : Login to DockerHub
41+ uses : docker/login-action@v3
42+ with :
43+ username : ${{ secrets.DOCKER_USERNAME }}
44+ password : ${{ secrets.DOCKER_PASSWORD }}
45+
46+ # 4. 도커 허브에 이미지 푸시
47+ - name : Push Docker Image
48+ uses : docker/build-push-action@v6
49+ with :
50+ push : true
51+ context : .
52+ tags : ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest
53+
54+ # 5. GCP 인증
55+ - name : Authenticate to GCP
56+ id : ' auth'
57+ uses : ' google-github-actions/auth@v2'
58+ with :
59+ workload_identity_provider : ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }} # 생성한 워크로드 아이덴티티 풀의 ID
60+ service_account : ${{ secrets.SERVICE_ACCOUNT_EMAIL }} # IAM 서비스 계정
61+
62+ # 6. GCP Compute Engine SSH로 배포
63+ - name : Deploy to GCP Compute Engine
64+ id : ' compute-ssh'
65+ uses : ' google-github-actions/ssh-compute@v1'
66+ with :
67+ instance_name : ${{ secrets.GCP_INSTANCE_NAME }}
68+ zone : ${{ secrets.GCP_INSTANCE_ZONE }}
69+ ssh_private_key : ${{ secrets.GCP_SSH_PRIVATE_KEY }}
70+ command : |
71+ mkdir ./${{ github.repository }}
72+ cd ./${{ github.repository }}
73+ ls -al
74+ sudo curl -o docker-compose.yml https://raw.githubusercontent.com/${{ github.repository }}/main/docker-compose.yml
75+ sudo docker-compose down
76+ sudo docker-compose up -d ${{ secrets.DOCKER_IMAGE_NAME }}
77+ sudo docker image prune -a -f
78+
79+ # Example of using the output
80+ - id : ' debug'
81+ run : |-
82+ echo '${{ steps.compute-ssh.outputs.stdout }}'
83+ echo '${{ steps.compute-ssh.outputs.stderr }}'
84+
85+
86+ dependency-submission :
87+
88+ runs-on : ubuntu-latest
89+ permissions :
90+ contents : write
91+
92+ steps :
93+ - uses : actions/checkout@v4
94+ - name : Set up JDK 17
95+ uses : actions/setup-java@v4
96+ with :
97+ java-version : ' 21'
98+ distribution : ' temurin'
99+
100+ # Generates and submits a dependency graph, enabling Dependabot Alerts for all project dependencies.
101+ # See: https://github.com/gradle/actions/blob/main/dependency-submission/README.md
102+ - name : Generate and submit dependency graph
103+ uses : gradle/actions/dependency-submission@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
0 commit comments