blob: 26c6ee50e9f9c557c9d26ba106a58aad1b47609f [file] [log] [blame]
Sergey Otpuschennikovaad1ae02020-09-11 19:33:51 +04001#!groovy
2
3def main(String cacheHome = 'output/cache') {
4 String gitUrl = "${env.GERRIT_SCHEME}://${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}"
5 String gitRef = env.GERRIT_BRANCH ?: env.GERRIT_REFNAME
6 String artInfraNamespace = 'binary-dev-local/infra'
7 String artCacheFile = "${env.CI_NAME}.jjb.zip"
8 String artCacheUrl = "${env.ARTIFACTORY_URL}/artifactory/${artInfraNamespace}/${artCacheFile}"
9 String artCredential = env.ART_CREDENTIALS_ID
10 String jenkinsCredential = env.JENKINS_CREDENTIALS_ID
11 def response
12
13 def jenkinsJobs
14 def jjbJobs
15 def jobsToRemove
16
17 currentBuild.description = ''
18
19 // Set current build description
20 if (env.GERRIT_CHANGE_URL) {
21 currentBuild.description = """
22 <p>
23 Triggered by change: <a href="${env.GERRIT_CHANGE_URL}">${env.GERRIT_CHANGE_NUMBER},${env.GERRIT_PATCHSET_NUMBER}</a><br/>
24 Project: <b>${env.GERRIT_PROJECT}</b><br/>
25 Branch: <b>${env.GERRIT_BRANCH}</b><br/>
26 Subject: <b>${env.GERRIT_CHANGE_SUBJECT}</b><br/>
27 </p>
28 """
29 }
30
31 stage('SCM checkout') {
32 if (env.MAINTAIN_MODE.toLowerCase() == 'false') {
33 checkout([
34 $class: 'GitSCM',
35 branches: [[
36 name: 'FETCH_HEAD'
37 ]],
38 userRemoteConfigs: [[
39 url: gitUrl,
40 refspec: gitRef,
41 credentialsId: env.GIT_CREDENTIALS_ID
42 ]],
43 extensions: [[
44 $class: 'WipeWorkspace'
45 ]],
46 ])
47 }
48 }
49
Sergey Otpuschennikov9bb06d32020-09-15 13:13:31 +040050 /*stage('Get JJB cache') {
Sergey Otpuschennikovaad1ae02020-09-11 19:33:51 +040051 dir(cacheHome) {
52 response = httpRequest \
53 url: artCacheUrl,
54 authentication: artCredential,
55 httpMode: 'GET',
56 outputFile: artCacheFile,
57 validResponseCodes: '100:399,404'
58 if (response.status != 404) {
59 unzip \
60 zipFile: artCacheFile,
61 dir: '.cache'
62 }
63 }
Sergey Otpuschennikov9bb06d32020-09-15 13:13:31 +040064 }*/
Sergey Otpuschennikovaad1ae02020-09-11 19:33:51 +040065
66 stage('Update JJB jobs') {
67 if (env.MAINTAIN_MODE.toLowerCase() == 'false') {
68 withCredentials([
69 usernamePassword(
70 credentialsId: env.JENKINS_CREDENTIALS_ID,
71 usernameVariable: 'JJB_USER',
72 passwordVariable: 'JJB_PASSWORD')
73 ]) {
74 withEnv([
75 "HOME=${cacheHome}"
76 ]) {
77 sh 'tox -v -e update $JOBS_LIST'
78 }
79 }
80 } else {
81 input \
82 message: 'Sleeping for maintainance'
83 }
84 }
85
86 stage('Get deployed jobs list') {
87 response = httpRequest \
88 url: "${JENKINS_URL}/crumbIssuer/api/json",
89 authentication: jenkinsCredential
90 def crumb = readJSON text: response.getContent()
91
92 response = httpRequest \
93 url: "${JENKINS_URL}/api/json?tree=jobs[name,description]",
94 authentication: jenkinsCredential,
95 customHeaders: [[
96 name: crumb.crumbRequestField,
97 value: crumb.crumb,
98 maskValue: true
99 ]]
100 def jobs = readJSON text: response.getContent()
101
102 jenkinsJobs = jobs.jobs.findAll {
103 // Filter jenkins jobs deployed by JJB
104 it.description.toString().contains('Managed by Jenkins Job Builder')
105 }.collect{ it.name }
106 }
107
108 stage('Get JJB jobs list') {
109 withEnv([
110 "HOME=${cacheHome}"
111 ]) {
112 sh 'tox -v -e jobs'
113 }
114 dir("output/${env.CI_NAME}") {
115 jjbJobs = sh(
116 script: "grep -rl actions | sed 's|/config.xml\$||g'",
117 returnStdout: true
118 ).trim().readLines()
119 }
120
121 if (jjbJobs.size() == 0) {
122 error 'ERROR: Unexpected JJB output. No generated jobs found'
123 }
124 }
125
126 stage('Remove undefined jobs') {
127 jobsToRemove = jenkinsJobs.findAll { ! jjbJobs.contains(it) }
128
129 if (jobsToRemove.size() > 0) {
130 withCredentials([
131 usernamePassword(
132 credentialsId: jenkinsCredential,
133 usernameVariable: 'JJB_USER',
134 passwordVariable: 'JJB_PASSWORD')
135 ]) {
136 withEnv([
137 "HOME=${cacheHome}"
138 ]) {
139 sh "tox -v -e delete ${jobsToRemove.join(' ')}"
140 }
141 }
142
143 String description = '<b>DELETED</b><ul>'
144 jobsToRemove.each { description += "<li>${it}</li>" }
145 description += '</ul>'
146 currentBuild.description += description
147 } else {
148 currentBuild.description += 'No jobs to remove'
149 }
150 }
151
Sergey Otpuschennikov9bb06d32020-09-15 13:13:31 +0400152 /*stage('Save JJB cache') {
Sergey Otpuschennikovaad1ae02020-09-11 19:33:51 +0400153 dir(cacheHome) {
154 sh "rm -f ${artCacheFile}"
155 zip \
156 zipFile: artCacheFile,
157 dir: '.cache',
158 glob: 'jenkins_jobs/**'
159 response = httpRequest \
160 url: artCacheUrl,
161 authentication: artCredential,
162 httpMode: 'PUT',
163 multipartName: 'file',
164 uploadFile: artCacheFile
165 }
Sergey Otpuschennikov9bb06d32020-09-15 13:13:31 +0400166 }*/
Sergey Otpuschennikovaad1ae02020-09-11 19:33:51 +0400167}
168
169String podTpl = """
170 apiVersion: "v1"
171 kind: "Pod"
172 spec:
173 securityContext:
174 runAsUser: 1000
175 containers:
176 - name: "tox"
177 image: "${env.DOCKER_IMAGE}"
178 command:
179 - "cat"
180 securityContext:
181 privileged: false
182 tty: true
183"""
184
185if (env.K8S_CLUSTER == 'unset') {
186 node(env.SLAVE_LABEL ?: 'docker') {
187 main(env.WORKSPACE)
188 }
189} else {
190 podTemplate(
191 cloud: env.K8S_CLUSTER,
192 yaml: podTpl,
193 showRawYaml: false
194 ) {
195 node(POD_LABEL) {
196 container('tox') {
197 main()
198 }
199 }
200 }
201}
202