-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile-New
More file actions
230 lines (217 loc) · 9.89 KB
/
Jenkinsfile-New
File metadata and controls
230 lines (217 loc) · 9.89 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
def COMMIT_ID
def RESPONSE
pipeline {
parameters {
string(name: 'PRODUCTION_NAMESPACE', description: 'Production Namespace', defaultValue: '')
string(name: 'STAGING_NAMESPACE', description: 'Staging Namespace', defaultValue: '')
string(name: 'DEVELOPMENT_NAMESPACE', description: 'Development Namespace', defaultValue: 'demoplaycourt') // codebase-api-dev
// string(name: 'VSAN_REGISTRY_URL', description: ' Openshift registry URL', defaultValue: '') // docker-registry-default.vsan-apps.sample.id
string(name: 'DELL_REGISTRY_URL', description: ' Openshift registry URL', defaultValue: 'docker-registry-default.apps.playcourt.id') // docker-registry-default.apps.sample.id
string(name: 'DOCKER_IMAGE_NAME', description: 'Docker Image Name', defaultValue: 'codebase-api') // codebase-api
string(name: 'CHAT_ID', description: 'Chat ID of Telegram Group', defaultValue: '') //-383243277
}
// environment{
// telegram = credentials('telegram-token')
// }
agent none
options {
// Skip default checkout behavior
skipDefaultCheckout()
}
stages {
stage('Checkout SCM') {
agent { label "agent-node-go" }
steps {
checkout scm
script {
echo "get COMMIT_ID"
COMMIT_ID = sh(
returnStdout : true,
script : "git rev-parse --short HEAD"
).trim()
}
// stash this current workspace
stash(name: 'workspace-basic', includes:'**')
}
}
stage('Initialize') {
parallel {
stage("Agent Jenkins: Node Go") {
agent { label "agent-node-go" }
steps {
cleanWs()
unstash 'workspace-basic'
script{
sh "npm install"
}
}
}
stage("Agent Jenkis: Docker") {
agent { label "agent-docker" }
steps {
cleanWs()
script{
// Setup variable for retagging purpose
if ( env.BRANCH_NAME == 'master' ){
projectName = "${params.PRODUCTION_NAMESPACE}"
registryURL = "${params.DELL_REGISTRY_URL}"
envStage = "Production"
} else if ( env.BRANCH_NAME == 'release' ){
projectName = "${params.STAGING_NAMESPACE}"
registryURL = "${params.DELL_REGISTRY_URL}"
envStage = "Staging"
} else if ( env.BRANCH_NAME == 'development'){
projectName = "${params.DEVELOPMENT_NAMESPACE}"
registryURL = "${params.DELL_REGISTRY_URL}"
envStage = "Development"
}
// Defind for Final image name
// imageNameFinal = "${registryURL}/${projectName}/${params.DOCKER_IMAGE_NAME}"
}
}
}
}
}
stage('Testing Stage') {
agent { label "agent-node-go" }
steps {
script{
sh "ls -alh"
sh "npm test"
}
}
}
stage('SonarQube Analysis') {
when {
anyOf {
branch 'master'
branch 'release'
branch 'development'
}
}
agent { label "agent-node-go" }
steps {
echo "Run SonarQube"
script {
echo "defining sonar-scanner"
def scannerHome = tool 'SonarQube-Scanner' ;
withSonarQubeEnv('SonarQube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
stage('Build image') {
when {
anyOf{
branch 'master'
branch 'release'
branch 'development'
}
}
agent { label "agent-docker" }
steps {
script{
unstash 'workspace-basic'
sh "docker build --rm -t ${params.DOCKER_IMAGE_NAME}:${BUILD_NUMBER}-${COMMIT_ID} ."
sh "docker run -d -p 3000:3000 --name ${params.DOCKER_IMAGE_NAME} ${params.DOCKER_IMAGE_NAME}:${BUILD_NUMBER}-${COMMIT_ID}"
sh "docker ps -a"
sh "netstat -antp"
sh "docker exec ${params.DOCKER_IMAGE_NAME} ls -alh"
sh "docker stop ${params.DOCKER_IMAGE_NAME}"
sh "docker rm ${params.DOCKER_IMAGE_NAME}"
}
}
}
stage('Deployment') {
when {
anyOf{
branch 'master'
branch 'release'
branch 'development'
}
}
agent { label "agent-docker" }
stages{
stage("Login into registry") {
steps{
echo "In this stage try to connect internal registry"
script{
if ( env.BRANCH_NAME == 'master'){
withCredentials([string(credentialsId: 'TOKEN_DELL', variable: 'TOKEN')]) {
sh "docker login ${registryURL} -u aassuhendar -p ${TOKEN}"
}
} else {
withCredentials([string(credentialsId: 'TOKEN_DELL', variable: 'TOKEN')]) {
sh "docker login ${registryURL} -u aassuhendar -p ${TOKEN}"
}
}
}
}
}
stage ('Re-tag Image'){
steps{
echo "Retaging Image"
sh "docker tag ${params.DOCKER_IMAGE_NAME}:${BUILD_NUMBER}-${COMMIT_ID}\
${imageNameFinal}:latest"
}
}
stage ('Deploy'){
steps{
// unstash 'ws'
echo "In this stage try to deploy image to prod"
//vars envStage, ProjectName and imageNameFinal is from previous deployment stage (re-tagin image)
script{
// this step will be hold until deployment confirmed
if (env.BRANCH_NAME == 'master'){
timeout(10) {
input message: 'Deploy to PRODUCTION?', ok: 'Deploy'
}
echo "Deploying to ${envStage} (${projectName})"
sh "docker push ${imageNameFinal}:latest"
// push image with COMMIT_ID tag
sh "docker tag ${imageNameFinal}:latest ${imageNameFinal}:${COMMIT_ID}"
sh "docker push ${imageNameFinal}:${COMMIT_ID}"
sh "docker rmi -f ${imageNameFinal}:${COMMIT_ID}"
} else{
echo "Deploying to ${envStage} (${projectName})"
sh "docker push ${imageNameFinal}:latest"
}
}
echo "CleanUp Images"
sh "docker rmi -f ${params.DOCKER_IMAGE_NAME}:${BUILD_NUMBER}-${COMMIT_ID}"
// sh "docker rmi -f ${imageNameFinal}"
}
}
}
}
}
post {
failure{
node("agent-docker"){
echo "Send message if failure occured"
// script{
// withCredentials([string(credentialsId: 'telegram-token', variable: 'TELEGRAM_TOKEN')]) {
// if (BRANCH_NAME == 'master' || BRANCH_NAME == 'release' || BRANCH_NAME == 'develop'){
// textMessage = "(╥﹏╥) Jenkins Job --- ${JOB_NAME}-${BUILD_NUMBER}-${COMMIT_ID} is FAILED , Please check ${BUILD_URL}console"
// sh "curl -s -X POST 'https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage?chat_id=${params.CHAT_ID}&text=${textMessage}'"
// }
// }
// }
}
}
success{
node("agent-docker"){
echo "Send message if success occured"
// script{
// withCredentials([string(credentialsId: 'telegram-token', variable: 'TELEGRAM_TOKEN')]) {
// if (BRANCH_NAME == 'master' || BRANCH_NAME == 'release' || BRANCH_NAME == 'develop'){
// textMessage = "҉\\(•˘▽˘•)/҉ Jenkins Job --- ${JOB_NAME}-${BUILD_NUMBER}-${COMMIT_ID} is SUCCESS"
// sh "curl -s -X POST 'https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage?chat_id=${params.CHAT_ID}&text=${textMessage}'"
// }
// }
// }
}
}
}
}