-
Notifications
You must be signed in to change notification settings - Fork 3
192 lines (168 loc) · 6.47 KB
/
gradle.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
name: CI/CD Github Action
on:
push:
branches: [ "Master", "Weekly/*" ]
permissions:
contents: read
env:
DATASOURCE_URL : ${{ secrets.ENV_DATASOURCE_URL }}
DATASOURCE_USERNAME : ${{ secrets.ENV_DATASOURCE_USERNAME }}
DATASOURCE_PASSWORD : ${{ secrets.ENV_DATASOURCE_PASSWORD }}
GOOGLE_CLIENT_ID : ${{ secrets.ENV_GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET: ${{ secrets.ENV_GOOGLE_CLIENT_SECRET }}
IMAGE_UPLOAD_PATH : ${{ secrets.ENV_IMAGE_UPLOAD_PATH }}
JWT_SECRET : ${{ secrets.ENV_JWT_SECRET }}
KAKAO_CLIENT_ID : ${{ secrets.ENV_KAKAO_CLIENT_ID }}
KAKAO_CLIENT_SECRET : ${{ secrets.ENV_KAKAO_CLIENT_SECRET }}
KAKAOPAY_SECRET_KEY : ${{ secrets.ENV_KAKAOPAY_SECRET_KEY }}
SERVER_DOMAIN_NAME : ${{ secrets.ENV_SERVER_DOMAIN_NAME }}
jobs:
## 1단계: 프로젝트 빌드
Build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'corretto'
- name: Gradle Caching (for faster build)
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build Application
run: |
chmod +x gradlew
./gradlew clean build -x test
- name: Store build failure reports (execute when build fail)
if: failure()
uses: actions/upload-artifact@v3
with:
name: build-failure-reports
path: |
**/build/reports/
- name: Store build artifacts
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: build/libs/*.jar
## 2단계: 테스트 실행
Test:
runs-on: ubuntu-22.04
needs: Build # Build 단계가 완료되어야 실행됨
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'corretto'
- name: Gradle Caching (for faster build)
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Test Application
run: |
chmod +x gradlew
./gradlew test
- name: Store test failure reports (execute when test fail)
if: failure()
uses: actions/upload-artifact@v3
with:
name: test-failure-reports
path: |
**/build/test-results/
## 3단계: Docker 빌드 및 푸시
Docker-Build:
runs-on: ubuntu-22.04
needs: Test # Test 단계가 성공적으로 완료되어야 실행됨
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Download Build Artifacts
uses: actions/download-artifact@v3
with:
name: build-artifacts
path: build/libs
- name: Docker Hub Login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Docker Build
run: docker build -f Dockerfile --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_FULLNAME }} .
- name: Docker Push
run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }}
## 4단계: 서버에 배포
Deploy:
runs-on: ubuntu-22.04
needs: Docker-Build
if: github.event_name == 'push'
steps:
- name: Update .env
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
script: |
echo "DATASOURCE_URL=${{ secrets.ENV_DATASOURCE_URL }}" > ~/.env
echo "DATASOURCE_USERNAME=${{ secrets.ENV_DATASOURCE_USERNAME }}" >> ~/.env
echo "DATASOURCE_PASSWORD=${{ secrets.ENV_DATASOURCE_PASSWORD }}" >> ~/.env
echo "GOOGLE_CLIENT_ID=${{ secrets.ENV_GOOGLE_CLIENT_ID }}" >> ~/.env
echo "GOOGLE_CLIENT_SECRET=${{ secrets.ENV_GOOGLE_CLIENT_SECRET }}" >> ~/.env
echo "JWT_SECRET=${{ secrets.ENV_JWT_SECRET }}" >> ~/.env
echo "IMAGE_UPLOAD_PATH=${{ secrets.ENV_IMAGE_UPLOAD_PATH }}" >> ~/.env
echo "KAKAO_CLIENT_ID=${{ secrets.ENV_KAKAO_CLIENT_ID }}" >> ~/.env
echo "KAKAO_CLIENT_SECRET=${{ secrets.ENV_KAKAO_CLIENT_SECRET }}" >> ~/.env
echo "KAKAOPAY_SECRET_KEY=${{ secrets.ENV_KAKAOPAY_SECRET_KEY }}" >> ~/.env
echo "SERVER_DOMAIN_NAME=${{ secrets.ENV_SERVER_DOMAIN_NAME }}" >> ~/.env
echo "Environment setup has been completed."
- name: Pull New Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker pull ${{ secrets.DOCKER_REPO_FULLNAME }}
- name: Stop Old Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker stop would-you-in
- name: Run New Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker run --rm -d -p 80:8080 --env-file ~/.env --name would-you-in -v would-you-in-image-volume:${{ secrets.ENV_IMAGE_UPLOAD_PATH }} ${{ secrets.DOCKER_REPO_FULLNAME }}
- name: Clean-Up Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker image prune -f