-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
233 lines (213 loc) · 7.49 KB
/
Jenkinsfile
File metadata and controls
233 lines (213 loc) · 7.49 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
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 = 'springboot'
IMAGE_TAG = "${env.GIT_COMMIT}"
LATEST_TAG = 'backend-latest'
PROD_TAG = 'backend-prod-latest'
/* GitHub Checks */
GH_CHECK_NAME = 'BE 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/backend/values.yaml'
ARGOCD_SERVER = 'argocd.monitory.space' // Argo CD server endpoint
ARGOCD_APPLICATION_NAME = 'backend'
}
stages {
/* 0) 환경 변수 설정 */
stage('Environment Setup') {
steps {
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 {
publishChecks name: GH_CHECK_NAME,
status: 'IN_PROGRESS',
detailsURL: env.BUILD_URL
// Gradle 빌드 환경 변수 설정
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: *BE 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([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'jenkins-access']]) {
sh """
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} \
| docker login --username AWS --password-stdin ${ECR_REGISTRY}
docker build -t ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${LATEST_TAG} -t ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${env.GIT_COMMIT} .
docker push ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${LATEST_TAG}
docker push ${ECR_REGISTRY}/${IMAGE_REPO_NAME}:${env.GIT_COMMIT}
"""
}
}
/* Slack 알림 */
post {
failure {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#ff0000',
message: """:x: *BE develop branch CI 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
}
}
/* 3) develop 전용 ─ Argo CD 배포 */
stage('Deploy (develop only)') {
when {
allOf {
branch 'develop'
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 "ci-bot@monitory.space"
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: *BE develop 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/backend|Argo CD 보기>)
"""
}
failure {
slackSend channel: env.SLACK_CHANNEL,
tokenCredentialId: env.SLACK_CRED_ID,
color: '#ff0000',
message: """:x: *BE develop branch CD 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>) (<https://argocd.monitory.space/applications/argocd/backend|Argo CD 보기>)
"""
}
}
}
/* 4) main 전용 ─ 이미지 빌드 & ECR Push (EC2) */
stage('Docker Build & Push (main only)') {
when {
allOf {
branch 'main'
not { changeRequest() }
}
}
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'jenkins-access']]) {
sh """
aws ecr get-login-password --region ${AWS_DEFAULT_REGION} \
| docker login --username AWS --password-stdin ${ECR_REGISTRY}
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: *BE 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: *BE main branch CI 실패*
파이프라인: <${env.BUILD_URL}|열기>
커밋: `${env.GIT_COMMIT}` – `${env.COMMIT_MSG}`
(<${env.REPO_URL}/commit/${env.GIT_COMMIT}|커밋 보기>)
"""
}
}
}
}
}