blob: 881e44aeea7e6466301472bac3b243286a1174cd [file] [log] [blame]
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03001package com.mirantis.mcp
2
Sergey Kolekonov74a6b6e2019-06-28 11:45:47 +04003import org.jfrog.hudson.pipeline.common.types.ArtifactoryServer
4import org.jfrog.hudson.pipeline.common.types.buildInfo.BuildInfo
Sergey Kulanov91d8def2016-11-15 13:53:17 +02005
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
Sergey Kolekonov54c44842019-06-17 19:25:52 +040043 * @param repos ArrayList, a list of repositories to search in
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030044 *
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030045 */
Sergey Kolekonov54c44842019-06-17 19:25:52 +040046def uriByProperties(String artifactoryURL, LinkedHashMap properties, Boolean onlyLastItem=true, ArrayList repos=[]) {
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030047 def key, value
48 def properties_str = ''
49 for (int i = 0; i < properties.size(); i++) {
50 // avoid serialization errors
Kirill Mashchenko56c8ff32018-06-28 03:01:34 +030051 key = properties.entrySet().toArray()[i].key.trim()
52 value = properties.entrySet().toArray()[i].value.trim()
53 properties_str += /${key}=${value}&/
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030054 }
Sergey Kolekonov54c44842019-06-17 19:25:52 +040055 def repos_str = (repos) ? repos.join(',') : ''
56 def search_url
57 if (repos_str) {
58 search_url = "${artifactoryURL}/api/search/prop?${properties_str}&repos=${repos_str}"
59 } else {
60 search_url = "${artifactoryURL}/api/search/prop?${properties_str}"
61 }
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030062
Kirill Mashchenko56c8ff32018-06-28 03:01:34 +030063 def result = sh(script: /curl -X GET '${search_url}'/,
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030064 returnStdout: true).trim()
65 def content = new groovy.json.JsonSlurperClassic().parseText(result)
66 def uri = content.get("results")
67 if (uri) {
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030068 if (onlyLastItem) {
69 return uri.last().get("uri")
70 } else {
71 res = []
72 uri.each {it ->
73 res.add(it.get("uri"))
74 }
75 return res
76 }
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030077 } else {
78 return null
79 }
80}
81
Kirill Mashchenko1d225c22018-06-19 13:52:17 +030082
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030083/**
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +030084 * Set properties for artifact in Artifactory repo
85 *
86 * @param artifactUrl String, an URL to artifact in Artifactory repo
87 * @param properties LinkedHashMap, a Hash of properties (key-value) which
88 * should be assigned for choosen artifact
89 * @param recursive Boolean, if artifact_url is a directory, whether to set
90 * properties recursively or not
91 */
92def setProperties(String artifactUrl, LinkedHashMap properties, Boolean recursive = false) {
93 def properties_str = 'properties='
94 def key, value
95 if (recursive) {
96 recursive = 'recursive=1'
97 } else {
98 recursive = 'recursive=0'
99 }
Alexander Evseevbd40ef92017-10-18 12:24:45 +0300100 properties_str += properties.collect({"${it.key}=${it.value}"}).join(';')
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300101 def url = "${artifactUrl}?${properties_str}&${recursive}"
102 withCredentials([
103 [$class : 'UsernamePasswordMultiBinding',
104 credentialsId : 'artifactory',
105 passwordVariable: 'ARTIFACTORY_PASSWORD',
106 usernameVariable: 'ARTIFACTORY_LOGIN']
107 ]) {
108 sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\""
109 }
110}
111
112/**
Sergey Kolekonov76c17f52019-09-09 16:55:01 +0400113 * Create an empty directory in Artifactory repo
114 *
115 * @param artifactoryURL String, an URL to Artifactory
116 * @param path String, a path to the desired directory including repository name
117 * @param dir String, desired directory name
118 */
119def createDir (String artifactoryURL, String path, String dir) {
120 def url = "${artifactoryURL}/${path}/${dir}/"
121 withCredentials([
122 [$class : 'UsernamePasswordMultiBinding',
123 credentialsId : 'artifactory',
124 passwordVariable: 'ARTIFACTORY_PASSWORD',
125 usernameVariable: 'ARTIFACTORY_LOGIN']
126 ]) {
127 sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\""
128 }
129}
130
131/**
Sergey Kolekonovce616712019-09-10 16:09:23 +0400132 * Move/copy an artifact or a folder to the specified destination
Roman Vialovd36c1c02025-11-21 12:49:11 +0600133 * Uses curl to download/upload/delete files since /api/copy and /api/move are not supported
Sergey Kolekonovce616712019-09-10 16:09:23 +0400134 *
135 * @param artifactoryURL String, an URL to Artifactory
136 * @param sourcePath String, a source path to the artifact including repository name
137 * @param dstPath String, a destination path to the artifact including repository name
138 * @param copy boolean, whether to copy or move the item, default is move
139 * @param dryRun boolean, whether to perform dry run on not, default is false
140 */
Sergey Otpuschennikov715765c2025-12-02 12:22:34 +0000141def moveItem (String artifactoryURL, String sourcePath, String dstPath, boolean copy = false, boolean dryRun = false, String credentialsId = 'artifactory') {
Roman Vialovd36c1c02025-11-21 12:49:11 +0600142 def respCode = 200
143 def respText = ''
144
145 withCredentials([
146 [$class : 'UsernamePasswordMultiBinding',
147 credentialsId : credentialsId,
148 passwordVariable: 'ARTIFACTORY_PASSWORD',
149 usernameVariable: 'ARTIFACTORY_LOGIN']
150 ]) {
151 try {
152 // Check if source is a file or directory
153 def storageUrl = "${artifactoryURL}/api/storage/${sourcePath}"
154 def storageResult = sh(script: """
155 set +e
156 response=\$(curl -s -w "\\n%{http_code}" -X GET -u \${ARTIFACTORY_LOGIN}:\${ARTIFACTORY_PASSWORD} '${storageUrl}' 2>&1)
157 echo "\$response"
158 """, returnStdout: true).trim()
159
160 def storageLines = storageResult.split('\n')
161 def storageCode = storageLines.length > 0 && storageLines[-1] ==~ /^\d{3}$/ ? storageLines[-1].toInteger() : 404
162 def storageBody = storageLines.length > 1 ? storageLines[0..-2].join('\n') : ''
163
164 if (storageCode != 200) {
165 return [storageCode, storageBody ?: "Source path not found: ${sourcePath}"]
166 }
167
168 def storageInfo = new groovy.json.JsonSlurperClassic().parseText(storageBody)
169 def isDirectory = storageInfo.get('children') != null
170
171 if (dryRun) {
172 respText = "DRY RUN: Would ${copy ? 'copy' : 'move'} ${isDirectory ? 'directory' : 'file'} from ${sourcePath} to ${dstPath}"
173 return [200, respText]
174 }
175
176 if (isDirectory) {
177 // Handle directory: recursively copy all files using checksum deploy
178 def filesToCopy = getDirectoryFiles(artifactoryURL, sourcePath, credentialsId)
179 def errors = []
180
181 filesToCopy.each { filePath ->
182 def relativePath = filePath.replaceFirst("^${sourcePath}/", '')
183 def copyResult = copyFileByChecksum(artifactoryURL, filePath, "${dstPath}/${relativePath}", credentialsId)
184
185 if (copyResult[0] != 200) {
186 errors.add("Failed to copy ${filePath}: HTTP ${copyResult[0]} - ${copyResult[1]}")
187 respCode = copyResult[0]
188 }
189 }
190
191 // Delete source directory if move (not copy)
192 if (!copy && respCode == 200) {
193 def deleteResult = deleteItem(artifactoryURL, sourcePath)
194 if (deleteResult[0] != 200) {
195 errors.add("Failed to delete source directory: HTTP ${deleteResult[0]}")
196 respCode = deleteResult[0]
197 }
198 }
199
200 respText = errors ? errors.join('; ') : "Successfully ${copy ? 'copied' : 'moved'} directory from ${sourcePath} to ${dstPath}"
201 } else {
202 // Handle single file using checksum deploy
203 def copyResult = copyFileByChecksum(artifactoryURL, sourcePath, dstPath, credentialsId)
204 respCode = copyResult[0]
205 respText = copyResult[1]
206 // Delete source file if move (not copy)
207 if (!copy && respCode == 200) {
208 def deleteResult = deleteItem(artifactoryURL, sourcePath)
209 if (deleteResult[0] != 200) {
210 respCode = deleteResult[0]
211 respText = "Copied but failed to delete source: ${deleteResult[1]}"
212 } else {
213 respText = respText ?: "Successfully ${copy ? 'copied' : 'moved'} file from ${sourcePath} to ${dstPath}"
214 }
215 } else if (respCode == 200) {
216 respText = respText ?: "Successfully ${copy ? 'copied' : 'moved'} file from ${sourcePath} to ${dstPath}"
217 }
218 }
219 //If successful, rewrite the return code to the expected one
220 if ( respCode ==~ /^2\d{2}$/ ) {
221 respCode = 200
222 }
223 } catch (Exception e) {
224 respCode = 500
225 respText = "Error during ${copy ? 'copy' : 'move'} operation: ${e.getMessage()}"
226 }
227 }
228
229 return [respCode, respText]
230}
231
232/**
233 * Copy a file using checksum-based deploy API (no file download required)
234 * Uses JFrog REST API: PUT /artifactory/api/checksum/deploy/{repoKey}/{filePath}
235 *
236 * @param artifactoryURL String, an URL to Artifactory
237 * @param sourcePath String, a source path to the artifact including repository name
238 * @param dstPath String, a destination path to the artifact including repository name
239 * @return Array with [responseCode, responseText]
240 */
241def copyFileByChecksum(String artifactoryURL, String sourcePath, String dstPath, String credentialsId = 'artifactory') {
242 def respCode = 200
243 def respText = ''
244
245 withCredentials([
246 [$class : 'UsernamePasswordMultiBinding',
247 credentialsId : credentialsId,
248 passwordVariable: 'ARTIFACTORY_PASSWORD',
249 usernameVariable: 'ARTIFACTORY_LOGIN']
250 ]) {
251 // Get checksums from source file
252 def storageUrl = "${artifactoryURL}/api/storage/${sourcePath}"
253 def storageResult = sh(script: """
254 set +e
255 response=\$(curl -s -w "\\n%{http_code}" -X GET -u \${ARTIFACTORY_LOGIN}:\${ARTIFACTORY_PASSWORD} '${storageUrl}' 2>&1)
256 echo "\$response"
257 """, returnStdout: true).trim()
258
259 def storageLines = storageResult.split('\n')
260 def storageCode = storageLines.length > 0 && storageLines[-1] ==~ /^\d{3}$/ ? storageLines[-1].toInteger() : 404
261 def storageBody = storageLines.length > 1 ? storageLines[0..-2].join('\n') : ''
262
263 if (storageCode != 200) {
264 return [storageCode, storageBody ?: "Source file not found: ${sourcePath}"]
265 }
266
267 def storageInfo = new groovy.json.JsonSlurperClassic().parseText(storageBody)
268 def checksums = storageInfo.get('checksums', [:])
269 def md5 = checksums.get('md5', '')
270 def sha1 = checksums.get('sha1', '')
271 def sha256 = checksums.get('sha256', '')
272
273 if (!md5 && !sha1) {
274 return [500, "Source file has no checksums available: ${sourcePath}"]
275 }
276
277 // Use checksum deploy API to copy file without downloading
278 def deployUrl = "${artifactoryURL}/${dstPath}"
279 // Build curl command with headers
280 def curlHeaders = "-H \"X-Checksum-Deploy: true\""
281 if (sha1) {
282 curlHeaders += " -H \"X-Checksum-Sha1: ${sha1}\""
283 }
284 if (sha256) {
285 curlHeaders += " -H \"X-Checksum-Sha256: ${sha256}\""
286 }
287 if (md5) {
288 curlHeaders += " -H \"X-Checksum-Md5: ${md5}\""
289 }
290
291 def deployResult = sh(script: """
292 set +e
293 response=\$(curl -s -w "\\n%{http_code}" -X PUT -u \${ARTIFACTORY_LOGIN}:\${ARTIFACTORY_PASSWORD} ${curlHeaders} '${deployUrl}' 2>&1)
294 echo "\$response"
295 """, returnStdout: true).trim()
296
297 def deployLines = deployResult.split('\n')
298 respCode = deployLines.length > 0 && deployLines[-1] ==~ /^\d{3}$/ ? deployLines[-1].toInteger() : 500
299 respText = deployLines.length > 1 ? deployLines[0..-2].join('\n') : ''
300
301 if (respCode != 200 && !respText) {
302 respText = "Failed to deploy by checksum: HTTP ${respCode}"
303 }
304 }
305
306 return [respCode, respText]
307}
308
309/**
310 * Recursively get all files in a directory
311 *
312 * @param artifactoryURL String, an URL to Artifactory
313 * @param dirPath String, a directory path including repository name
314 * @return List of file paths
315 */
316def getDirectoryFiles(String artifactoryURL, String dirPath, String credentialsId = 'artifactory') {
317 def files = []
318 def storageUrl = "${artifactoryURL}/api/storage/${dirPath}"
319
320 withCredentials([
321 [$class : 'UsernamePasswordMultiBinding',
322 credentialsId : credentialsId,
323 passwordVariable: 'ARTIFACTORY_PASSWORD',
324 usernameVariable: 'ARTIFACTORY_LOGIN']
325 ]) {
326 def result = sh(script: "bash -c \"curl -X GET -u \${ARTIFACTORY_LOGIN}:\${ARTIFACTORY_PASSWORD} '${storageUrl}'\"",
327 returnStdout: true).trim()
328
329 def storageInfo = new groovy.json.JsonSlurperClassic().parseText(result)
330 def children = storageInfo.get('children', [])
331
332 children.each { child ->
333 def childPath = "${dirPath}/${child.uri.replaceAll('^/', '')}"
334 if (child.folder) {
335 files.addAll(getDirectoryFiles(artifactoryURL, childPath))
336 } else {
337 files.add(childPath)
338 }
339 }
340 }
341 return files
Sergey Kolekonovce616712019-09-10 16:09:23 +0400342}
343
344/**
345 * Recursively delete the specified artifact or a folder
346 *
347 * @param artifactoryURL String, an URL to Artifactory
348 * @param itemPath String, a source path to the item including repository name
349 */
350def deleteItem (String artifactoryURL, String itemPath) {
351 def url = "${artifactoryURL}/${itemPath}"
Alexandr Lovtsov89d24a52021-01-28 17:41:14 +0300352 def http = new com.mirantis.mk.Http()
353 return http.doDelete(url, 'artifactory')
Sergey Kolekonovce616712019-09-10 16:09:23 +0400354}
355
356/**
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300357 * Get properties for specified artifact in Artifactory
358 * Returns LinkedHashMap of properties
359 *
360 * @param artifactUrl String, an URL to artifact in Artifactory repo
361 */
362def getPropertiesForArtifact(String artifactUrl) {
363 def url = "${artifactUrl}?properties"
364 def result
365 withCredentials([
366 [$class : 'UsernamePasswordMultiBinding',
367 credentialsId : 'artifactory',
368 passwordVariable: 'ARTIFACTORY_PASSWORD',
369 usernameVariable: 'ARTIFACTORY_LOGIN']
370 ]) {
371 result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"",
372 returnStdout: true).trim()
373 }
374 def properties = new groovy.json.JsonSlurperClassic().parseText(result)
375 return properties.get("properties")
376}
377
378/**
vnaumov5b2dccf2019-10-10 22:12:15 +0200379 * Get checksums of artifact
380 *
381 * @param artifactoryUrl String, an URL ofArtifactory repo
382 * @param repoName Artifact repository name
383 * @param artifactName Artifactory object name
384 * @param checksumType Type of checksum (default md5)
385 */
386
387def getArtifactChecksum(artifactoryUrl, repoName, artifactName, checksumType = 'md5'){
388 def url = "${artifactoryUrl}/api/storage/${repoName}/${artifactName}"
389 withCredentials([
390 [$class : 'UsernamePasswordMultiBinding',
391 credentialsId : 'artifactory',
392 passwordVariable: 'ARTIFACTORY_PASSWORD',
393 usernameVariable: 'ARTIFACTORY_LOGIN']
394 ]) {
395 def result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"",
396 returnStdout: true).trim()
397 }
398
399 def properties = new groovy.json.JsonSlurperClassic().parseText(result)
400 return properties['checksums'][checksumType]
401}
402
403/**
Denis Egorenkoedd21dc2018-11-23 17:38:17 +0400404 * Check if image with tag exist by provided path
405 * Returns true or false
406 *
407 * @param artifactoryURL String, an URL to Artifactory
408 * @param imageRepo String, path to image to check, includes repo path and image name
409 * @param tag String, tag to check
410 * @param artifactoryCreds String, artifactory creds to use. Optional, default is 'artifactory'
411 */
412def imageExists(String artifactoryURL, String imageRepo, String tag, String artifactoryCreds = 'artifactory') {
Sergey Otpuschennikov406778f2019-10-10 14:49:40 +0400413 def url = artifactoryURL + '/v2/' + imageRepo + '/manifests/' + tag
Denis Egorenkoedd21dc2018-11-23 17:38:17 +0400414 def result
415 withCredentials([
416 [$class : 'UsernamePasswordMultiBinding',
417 credentialsId : artifactoryCreds,
418 passwordVariable: 'ARTIFACTORY_PASSWORD',
419 usernameVariable: 'ARTIFACTORY_LOGIN']
420 ]) {
421 result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"",
422 returnStdout: true).trim()
423 }
424 def properties = new groovy.json.JsonSlurperClassic().parseText(result)
425 return properties.get("errors") ? false : true
426}
427
428/**
Denis Egorenko7c0abfe2017-02-14 15:42:02 +0400429 * Find docker images by tag
430 * Returns Array of image' hashes with names as full path in @repo
431 *
432 * Example:
433 *
434 * [ {
435 * "path" : "mirantis/ccp/ci-cd/gerrit-manage/test"
436 * },
437 * {
438 * "path" : "mirantis/ccp/ci-cd/gerrit/test"
439 * }
440 * ]
441 *
442 * @param artifactoryURL String, an URL to Artifactory
443 * @param repo String, a name of repo where should be executed search
444 * @param tag String, tag of searched image
445 */
446def getImagesByTag(String artifactoryURL, String repo, String tag) {
447 def url = "${artifactoryURL}/api/search/aql"
448 def result
449 writeFile file: "query",
450 text: """\
451 items.find(
452 {
453 \"repo\": \"${repo}\",
454 \"@docker.manifest\": { \"\$match\" : \"${tag}*\" }
455 }
456 ).
457 include(\"path\")
458 """.stripIndent()
459 withCredentials([
460 [$class: 'UsernamePasswordMultiBinding',
461 credentialsId: 'artifactory',
462 passwordVariable: 'ARTIFACTORY_PASSWORD',
463 usernameVariable: 'ARTIFACTORY_LOGIN']
464 ]) {
465 result = sh(script: "bash -c \"curl -X POST -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -d @query \'${url}\'\"",
466 returnStdout: true).trim()
467 }
468 def images = new groovy.json.JsonSlurperClassic().parseText(result)
469 return images.get("results")
470}
471
472/**
Alexandr Lovtsova0295432021-01-28 17:20:32 +0300473 * Convert Mirantis docker image url/path to Mirantis artifactory path ready for use in API calls
474 *
475 * For example:
476 * 'docker-dev-kaas-local.docker.mirantis.net/mirantis/kaas/si-test:master' -> 'docker-dev-kaas-local/mirantis/kaas/si-test/master'
477 *
478 */
479def dockerImageToArtifactoryPath(String image) {
480 List imageParts = image.tokenize('/')
481 String repoName = imageParts[0].tokenize('.')[0]
482 String namespace = imageParts[1..-2].join('/')
483 String imageName = imageParts[-1].tokenize(':')[0]
484 String imageTag = imageParts[-1].tokenize(':')[1]
485
486 return [repoName, namespace, imageName, imageTag].join('/')
487}
488
489/**
Alexandr Lovtsov8fb5d7c2021-01-28 17:42:24 +0300490 * Copy docker image from one url to another
491 *
492 * @param srcImage String, Mirantis URL/path for docker image to copy from
493 * @param dstImage String, Mirantis URL/path for docker image to copy to
494 */
495def copyDockerImage(String srcImage, String dstImage) {
496 def artifactoryServer = Artifactory.server(env.ARTIFACTORY_SERVER ?: 'mcp-ci')
497 String srcPath = dockerImageToArtifactoryPath(srcImage)
498 String dstPath = dockerImageToArtifactoryPath(dstImage)
499
500 return moveItem(artifactoryServer.getUrl(), srcPath, dstPath, true)
501}
502
503/**
504 * Delete docker image on Mirantis's artifactory
505 *
506 * @param image String, Mirantis URL/path for docker image to delete
507 */
508def deleteDockerImage(String image) {
509 def artifactoryServer = Artifactory.server(env.ARTIFACTORY_SERVER ?: 'mcp-ci')
510
511 return deleteItem(artifactoryServer.getUrl() + '/artifactory', dockerImageToArtifactoryPath(image))
512}
513
514/**
Alexandr Lovtsovd3023d02021-05-12 15:54:44 +0300515 * Upload list of docker images to Artifactory
516 *
517 * @param server ArtifactoryServer, the instance of Artifactory server
518 * @param registry String, the name of Docker registry
519 * @param images List[Map], list of maps where each map consist of following fields:
520 * {
521 * 'repository': String '...', // (mandatory) The name of Artifactory Docker repository
522 * 'name': String '...', // (mandatory) docker image name
523 * 'tag': String '...', // (mandatory) docker image tag/version
524 * 'buildInfo': BuildInfo '...', // (optional) the instance of a build-info object which
525 * can be published, if it's not null (default),
526 * then we publish BuildInfo,
527 * 'properties': LinkedHashMap '...', // (optional) Map of artifactory properties to set for image,
528 * if not provided, then some common properties will be set
529 * }
530 *
531 */
532def uploadImagesToArtifactory(ArtifactoryServer server, String registry, List images) {
533 // Check that every provided image's specs contain mandatory fields (name, tag, repository)
534 images.each {
535 if (!(it.name && it.tag && it.repository)) {
536 error("Incorrect image upload spec: ${it}")
537 }
538 }
539
540 // TODO Switch to Artifactoy image' pushing mechanism once we will
541 // prepare automatical way for enabling artifactory build-proxy
542 //def artDocker
543 withCredentials([[
544 $class: 'UsernamePasswordMultiBinding',
545 credentialsId: env.ARTIFACTORY_CREDENTIALS_ID ?: 'artifactory',
546 passwordVariable: 'ARTIFACTORY_PASSWORD',
547 usernameVariable: 'ARTIFACTORY_LOGIN',
548 ]]) {
549 sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}")
550 //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}")
551 }
552
553 images.each {
554 String image = it.name // mandatory
555 String version = it.tag // mandatory
556 String repository = it.repository // mandatory
557
558 sh ("docker push ${registry}/${image}:${version}")
559 //artDocker.push("${registry}/${image}:${version}", repository)
560 def image_url = server.getUrl() + "/api/storage/${repository}/${image}/${version}"
561
562 LinkedHashMap properties = it.get('properties')
563 if ( ! properties ) {
564 properties = [
565 'com.mirantis.buildName':"${env.JOB_NAME}",
566 'com.mirantis.buildNumber': "${env.BUILD_NUMBER}",
567 'com.mirantis.gerritProject': "${env.GERRIT_PROJECT}",
568 'com.mirantis.gerritChangeNumber': "${env.GERRIT_CHANGE_NUMBER}",
569 'com.mirantis.gerritPatchsetNumber': "${env.GERRIT_PATCHSET_NUMBER}",
570 'com.mirantis.gerritChangeId': "${env.GERRIT_CHANGE_ID}",
571 'com.mirantis.gerritPatchsetRevision': "${env.GERRIT_PATCHSET_REVISION}",
572 'com.mirantis.targetImg': "${image}",
573 'com.mirantis.targetTag': "${version}"
574 ]
575 }
576
577 setProperties(image_url, properties)
578
579 BuildInfo buildInfo = it.get('buildInfo')
580 if ( buildInfo != null ) {
581 buildInfo.env.capture = true
582 buildInfo.env.filter.addInclude("*")
583 buildInfo.env.filter.addExclude("*PASSWORD*")
584 buildInfo.env.filter.addExclude("*password*")
585 buildInfo.env.collect()
586 server.publishBuildInfo(buildInfo)
587 }
588 }
589}
590/**
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300591 * Upload docker image to Artifactory
592 *
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200593 * @param server ArtifactoryServer, the instance of Artifactory server
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300594 * @param registry String, the name of Docker registry
595 * @param image String, Docker image name
596 * @param version String, Docker image version
597 * @param repository String, The name of Artifactory Docker repository
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200598 * @param buildInfo BuildInfo, the instance of a build-info object which can be published,
599 * if defined, then we publish BuildInfo
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300600 */
Sergey Kulanov8cd6d222016-11-17 13:42:47 +0200601def uploadImageToArtifactory (ArtifactoryServer server, String registry, String image,
602 String version, String repository,
Dmitry Burmistrov6ee39522017-05-22 12:46:25 +0400603 BuildInfo buildInfo = null,
604 LinkedHashMap properties = null) {
Alexandr Lovtsovd3023d02021-05-12 15:54:44 +0300605 Map images = [
606 'repository': repository,
607 'name': image,
608 'tag': version,
609 'buildInfo': buildInfo,
610 'properties': properties,
611 ]
612 uploadImagesToArtifactory(server, registry, [images])
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300613}
614
615/**
616 * Upload binaries to Artifactory
617 *
618 * @param server ArtifactoryServer, the instance of Artifactory server
619 * @param buildInfo BuildInfo, the instance of a build-info object which can be published
620 * @param uploadSpec String, a spec which is a JSON file that specifies which files should be
621 * uploaded or downloaded and the target path
622 * @param publishInfo Boolean, whether publish a build-info object to Artifactory
623 */
Sergey Kulanov91d8def2016-11-15 13:53:17 +0200624def uploadBinariesToArtifactory (ArtifactoryServer server, BuildInfo buildInfo, String uploadSpec,
625 Boolean publishInfo = false) {
Jakub Josefbefcf6c2017-11-14 18:03:10 +0100626 server.upload(uploadSpec, buildInfo)
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300627
628 if ( publishInfo ) {
629 buildInfo.env.capture = true
630 buildInfo.env.filter.addInclude("*")
631 buildInfo.env.filter.addExclude("*PASSWORD*")
632 buildInfo.env.filter.addExclude("*password*")
633 buildInfo.env.collect()
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300634 server.publishBuildInfo(buildInfo)
635 }
636}
637
638/**
639 * Promote Docker image artifact to release repo
640 *
641 * @param artifactoryURL String, an URL to Artifactory
642 * @param artifactoryDevRepo String, the source dev repository name
643 * @param artifactoryProdRepo String, the target repository for the move or copy
644 * @param dockerRepo String, the docker repository name to promote
645 * @param artifactTag String, an image tag name to promote
646 * @param targetTag String, target tag to assign the image after promotion
647 * @param copy Boolean, an optional value to set whether to copy instead of move
648 * Default: false
649 */
650def promoteDockerArtifact(String artifactoryURL, String artifactoryDevRepo,
651 String artifactoryProdRepo, String dockerRepo,
652 String artifactTag, String targetTag, Boolean copy = false) {
653 def url = "${artifactoryURL}/api/docker/${artifactoryDevRepo}/v2/promote"
Dmitry Burmistrov5deaa7d2017-05-30 17:12:54 +0400654 String queryFile = UUID.randomUUID().toString()
Dmitry Burmistrov97beb9b2017-05-29 17:21:34 +0400655 writeFile file: queryFile,
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300656 text: """{
657 \"targetRepo\": \"${artifactoryProdRepo}\",
658 \"dockerRepository\": \"${dockerRepo}\",
659 \"tag\": \"${artifactTag}\",
660 \"targetTag\" : \"${targetTag}\",
661 \"copy\": \"${copy}\"
662 }""".stripIndent()
Dmitry Burmistrov97beb9b2017-05-29 17:21:34 +0400663 sh "cat ${queryFile}"
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300664 withCredentials([
665 [$class : 'UsernamePasswordMultiBinding',
666 credentialsId : 'artifactory',
667 passwordVariable: 'ARTIFACTORY_PASSWORD',
668 usernameVariable: 'ARTIFACTORY_LOGIN']
669 ]) {
Sergey Reshetnyakf0775fb2018-06-28 14:54:01 +0400670 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 +0300671 }
Dmitry Burmistrov97beb9b2017-05-29 17:21:34 +0400672 sh "rm -v ${queryFile}"
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300673}
Denis Egorenko60f47c12019-03-11 20:54:13 +0400674
675/**
676 * Save job artifacts to Artifactory server if available.
677 * Returns link to Artifactory repo, where saved job artifacts.
678 *
679 * @param config LinkedHashMap which contains next parameters:
680 * @param artifactory String, Artifactory server id
681 * @param artifactoryRepo String, repo to save job artifacts
682 * @param buildProps ArrayList, additional props for saved artifacts. Optional, default: []
683 * @param artifactory_not_found_fail Boolean, whether to fail if provided artifactory
684 * id is not found or just print warning message. Optional, default: false
685 */
686def uploadJobArtifactsToArtifactory(LinkedHashMap config) {
687 def common = new com.mirantis.mk.Common()
688 def artifactsDescription = ''
689 def artifactoryServer
Dmitry Tyzhnenko9ade0722020-03-31 13:17:54 +0300690
691 if (!config.containsKey('deleteArtifacts')) {
692 config.deleteArtifacts = true // default behavior before add the flag
693 }
694
Denis Egorenko60f47c12019-03-11 20:54:13 +0400695 try {
696 artifactoryServer = Artifactory.server(config.get('artifactory'))
697 } catch (Exception e) {
698 if (config.get('artifactory_not_found_fail', false)) {
699 throw e
700 } else {
701 common.warningMsg(e)
702 return "Artifactory server is not found. Can't save artifacts in Artifactory."
703 }
704 }
Dmitry Tyzhnenko812673a2020-03-26 21:59:14 +0200705 def artifactDir = config.get('artifactDir') ?: 'cur_build_artifacts'
Denis Egorenko60f47c12019-03-11 20:54:13 +0400706 def user = ''
707 wrap([$class: 'BuildUser']) {
708 user = env.BUILD_USER_ID
709 }
710 dir(artifactDir) {
711 try {
Denis Egorenko5fc40f82019-03-13 18:35:51 +0400712 unarchive(mapping: ['**/*' : '.'])
Denis Egorenko60f47c12019-03-11 20:54:13 +0400713 // Mandatory and additional properties
714 def properties = getBinaryBuildProperties(config.get('buildProps', []) << "buildUser=${user}")
Dmitry Tyzhnenko812673a2020-03-26 21:59:14 +0200715 def pattern = config.get('artifactPattern') ?: '*'
Denis Egorenko60f47c12019-03-11 20:54:13 +0400716
717 // Build Artifactory spec object
718 def uploadSpec = """{
719 "files":
720 [
721 {
Dmitry Tyzhnenko812673a2020-03-26 21:59:14 +0200722 "pattern": "${pattern}",
Denis Egorenko60f47c12019-03-11 20:54:13 +0400723 "target": "${config.get('artifactoryRepo')}/",
Denis Egorenko850f56a2019-03-13 20:44:43 +0400724 "flat": false,
Denis Egorenko60f47c12019-03-11 20:54:13 +0400725 "props": "${properties}"
726 }
727 ]
728 }"""
729
730 artifactoryServer.upload(uploadSpec, newBuildInfo())
731 def linkUrl = "${artifactoryServer.getUrl()}/artifactory/${config.get('artifactoryRepo')}"
732 artifactsDescription = "Job artifacts uploaded to Artifactory: <a href=\"${linkUrl}\">${linkUrl}</a>"
733 } catch (Exception e) {
734 if (e =~ /no artifacts/) {
735 artifactsDescription = 'Build has no artifacts saved.'
736 } else {
737 throw e
738 }
739 } finally {
Dmitry Tyzhnenko9ade0722020-03-31 13:17:54 +0300740 if (config.deleteArtifacts) {
741 deleteDir()
742 }
Denis Egorenko60f47c12019-03-11 20:54:13 +0400743 }
744 }
745 return artifactsDescription
746}
Dmitry Tyzhnenko39cf09c2020-05-05 20:08:52 +0300747
748/**
749 * Save custom artifacts to Artifactory server if available.
750 * Returns link to Artifactory repo, where saved artifacts.
751 *
752 * @param config LinkedHashMap which contains next parameters:
753 * @param artifactory String, Artifactory server id
754 * @param artifactoryRepo String, repo to save job artifacts
755 * @param buildProps ArrayList, additional props for saved artifacts. Optional, default: []
756 * @param artifactory_not_found_fail Boolean, whether to fail if provided artifactory
757 * id is not found or just print warning message. Optional, default: false
758 */
759def uploadArtifactsToArtifactory(LinkedHashMap config) {
760 def common = new com.mirantis.mk.Common()
761 def artifactsDescription = ''
762 def artifactoryServer
763
764 try {
765 artifactoryServer = Artifactory.server(config.get('artifactory'))
766 } catch (Exception e) {
767 if (config.get('artifactory_not_found_fail', false)) {
768 throw e
769 } else {
770 common.warningMsg(e)
771 return "Artifactory server is not found. Can't save artifacts in Artifactory."
772 }
773 }
774 def user = ''
775 wrap([$class: 'BuildUser']) {
776 user = env.BUILD_USER_ID
777 }
778 try {
779 // Mandatory and additional properties
780 def properties = getBinaryBuildProperties(config.get('buildProps', []) << "buildUser=${user}")
781 def pattern = config.get('artifactPattern') ?: '*'
782
783 // Build Artifactory spec object
784 def uploadSpec = """{
785 "files":
786 [
787 {
788 "pattern": "${pattern}",
789 "target": "${config.get('artifactoryRepo')}/",
790 "flat": false,
791 "props": "${properties}"
792 }
793 ]
794 }"""
795
796 artifactoryServer.upload(uploadSpec, newBuildInfo())
Alexandr Lovtsov9293d992021-01-19 19:55:41 +0300797 def linkUrl = "${artifactoryServer.getUrl()}/${config.get('artifactoryRepo')}"
Dmitry Tyzhnenko39cf09c2020-05-05 20:08:52 +0300798 artifactsDescription = "Job artifacts uploaded to Artifactory: <a href=\"${linkUrl}\">${linkUrl}</a>"
799 } catch (Exception e) {
800 if (e =~ /no artifacts/) {
801 artifactsDescription = 'Build has no artifacts saved.'
802 } else {
803 throw e
804 }
805 }
806 return artifactsDescription
807}
Andrii Baraniukf9992ca2023-05-04 11:43:44 +0300808
809/**
810 * Get artifactory server object
811 *
812 * @param serverName Artifactory server name
813 */
814def getArtifactoryServer(serverName = ''){
815 if (!serverName) {
816 error ("Artifactory serverName must be specified")
817 }
818 return Artifactory.server(serverName)
819}