-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
176 lines (170 loc) · 5.67 KB
/
Jenkinsfile
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
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS') // set timeout 1 hour
}
environment {
TIME_ZONE = 'Asia/Seoul'
PROFILE = 'local'
REPOSITORY_CREDENTIAL_ID = 'gitlab-jenkins-key'
REPOSITORY_URL = '[email protected]:swm-13-main/13_swm56/belloga-auth-service.git'
TARGET_BRANCH = 'master'
CONTAINER_NAME = 'belloga-auth-service'
SWAGGER_ID = 'belloga-swagger'
OPEN_API_SPEC_NAME = 'open-api-3-auth-service.json'
AWS_CREDENTIAL_NAME = 'belloga-aws'
ECR_PATH = '023778162658.dkr.ecr.ap-northeast-2.amazonaws.com'
IMAGE_NAME = '023778162658.dkr.ecr.ap-northeast-2.amazonaws.com/belloga-auth-service'
REGION = 'ap-northeast-2'
}
stages{
stage('init') {
steps {
echo 'init stage'
deleteDir()
}
post {
success {
echo 'success init in pipeline'
}
failure {
error 'fail init in pipeline'
}
}
}
stage('clone project') {
steps {
git url: "$REPOSITORY_URL",
branch: "$TARGET_BRANCH",
credentialsId: "$REPOSITORY_CREDENTIAL_ID"
sh "ls -al"
}
post {
success {
echo 'success clone project'
}
failure {
error 'fail clone project' // exit pipeline
}
}
}
stage('create application-prod') {
steps {
dir('src/main/resources') {
withAWSParameterStore(credentialsId: "${env.AWS_CREDENTIAL_NAME}",
path: "/belloga/auth/application/profile",
naming: 'basename',
regionName: 'ap-northeast-2') {
writeFile file: 'application-prod.yml', text: "${env.PROD}"
}
}
}
post {
success {
echo 'success create application-prod'
}
failure {
error 'fail create application-prod'
}
}
}
stage('build project') {
steps {
sh '''
./gradlew bootJar
'''
}
post {
success {
echo 'success build project'
}
failure {
error 'fail build project' // exit pipeline
}
}
}
stage('generate api docs by spring rest docs and send to swagger ui') {
steps {
sh '''
./gradlew openapi3
'''
// swagger UI로 보내는 로직
dir('src/main/resources/static/docs') {
sshagent (credentials: ["$SWAGGER_ID"]) { // use SSH Agent
sh """
ssh -o StrictHostKeyChecking=no [email protected] '
sudo docker container ls
'
"""
sh 'scp ./$OPEN_API_SPEC_NAME [email protected]:/home/ubuntu/docs' //지정 경로로 파일 이동시켜 문서 변경
}
}
}
post {
success {
echo 'success generate api docs'
}
failure {
error 'fail generate api docs' // exit pipeline
}
}
}
// 도커 이미지를 만든다. build number로 태그를 주되 latest 태그도 부여한다.
stage('dockerizing project') {
steps {
sh '''
docker build -t $IMAGE_NAME:$BUILD_NUMBER .
docker tag $IMAGE_NAME:$BUILD_NUMBER $IMAGE_NAME:latest
'''
}
post {
success {
echo 'success dockerizing project'
}
failure {
error 'fail dockerizing project' // exit pipeline
}
}
}
// 빌드넘버 태그와 latest 태그 둘 다 올린다.
stage('upload aws ECR') {
steps {
script{
// cleanup current user docker credentials
sh 'rm -f ~/.dockercfg ~/.docker/config.json || true'
docker.withRegistry("https://${ECR_PATH}", "ecr:${REGION}:${AWS_CREDENTIAL_NAME}") {
docker.image("${IMAGE_NAME}:${BUILD_NUMBER}").push()
docker.image("${IMAGE_NAME}:latest").push()
}
}
}
post {
success {
echo 'success upload image'
}
failure {
error 'fail upload image' // exit pipeline
}
}
}
stage('kubectl apply') {
steps {
sshagent (credentials: ['bestion-ssh']) { // use SSH Agent
sh """
ssh -o StrictHostKeyChecking=no [email protected] '
kubectl set image deploy auth-service-v1 auth-service=${IMAGE_NAME}:${BUILD_NUMBER}
'
"""
}
}
post {
success {
echo 'success kubectl rolling update'
}
failure {
error 'fail kubectl rolling update' // exit pipeline
}
}
}
}
}