forked from SWM-304/TeamPlanner-BE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile:v1
201 lines (158 loc) · 7.66 KB
/
Jenkinsfile:v1
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
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS') // set timeout 1 hour
}
environment {
TIME_ZONE = 'Asia/Seoul'
//github
TARGET_BRANCH = 'develop'
REPOSITORY_URL= 'https://github.com/SWM-304/TeamPlanner-BE'
//docker-hub
registryCredential = 'docker-hub'
//aws ecr
CONTAINER_NAME = 'teamplanner-backend-container'
AWS_CREDENTIAL_NAME = 'AWS_ECR'
ECR_PATH = '129715120090.dkr.ecr.ap-northeast-2.amazonaws.com'
IMAGE_NAME = '129715120090.dkr.ecr.ap-northeast-2.amazonaws.com/teamplanner-backendserver'
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('Prepare') {
steps {
echo 'Cloning Repository'
git branch: 'develop',
credentialsId: 'repo-and-hook-access-token-credentials',
url: 'https://github.com/SWM-304/TeamPlanner-BE'
}
post {
success {
echo 'Successfully Cloned Repository'
}
failure {
error 'This pipeline stops here...'
}
}
}
// 일단은 테스트없이 빌드
stage('Build Gradle') {
steps {
echo 'Build Gradle'
dir('.'){
sh '''
pwd
cd /var/jenkins_home/workspace/teamPlannerBackEnd_jenkinsFile
chmod +x ./gradlew
./gradlew build --exclude-task test
'''
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
// 도커 이미지를 만든다. build number로 태그를 주되 latest 태그도 부여한다.
stage('Build Docker') {
steps {
echo 'Build Docker'
sh """
cd /var/jenkins_home/workspace/teamPlannerBackEnd_jenkinsFile
docker builder prune
docker build -t $IMAGE_NAME:$BUILD_NUMBER .
docker tag $IMAGE_NAME:$BUILD_NUMBER $IMAGE_NAME:latest
"""
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
// 빌드넘버 태그와 latest 태그 둘 다 올린다.
stage('Push Docker') {
steps {
echo 'Push Docker'
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 {
failure {
error 'This pipeline stops here...'
}
}
}
// aws cli 로그인 -> 및 ecr pull -> origin container delete -> docker compose down -> docker compose up -> origin image delete
stage('Docker Run') {
steps {
echo 'Pull Docker Image & Docker Image Run'
sshagent(credentials: ['ssh']) {
script {
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'sudo chmod 666 /var/run/docker.sock'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 129715120090.dkr.ecr.ap-northeast-2.amazonaws.com'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'docker compose down'"
// Delete existing Docker image
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'sudo docker rmi -f 129715120090.dkr.ecr.ap-northeast-2.amazonaws.com/teamplanner-backendserver:latest'"
docker.withRegistry("https://${ECR_PATH}", "ecr:${REGION}:${AWS_CREDENTIAL_NAME}") {
docker.image("${IMAGE_NAME}:${BUILD_NUMBER}").pull()
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'sudo docker ps -q --filter name=${CONTAINER_NAME}'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'containers=\$(sudo docker ps -aq --filter name=${CONTAINER_NAME}); if [ -n \"\$containers\" ]; then sudo docker rm -f \$containers; fi'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'docker compose up -d'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.3.222 'images=\$(sudo docker images -q -f dangling=true); if [ -n \"\$images\" ]; then sudo docker rmi -f \$images; fi'"
}
}
script {
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'sudo chmod 666 /var/run/docker.sock'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 129715120090.dkr.ecr.ap-northeast-2.amazonaws.com'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'docker compose down'"
// Delete existing Docker image
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'sudo docker rmi -f 129715120090.dkr.ecr.ap-northeast-2.amazonaws.com/teamplanner-backendserver:latest'"
docker.withRegistry("https://${ECR_PATH}", "ecr:${REGION}:${AWS_CREDENTIAL_NAME}") {
docker.image("${IMAGE_NAME}:${BUILD_NUMBER}").pull()
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'sudo docker ps -q --filter name=${CONTAINER_NAME}'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'containers=\$(sudo docker ps -aq --filter name=${CONTAINER_NAME}); if [ -n \"\$containers\" ]; then sudo docker rm -f \$containers; fi'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'docker compose up -d'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@10.1.4.24 'images=\$(sudo docker images -q -f dangling=true); if [ -n \"\$images\" ]; then sudo docker rmi -f \$images; fi'"
}
}
}
}
}
stage('Clean Up Docker Images on Jenkins Server') {
steps {
echo 'Cleaning up unused Docker images on Jenkins server'
// Clean up unused Docker images, including those created within the last hour
sh "docker image prune -f --all --filter \"until=1h\""
}
}
}
post {
success {
slackSend (channel: '#cicd-notification', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (channel: '#cicd-notification', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}