-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
132 lines (120 loc) · 5.5 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
pipeline {
agent any
tools {
maven 'M3'
}
environment {
dockerHubRegistry = 'qkdrmsgh73/service-response'
dockerHubRegistryCredential = '634c3ce0-9c39-469b-86f3-3836d26d2edf'
dockerImageName = 'service-response'
gitCredentialId = '6261fc8c-759c-4d26-b6ff-29666b2a5625'
gitSrcUrl = '[email protected]:KSWA-SWEEP/Formduo-Response-Service.git'
gitManifestUrl = '[email protected]:KSWA-SWEEP/k8s-manifest.git'
}
stages {
stage('Start') {
agent any
steps {
slackSend (channel: '#jenkins', color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
stage('Checkout Application Git Branch') {
steps {
git credentialsId: "${gitCredentialId}",
url: "${gitSrcUrl}",
branch: 'main'
}
post {
failure {
slackSend (channel: '#jenkins', color: '#FF0000', message: "Repository Clone Failure !: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
echo 'Repository clone failure !'
}
success {
echo 'Repository clone success !'
}
}
}
stage('Maven Jar Build') {
steps {
sh 'mvn clean install -DskipTests=true'
}
post {
failure {
slackSend (channel: '#jenkins', color: '#FF0000', message: "Maven jar build Failure !: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
echo 'Maven jar build failure !'
}
success {
echo 'Maven jar build success !'
}
}
}
stage('Docker Image Build') {
steps {
sh "docker build . -t ${dockerHubRegistry}:${currentBuild.number}"
sh "docker build . -t ${dockerHubRegistry}:latest"
}
post {
failure {
slackSend (channel: '#jenkins', color: '#FF0000', message: "Docker image build Failure !: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
echo 'Docker image build failure !'
}
success {
echo 'Docker image build success !'
}
}
}
stage('Docker Image Push') {
steps {
withDockerRegistry([ credentialsId: dockerHubRegistryCredential, url: "" ]) {
sh "docker push ${dockerHubRegistry}:${currentBuild.number}"
sh "docker push ${dockerHubRegistry}:latest"
sleep 10 /* Wait uploading */
}
}
post {
failure {
slackSend (channel: '#jenkins', color: '#FF0000', message: "Docker image push Failure !: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
echo 'Docker Image Push failure !'
sh "docker rmi ${dockerHubRegistry}:${currentBuild.number}"
sh "docker rmi ${dockerHubRegistry}:latest"
}
success {
echo 'Docker image push success !'
sh "docker rmi ${dockerHubRegistry}:${currentBuild.number}"
sh "docker rmi ${dockerHubRegistry}:latest"
}
}
}
stage('K8S Manifest Update') {
steps {
git credentialsId: "${gitCredentialId}",
url: "${gitManifestUrl}",
branch: 'main'
sh "sed -i 's/${dockerImageName}:.*\$/${dockerImageName}:${currentBuild.number}/g' ./${dockerImageName}/deployment.yaml"
sh "git add ./${dockerImageName}/deployment.yaml"
sh "git config --global user.email '[email protected]'"
sh "git config --global user.name 'banggeunho'"
sh "git commit -m '[UPDATE] ${dockerImageName} ${currentBuild.number} image versioning'"
sshagent(credentials: ["${gitCredentialId}"]) {
sh "git remote set-url origin ${gitManifestUrl}"
sh "git push -u origin main"
}
}
post {
failure {
slackSend (channel: '#jenkins', color: '#FF0000', message: "K8S Manifest Update Failure !: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
echo 'K8S Manifest Update failure !'
}
success {
echo 'K8S Manifest Update success !'
}
}
}
stage('End') {
agent any
steps {
slackSend (channel: '#jenkins', color: '#4B89DC', message: "Build Suceessful!: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
}