blob: 7820135e75368fd8757f36f020cca9d4d14b13c4 [file] [log] [blame]
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03001package com.mirantis.mcp
2
Sergey Kulanov91d8def2016-11-15 13:53:17 +02003import org.jfrog.hudson.pipeline.types.ArtifactoryServer
4import org.jfrog.hudson.pipeline.types.buildInfo.BuildInfo
5
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03006/**
7 * Return string of mandatory build properties for binaries
8 * User can also add some custom properties.
9 *
10 * @param customProperties a Array of Strings that should be added to mandatory props
11 * in format ["prop1=value1", "prop2=value2"]
12 * */
13def getBinaryBuildProperties(ArrayList customProperties) {
14 def namespace = "com.mirantis."
15 def properties = [
Sergey Kulanovc70f1c22016-11-16 13:05:20 +020016 "buildName=${env.JOB_NAME}",
17 "buildNumber=${env.BUILD_NUMBER}",
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030018 "gerritProject=${env.GERRIT_PROJECT}",
19 "gerritChangeNumber=${env.GERRIT_CHANGE_NUMBER}",
20 "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}",
21 "gerritChangeId=${env.GERRIT_CHANGE_ID}",
22 "gerritPatchsetRevision=${env.GERRIT_PATCHSET_REVISION}"
23 ]
24
25 if (customProperties) {
26 properties.addAll(customProperties)
27 }
28
29 def common = new com.mirantis.mcp.Common()
30
31 return common.constructString(properties, namespace, ";")
32}
33
34/**
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030035 * Get URL to artifact(s) by properties
36 * Returns String(s) with URL to found artifact or null if nothing
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030037 *
38 * @param artifactoryURL String, an URL to Artifactory
39 * @param properties LinkedHashMap, a Hash of properties (key-value) which
40 * which should determine artifact in Artifactory
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030041 * @param onlyLastItem Boolean, return only last URL if true(by default),
42 * else return list of all found artifact URLS
43 *
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030044 */
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030045def uriByProperties(String artifactoryURL, LinkedHashMap properties, Boolean onlyLastItem=true) {
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030046 def key, value
47 def properties_str = ''
48 for (int i = 0; i < properties.size(); i++) {
49 // avoid serialization errors
50 key = properties.entrySet().toArray()[i].key
51 value = properties.entrySet().toArray()[i].value
52 properties_str += "${key}=${value}&"
53 }
54 def search_url = "${artifactoryURL}/api/search/prop?${properties_str}"
55
56 def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"",
57 returnStdout: true).trim()
58 def content = new groovy.json.JsonSlurperClassic().parseText(result)
59 def uri = content.get("results")
60 if (uri) {
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030061 if (onlyLastItem) {
62 return uri.last().get("uri")
63 } else {
64 res = []
65 uri.each {it ->
66 res.add(it.get("uri"))
67 }
68 return res
69 }
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030070 } else {
71 return null
72 }
73}
74
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030075
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030076/**
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030077 * Set properties for artifact in Artifactory repo
78 *
79 * @param artifactUrl String, an URL to artifact in Artifactory repo
80 * @param properties LinkedHashMap, a Hash of properties (key-value) which
81 * should be assigned for choosen artifact
82 * @param recursive Boolean, if artifact_url is a directory, whether to set
83 * properties recursively or not
84 */
85def setProperties(String artifactUrl, LinkedHashMap properties, Boolean recursive = false) {
86 def properties_str = 'properties='
87 def key, value
88 if (recursive) {
89 recursive = 'recursive=1'
90 } else {
91 recursive = 'recursive=0'
92 }
Alexander Evseevbd40ef92017-10-18 12:24:45 +030093 properties_str += properties.collect({"${it.key}=${it.value}"}).join(';')
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030094 def url = "${artifactUrl}?${properties_str}&${recursive}"
95 withCredentials([
96 [$class : 'UsernamePasswordMultiBinding',
97 credentialsId : 'artifactory',
98 passwordVariable: 'ARTIFACTORY_PASSWORD',
99 usernameVariable: 'ARTIFACTORY_LOGIN']
100 ]) {
101 sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\""
102 }
103}
104
105/**
106 * Get properties for specified artifact in Artifactory
107 * Returns LinkedHashMap of properties
108 *
109 * @param artifactUrl String, an URL to artifact in Artifactory repo
110 */
111def getPropertiesForArtifact(String artifactUrl) {
112 def url = "${artifactUrl}?properties"
113 def result
114 withCredentials([
115 [$class : 'UsernamePasswordMultiBinding',
116 credentialsId : 'artifactory',
117 passwordVariable: 'ARTIFACTORY_PASSWORD',
118 usernameVariable: 'ARTIFACTORY_LOGIN']
119 ]) {
120 result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"",
121 returnStdout: true).trim()
122 }
123 def properties = new groovy.json.JsonSlurperClassic().parseText(result)
124 return properties.get("properties")
125}
126
127/**
Denis Egorenko7c0abfe2017-02-14 15:42:02 +0400128 * Find docker images by tag
129 * Returns Array of image' hashes with names as full path in @repo
130 *
131 * Example:
132 *
133 * [ {
134 * "path" : "mirantis/ccp/ci-cd/gerrit-manage/test"
135 * },
136 * {
137 * "path" : "mirantis/ccp/ci-cd/gerrit/test"
138 * }
139 * ]
140 *
141 * @param artifactoryURL String, an URL to Artifactory
142 * @param repo String, a name of repo where should be executed search
143 * @param tag String, tag of searched image
144 */
145def getImagesByTag(String artifactoryURL, String repo, String tag) {
146 def url = "${artifactoryURL}/api/search/aql"
147 def result
148 writeFile file: "query",
149 text: """\
150 items.find(
151 {
152 \"repo\": \"${repo}\",
153 \"@docker.manifest\": { \"\$match\" : \"${tag}*\" }
154 }
155 ).
156 include(\"path\")
157 """.stripIndent()
158 withCredentials([
159 [$class: 'UsernamePasswordMultiBinding',
160 credentialsId: 'artifactory',
161 passwordVariable: 'ARTIFACTORY_PASSWORD',
162 usernameVariable: 'ARTIFACTORY_LOGIN']
163 ]) {
164 result = sh(script: "bash -c \"curl -X POST -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -d @query \'${url}\'\"",
165 returnStdout: true).trim()
166 }
167 def images = new groovy.json.JsonSlurperClassic().parseText(result)
168 return images.get("results")
169}
170
171/**
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300172 * Upload docker image to Artifactory
173 *
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200174 * @param server ArtifactoryServer, the instance of Artifactory server
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300175 * @param registry String, the name of Docker registry
176 * @param image String, Docker image name
177 * @param version String, Docker image version
178 * @param repository String, The name of Artifactory Docker repository
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200179 * @param buildInfo BuildInfo, the instance of a build-info object which can be published,
180 * if defined, then we publish BuildInfo
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300181 */
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200182def uploadImageToArtifactory (ArtifactoryServer server, String registry, String image,
183 String version, String repository,
Dmitry Burmistrov6ee39522017-05-22 12:46:25 +0400184 BuildInfo buildInfo = null,
185 LinkedHashMap properties = null) {
Denis Egorenkoedba5a52016-11-15 19:55:56 +0300186 // TODO Switch to Artifactoy image' pushing mechanism once we will
187 // prepare automatical way for enabling artifactory build-proxy
188 //def artDocker
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300189 withCredentials([
190 [$class: 'UsernamePasswordMultiBinding',
191 credentialsId: 'artifactory',
192 passwordVariable: 'ARTIFACTORY_PASSWORD',
193 usernameVariable: 'ARTIFACTORY_LOGIN']
194 ]) {
195 sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}")
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300196 //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}")
197 }
198
Denis Egorenkoedba5a52016-11-15 19:55:56 +0300199 sh ("docker push ${registry}/${image}:${version}")
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300200 //artDocker.push("${registry}/${image}:${version}", "${repository}")
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200201 def image_url = server.getUrl() + "/api/storage/${repository}/${image}/${version}"
Dmitry Burmistrov6ee39522017-05-22 12:46:25 +0400202 if ( ! properties ) {
203 properties = [
Sergey Kulanovc70f1c22016-11-16 13:05:20 +0200204 'com.mirantis.buildName':"${env.JOB_NAME}",
205 'com.mirantis.buildNumber': "${env.BUILD_NUMBER}",
206 'com.mirantis.gerritProject': "${env.GERRIT_PROJECT}",
207 'com.mirantis.gerritChangeNumber': "${env.GERRIT_CHANGE_NUMBER}",
208 'com.mirantis.gerritPatchsetNumber': "${env.GERRIT_PATCHSET_NUMBER}",
209 'com.mirantis.gerritChangeId': "${env.GERRIT_CHANGE_ID}",
210 'com.mirantis.gerritPatchsetRevision': "${env.GERRIT_PATCHSET_REVISION}",
Sergey Kulanov4d3951c2016-11-24 13:58:15 +0200211 'com.mirantis.targetImg': "${image}",
Sergey Kulanovc70f1c22016-11-16 13:05:20 +0200212 'com.mirantis.targetTag': "${version}"
Dmitry Burmistrov6ee39522017-05-22 12:46:25 +0400213 ]
214 }
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300215
216 setProperties(image_url, properties)
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200217
218 if ( buildInfo != null ) {
219 buildInfo.env.capture = true
220 buildInfo.env.filter.addInclude("*")
221 buildInfo.env.filter.addExclude("*PASSWORD*")
222 buildInfo.env.filter.addExclude("*password*")
223 buildInfo.env.collect()
224 server.publishBuildInfo(buildInfo)
225 }
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300226}
227
228/**
229 * Upload binaries to Artifactory
230 *
231 * @param server ArtifactoryServer, the instance of Artifactory server
232 * @param buildInfo BuildInfo, the instance of a build-info object which can be published
233 * @param uploadSpec String, a spec which is a JSON file that specifies which files should be
234 * uploaded or downloaded and the target path
235 * @param publishInfo Boolean, whether publish a build-info object to Artifactory
236 */
Sergey Kulanov91d8def2016-11-15 13:53:17 +0200237def uploadBinariesToArtifactory (ArtifactoryServer server, BuildInfo buildInfo, String uploadSpec,
238 Boolean publishInfo = false) {
Jakub Josefbefcf6c2017-11-14 18:03:10 +0100239 server.upload(uploadSpec, buildInfo)
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300240
241 if ( publishInfo ) {
242 buildInfo.env.capture = true
243 buildInfo.env.filter.addInclude("*")
244 buildInfo.env.filter.addExclude("*PASSWORD*")
245 buildInfo.env.filter.addExclude("*password*")
246 buildInfo.env.collect()
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300247 server.publishBuildInfo(buildInfo)
248 }
249}
250
251/**
252 * Promote Docker image artifact to release repo
253 *
254 * @param artifactoryURL String, an URL to Artifactory
255 * @param artifactoryDevRepo String, the source dev repository name
256 * @param artifactoryProdRepo String, the target repository for the move or copy
257 * @param dockerRepo String, the docker repository name to promote
258 * @param artifactTag String, an image tag name to promote
259 * @param targetTag String, target tag to assign the image after promotion
260 * @param copy Boolean, an optional value to set whether to copy instead of move
261 * Default: false
262 */
263def promoteDockerArtifact(String artifactoryURL, String artifactoryDevRepo,
264 String artifactoryProdRepo, String dockerRepo,
265 String artifactTag, String targetTag, Boolean copy = false) {
266 def url = "${artifactoryURL}/api/docker/${artifactoryDevRepo}/v2/promote"
Dmitry Burmistrov5deaa7d2017-05-30 17:12:54 +0400267 String queryFile = UUID.randomUUID().toString()
Dmitry Burmistrov97beb9b2017-05-29 17:21:34 +0400268 writeFile file: queryFile,
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300269 text: """{
270 \"targetRepo\": \"${artifactoryProdRepo}\",
271 \"dockerRepository\": \"${dockerRepo}\",
272 \"tag\": \"${artifactTag}\",
273 \"targetTag\" : \"${targetTag}\",
274 \"copy\": \"${copy}\"
275 }""".stripIndent()
Dmitry Burmistrov97beb9b2017-05-29 17:21:34 +0400276 sh "cat ${queryFile}"
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300277 withCredentials([
278 [$class : 'UsernamePasswordMultiBinding',
279 credentialsId : 'artifactory',
280 passwordVariable: 'ARTIFACTORY_PASSWORD',
281 usernameVariable: 'ARTIFACTORY_LOGIN']
282 ]) {
Sergey Reshetnyakf0775fb2018-06-28 14:54:01 +0400283 sh "bash -c \"curl --fail -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -H \"Content-Type:application/json\" -X POST -d @${queryFile} ${url}\""
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300284 }
Dmitry Burmistrov97beb9b2017-05-29 17:21:34 +0400285 sh "rm -v ${queryFile}"
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300286}