This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
201 lines (180 loc) · 7.69 KB
/
Copy pathJenkinsfile
File metadata and controls
201 lines (180 loc) · 7.69 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
pipeline {
agent {
kubernetes {
yamlFile 'k8s/jenkins-slave.yaml'
defaultContainer 'java11'
}
}
triggers {
pollSCM('H/2 * * * *')
}
options {
disableConcurrentBuilds()
timeout(time: 10)
buildDiscarder(logRotator(numToKeepStr: '10'))
}
environment {
DEPLOYMENT_NAME = "icamapi"
GIT_REPO = "github.com/ABC-COVID19/API-backend.git"
NAMESPACE_DEV = "icam-dev"
NAMESPACE_PROD = "icam-prod"
DOCKER_HUB = "docker.icam.org.pt"
// SLACK_CHANNEL = ''
// SLACK_TEAM_DOMAIN = ''
// SLACK_TOKEN = credentials('')
PROJECT_VERSION = readFile(file: 'version.txt').trim()
NEW_VERSION = bumpVersion("${PROJECT_VERSION}","patch")
GIT_USER = 'jenkins-icam@protonmail.com'
GIT_USER_NAME = 'jenkins-icam'
}
stages {
stage('Recursive build Check') {
steps {
script {
if (checkCommit("updated version to")){
timeout(time: 10, unit: 'SECONDS') {
input 'Recursive Run'
}
}
}
}
}
stage('Bump CICD Version') {
when {
branch "develop"
}
steps {
sh "echo ${NEW_VERSION} > version.txt"
sh "cat version.txt || true"
sh "echo The new version will be: ${NEW_VERSION}"
}
}
stage('Build') {
steps {
sh "unset MAVEN_CONFIG"
sh "./mvnw clean compile"
sh "./mvnw -ntp -Pprod jib:dockerBuild"
sh "docker tag icamapi:latest ${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION}"
}
}
stage('Update version on Develop') {
when {
branch "develop"
}
steps {
sh "git config --global user.email '${GIT_USER}'"
sh "git config --global user.name '${GIT_USER_NAME}'"
sh "git add -A"
sh "git commit -m 'updated version to ${NEW_VERSION}'"
withCredentials([usernamePassword(credentialsId: 'Jenkins-ICAM2', usernameVariable: 'username', passwordVariable: 'password')]) {
sh "git push https://${username}:${password}@${GIT_REPO} HEAD:develop"
}
}
}
stage('Merge to Master') {
when {
branch "develop"
}
steps {
script{
try{
timeout(time: 90, unit: 'SECONDS') {
input 'Confirm Merge to master?'
}
sh "git config --global user.email '${GIT_USER}'"
sh "git config --global user.name '${GIT_USER_NAME}'"
sh "git checkout -f origin/master"
sh "git merge --ff ${env.GIT_COMMIT}"
withCredentials([usernamePassword(credentialsId: 'Jenkins-ICAM2', usernameVariable: 'username', passwordVariable: 'password')]) {
sh "git push https://${username}:${password}@${GIT_REPO} HEAD:master"
}
}catch(err){
sh "echo Skipped by users"
}
}
}
}
stage('Deliver to Hub - Deploy to DEV') {
when {
branch "develop"
}
steps {
container('az-kube'){
sh "docker tag ${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION} ${DOCKER_HUB}/${DEPLOYMENT_NAME}:latest"
sh "docker push ${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION}"
sh "docker push ${DOCKER_HUB}/${DEPLOYMENT_NAME}:latest"
withCredentials([azureServicePrincipal('Azure_login')]) {
sh "az login --service-principal -u ${AZURE_CLIENT_ID} -p ${AZURE_CLIENT_SECRET} -t ${AZURE_TENANT_ID}"
sh "az aks get-credentials --name icamch --resource-group icam-ch --overwrite-existing"
sh "kubectl set image deployment ${DEPLOYMENT_NAME} ${DEPLOYMENT_NAME}-app=${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION} --record -n ${NAMESPACE_DEV}"
}
}
}
}
stage('Deliver to Hub - Deploy to PROD') {
when {
branch "master"
}
steps {
container('az-kube'){
sh "docker tag ${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION} ${DOCKER_HUB}/${DEPLOYMENT_NAME}:latest"
sh "docker push ${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION}"
sh "docker push ${DOCKER_HUB}/${DEPLOYMENT_NAME}:latest"
withCredentials([azureServicePrincipal('Azure_login')]) {
sh "az login --service-principal -u ${AZURE_CLIENT_ID} -p ${AZURE_CLIENT_SECRET} -t ${AZURE_TENANT_ID}"
sh "az aks get-credentials --name icamch --resource-group icam-ch --overwrite-existing"
sh "kubectl set image deployment ${DEPLOYMENT_NAME} ${DEPLOYMENT_NAME}-app=${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION} --record -n ${NAMESPACE_PROD}"
}
}
}
}
}
post {
always {
container('az-kube'){
sh "docker rmi ${DOCKER_HUB}/${DEPLOYMENT_NAME}:${NEW_VERSION} || true "
sh "docker rmi ${DOCKER_HUB}/${DEPLOYMENT_NAME}:latest || true "
}
// notifySlack()
}
cleanup {
cleanWs()
}
}
}
/*****************************************
* To use this function you need to install
* the Slack Notification Plugin
****************************************/
def notifySlack(additionalInfo = '') {
def colorCode = '#79ae40'
def status = 'SUCCESS'
if (currentBuild.result == 'FAILURE') {
colorCode = '#d34e56'
status = 'FAILURE'
}
def commitText = sh(returnStdout: true, script: 'git show -s --format=format:"*%s* _by %an_" HEAD').trim()
def subject = "${env.JOB_NAME} - #${env.BUILD_NUMBER} ${status} (<${env.BUILD_URL}|Open>)"
def summary = "${subject}\nChanges: ${commitText}\nBranch: ${env.GIT_BRANCH}\n${additionalInfo}"
slackSend channel: "${env.SLACK_CHANNEL}", color: colorCode, message: summary, teamDomain: "${env.SLACK_TEAM_DOMAIN}", token: "${env.SLACK_TOKEN}"
}
def getLastSprint(branch){
check = sh(returnStdout: true, script: 'git branch -a | grep sprint | tail -1 | cut -d "/" -f 3')
return check.contains(branch)
}
def chooseVersion(oldVersion, branch){
return branch.contains("sprint_") ? bumpVersion(oldVersion,'patch') :
branch.contains("develop") ? bumpVersion(oldVersion,'minor') : "NONE"
}
def bumpVersion(oldVersion, arg) {
//eg: bumpVersion(0.2.3-Snapshot, major) -> 1.0.0-Snapshot
def pos = ["major", "minor", "patch"].indexOf(arg)
def parts = oldVersion.split("-|\\.")
parts[pos] = Integer.parseInt(parts[pos]) + 1
for(i = pos + 1; i < 3; i++) parts[i] = 0
return parts.length == 3 ? parts[0..2].join('.') : [parts[0..2].join('.'), parts[3]].join('-')
}
def checkCommit(message) {
def commitText = sh(returnStdout: true, script: 'git show -s --format="%s"').trim()
return commitText.contains(message)
}