forked from Fac2Real/monitory-data-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
285 lines (261 loc) · 9.54 KB
/
Jenkinsfile
File metadata and controls
285 lines (261 loc) · 9.54 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
pipeline {
agent any
environment {
/* AWS & ECR */
AWS_DEFAULT_REGION = 'ap-northeast-2'
ECR_REGISTRY = '853660505909.dkr.ecr.ap-northeast-2.amazonaws.com'
IMAGE_REPO_NAME = 'flink'
IMAGE_TAG = "${env.GIT_COMMIT}"
LATEST_TAG = 'flink-latest'
PROD_TAG = 'flink-prod-latest'
/* GitHub Checks */
GH_CHECK_NAME = 'Flink Build Test'
/* Slack */
SLACK_CHANNEL = '#ci-cd'
SLACK_CRED_ID = 'slack-factoreal-token' // Slack App OAuth Token
/* Argo CD */
HELM_VALUES_PATH = 'monitory-helm-charts/flink/values.yaml'
ARGOCD_SERVER = 'argocd.monitory.space' // Argo CD server endpoint
ARGOCD_APPLICATION_NAME = 'flink'
}
stages {
/* 0) 환경 변수 설정 */
stage('Environment, Config Setup') {
steps {
withCredentials([
file (credentialsId: 'flink-root-pem', variable: 'ROOT_PEM'),
file (credentialsId: 'flink-priv-key', variable: 'PRIV_KEY'),
file (credentialsId: 'flink-cert-pem', variable: 'CERT_PEM')
]) {
sh '''
sudo mkdir -p src/main/resources/certs
sudo chmod 755 src/main/resources/certs
sudo cp "$ROOT_PEM" src/main/resources/certs/root.pem
sudo chmod 644 src/main/resources/certs/root.pem
sudo cp "$PRIV_KEY" src/main/resources/certs/private.pem.key
sudo chmod 644 src/main/resources/certs/private.pem.key
sudo cp "$CERT_PEM" src/main/resources/certs/certificate.pem.crt
sudo chmod 644 src/main/resources/certs/certificate.pem.crt
'''
}
script {
def rawUrl = sh(script: "git config --get remote.origin.url",
returnStdout: true).trim()
env.REPO_URL = rawUrl.replaceAll(/\.git$/, '')
env.COMMIT_MSG = sh(script: "git log -1 --pretty=format:'%s'",returnStdout: true).trim()
}
}
}
/* 1) 공통 테스트 */
stage('Test') {
when {
allOf {
not { branch 'develop' }
not { branch 'main' }
not { changeRequest() }
}
}
steps {
withCredentials([
file (credentialsId: 'flink-properties-ec2', variable: 'APP_PROPS')
]) {
sh '''
sudo cp "$APP_PROPS" src/main/resources/application.properties
sudo chmod 644 src/main/resources/application.properties
'''
}
publishChecks name: GH_CHECK_NAME,
status: 'IN_PROGRESS',
detailsURL: env.BUILD_URL
withCredentials([ file(credentialsId: 'backend-env', variable: 'ENV_FILE') ]) {
sh '''
set -o allexport
source "$ENV_FILE"
set +o allexport
./gradlew test --no-daemon
'''
}
}
post {
success {
publishChecks name: GH_CHECK_NAME,
conclusion: 'SUCCESS',
detailsURL: env.BUILD_URL
}
failure {
publishChecks name: GH_CHECK_NAME,
conclusion: 'FAILURE',
detailsURL: "${env.BUILD_URL}console"
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#ff0000',
message: """:x: *Flink Test 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
}
}
/* 2) develop 전용 ─ Docker 이미지 빌드 & ECR Push */
stage('Docker Build & Push (develop only)') {
when {
allOf {
branch 'develop'
not { changeRequest() }
}
}
steps {
withCredentials([
file (credentialsId: 'flink-properties-ec2', variable: 'APP_PROPS')
]) {
sh '''
sudo cp "$APP_PROPS" src/main/resources/application.properties
sudo chmod 644 src/main/resources/application.properties
'''
}
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'jenkins-access']]) {
sh """
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
./gradlew build --no-daemon -x test
docker build -t ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${LATEST_TAG} .
docker push ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${LATEST_TAG}
"""
}
}
/* Slack 알림 */
post {
success {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#36a64f',
message: """:white_check_mark: *Flink develop branch CI 성공*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
failure {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#ff0000',
message: """:x: *Flink develop branch CI 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
}
}
/* 3) main 전용 ─ Docker 이미지 빌드 & ECR Push */
stage('Docker Build & Push (main only)') {
when {
allOf {
branch 'main'
not { changeRequest() }
}
}
steps {
withCredentials([
file (credentialsId: 'flink-properties-k8s', variable: 'APP_PROPS')
]) {
sh '''
sudo cp "$APP_PROPS" src/main/resources/application.properties
sudo chmod 644 src/main/resources/application.properties
'''
}
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'jenkins-access']]) {
sh """
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
./gradlew build --no-daemon -x test
docker build -t ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${PROD_TAG} -t ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${env.GIT_COMMIT} .
docker push ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${PROD_TAG}
docker push ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${env.GIT_COMMIT}
"""
}
}
/* Slack 알림 */
post {
success {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#36a64f',
message: """:white_check_mark: *Flink main branch CI 성공*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
failure {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#ff0000',
message: """:x: *Flink main branch CI 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
}
}
/* 4) main 전용 ─ Argo CD 배포 */
stage('Deploy (main only)') {
when {
allOf {
branch 'main'
not { changeRequest() }
}
}
steps {
withCredentials([string(credentialsId: 'monitory-iac-github-token', variable: 'GIT_TOKEN')]){
sh """
git clone https://${GIT_TOKEN}@github.com/Fac2Real/monitory-iac.git ${env.GIT_COMMIT}
cd ${env.GIT_COMMIT}
git checkout deploy
git fetch origin main
git merge --no-ff origin/main -m "ci: merge main → deploy"
yq -i ".image.tag = \\"${env.GIT_COMMIT}\\"" ${HELM_VALUES_PATH}
git config user.name "ci-bot"
git config user.email "[email protected]"
git add ${HELM_VALUES_PATH}
git commit -m "ci(${ARGOCD_APPLICATION_NAME}): bump image to ${env.GIT_COMMIT})"
git push https://${GIT_TOKEN}@github.com/Fac2Real/monitory-iac.git deploy
"""
}
withCredentials([string(credentialsId: 'argo-jenkins-token', variable: 'ARGOCD_AUTH_TOKEN')]) {
sh '''
argocd --server $ARGOCD_SERVER --insecure --grpc-web \
app sync $ARGOCD_APPLICATION_NAME
argocd --server $ARGOCD_SERVER --insecure --grpc-web \
app wait $ARGOCD_APPLICATION_NAME --health --timeout 300
'''
}
}
/* Slack 알림 */
post {
success {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#36a64f',
message: """:white_check_mark: *Flink main branch CD 성공*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>) (<https://argocd.monitory.space/applications/argocd/flink|Argo CD 보기>)
"""
}
failure {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#ff0000',
message: """:x: *Flink main branch CI/CD 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>) (<https://argocd.monitory.space/applications/argocd/flink|Argo CD 보기>)
"""
}
}
}
}
}