Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 1 | package com.mirantis.mcp |
| 2 | |
Sergey Kulanov | 91d8def | 2016-11-15 13:53:17 +0200 | [diff] [blame] | 3 | import org.jfrog.hudson.pipeline.types.ArtifactoryServer |
| 4 | import org.jfrog.hudson.pipeline.types.buildInfo.BuildInfo |
| 5 | |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 6 | /** |
| 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 | * */ |
| 13 | def getBinaryBuildProperties(ArrayList customProperties) { |
| 14 | def namespace = "com.mirantis." |
| 15 | def properties = [ |
Sergey Kulanov | c70f1c2 | 2016-11-16 13:05:20 +0200 | [diff] [blame] | 16 | "buildName=${env.JOB_NAME}", |
| 17 | "buildNumber=${env.BUILD_NUMBER}", |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 18 | "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 Mashchenko | 1d225c2 | 2018-06-19 13:52:17 +0300 | [diff] [blame] | 35 | * Get URL to artifact(s) by properties |
| 36 | * Returns String(s) with URL to found artifact or null if nothing |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 37 | * |
| 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 Mashchenko | 1d225c2 | 2018-06-19 13:52:17 +0300 | [diff] [blame] | 41 | * @param onlyLastItem Boolean, return only last URL if true(by default), |
| 42 | * else return list of all found artifact URLS |
| 43 | * |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 44 | */ |
Kirill Mashchenko | 1d225c2 | 2018-06-19 13:52:17 +0300 | [diff] [blame] | 45 | def uriByProperties(String artifactoryURL, LinkedHashMap properties, Boolean onlyLastItem=true) { |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 46 | 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 Mashchenko | 1d225c2 | 2018-06-19 13:52:17 +0300 | [diff] [blame] | 61 | 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 Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 70 | } else { |
| 71 | return null |
| 72 | } |
| 73 | } |
| 74 | |
Kirill Mashchenko | 1d225c2 | 2018-06-19 13:52:17 +0300 | [diff] [blame] | 75 | |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 76 | /** |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 77 | * 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 | */ |
| 85 | def 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 Evseev | bd40ef9 | 2017-10-18 12:24:45 +0300 | [diff] [blame] | 93 | properties_str += properties.collect({"${it.key}=${it.value}"}).join(';') |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 94 | 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 | */ |
| 111 | def 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 Egorenko | 7c0abfe | 2017-02-14 15:42:02 +0400 | [diff] [blame] | 128 | * 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 | */ |
| 145 | def 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 Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 172 | * Upload docker image to Artifactory |
| 173 | * |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame] | 174 | * @param server ArtifactoryServer, the instance of Artifactory server |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 175 | * @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 Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame] | 179 | * @param buildInfo BuildInfo, the instance of a build-info object which can be published, |
| 180 | * if defined, then we publish BuildInfo |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 181 | */ |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame] | 182 | def uploadImageToArtifactory (ArtifactoryServer server, String registry, String image, |
| 183 | String version, String repository, |
Dmitry Burmistrov | 6ee3952 | 2017-05-22 12:46:25 +0400 | [diff] [blame] | 184 | BuildInfo buildInfo = null, |
| 185 | LinkedHashMap properties = null) { |
Denis Egorenko | edba5a5 | 2016-11-15 19:55:56 +0300 | [diff] [blame] | 186 | // TODO Switch to Artifactoy image' pushing mechanism once we will |
| 187 | // prepare automatical way for enabling artifactory build-proxy |
| 188 | //def artDocker |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 189 | 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 Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 196 | //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}") |
| 197 | } |
| 198 | |
Denis Egorenko | edba5a5 | 2016-11-15 19:55:56 +0300 | [diff] [blame] | 199 | sh ("docker push ${registry}/${image}:${version}") |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 200 | //artDocker.push("${registry}/${image}:${version}", "${repository}") |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame] | 201 | def image_url = server.getUrl() + "/api/storage/${repository}/${image}/${version}" |
Dmitry Burmistrov | 6ee3952 | 2017-05-22 12:46:25 +0400 | [diff] [blame] | 202 | if ( ! properties ) { |
| 203 | properties = [ |
Sergey Kulanov | c70f1c2 | 2016-11-16 13:05:20 +0200 | [diff] [blame] | 204 | '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 Kulanov | 4d3951c | 2016-11-24 13:58:15 +0200 | [diff] [blame] | 211 | 'com.mirantis.targetImg': "${image}", |
Sergey Kulanov | c70f1c2 | 2016-11-16 13:05:20 +0200 | [diff] [blame] | 212 | 'com.mirantis.targetTag': "${version}" |
Dmitry Burmistrov | 6ee3952 | 2017-05-22 12:46:25 +0400 | [diff] [blame] | 213 | ] |
| 214 | } |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 215 | |
| 216 | setProperties(image_url, properties) |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame] | 217 | |
| 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 Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 226 | } |
| 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 Kulanov | 91d8def | 2016-11-15 13:53:17 +0200 | [diff] [blame] | 237 | def uploadBinariesToArtifactory (ArtifactoryServer server, BuildInfo buildInfo, String uploadSpec, |
| 238 | Boolean publishInfo = false) { |
Jakub Josef | befcf6c | 2017-11-14 18:03:10 +0100 | [diff] [blame] | 239 | server.upload(uploadSpec, buildInfo) |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 240 | |
| 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 Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 247 | 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 | */ |
| 263 | def 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 Burmistrov | 5deaa7d | 2017-05-30 17:12:54 +0400 | [diff] [blame] | 267 | String queryFile = UUID.randomUUID().toString() |
Dmitry Burmistrov | 97beb9b | 2017-05-29 17:21:34 +0400 | [diff] [blame] | 268 | writeFile file: queryFile, |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 269 | text: """{ |
| 270 | \"targetRepo\": \"${artifactoryProdRepo}\", |
| 271 | \"dockerRepository\": \"${dockerRepo}\", |
| 272 | \"tag\": \"${artifactTag}\", |
| 273 | \"targetTag\" : \"${targetTag}\", |
| 274 | \"copy\": \"${copy}\" |
| 275 | }""".stripIndent() |
Dmitry Burmistrov | 97beb9b | 2017-05-29 17:21:34 +0400 | [diff] [blame] | 276 | sh "cat ${queryFile}" |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 277 | withCredentials([ |
| 278 | [$class : 'UsernamePasswordMultiBinding', |
| 279 | credentialsId : 'artifactory', |
| 280 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 281 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 282 | ]) { |
Sergey Reshetnyak | f0775fb | 2018-06-28 14:54:01 +0400 | [diff] [blame^] | 283 | sh "bash -c \"curl --fail -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -H \"Content-Type:application/json\" -X POST -d @${queryFile} ${url}\"" |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 284 | } |
Dmitry Burmistrov | 97beb9b | 2017-05-29 17:21:34 +0400 | [diff] [blame] | 285 | sh "rm -v ${queryFile}" |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 286 | } |