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 | /** |
| 35 | * Get URL to artifact by properties |
| 36 | * Returns String with URL to found artifact or null if nothing |
| 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 |
| 41 | */ |
| 42 | def uriByProperties(String artifactoryURL, LinkedHashMap properties) { |
| 43 | def key, value |
| 44 | def properties_str = '' |
| 45 | for (int i = 0; i < properties.size(); i++) { |
| 46 | // avoid serialization errors |
| 47 | key = properties.entrySet().toArray()[i].key |
| 48 | value = properties.entrySet().toArray()[i].value |
| 49 | properties_str += "${key}=${value}&" |
| 50 | } |
| 51 | def search_url = "${artifactoryURL}/api/search/prop?${properties_str}" |
| 52 | |
| 53 | def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"", |
| 54 | returnStdout: true).trim() |
| 55 | def content = new groovy.json.JsonSlurperClassic().parseText(result) |
| 56 | def uri = content.get("results") |
| 57 | if (uri) { |
| 58 | return uri.last().get("uri") |
| 59 | } else { |
| 60 | return null |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 65 | * Set properties for artifact in Artifactory repo |
| 66 | * |
| 67 | * @param artifactUrl String, an URL to artifact in Artifactory repo |
| 68 | * @param properties LinkedHashMap, a Hash of properties (key-value) which |
| 69 | * should be assigned for choosen artifact |
| 70 | * @param recursive Boolean, if artifact_url is a directory, whether to set |
| 71 | * properties recursively or not |
| 72 | */ |
| 73 | def setProperties(String artifactUrl, LinkedHashMap properties, Boolean recursive = false) { |
| 74 | def properties_str = 'properties=' |
| 75 | def key, value |
| 76 | if (recursive) { |
| 77 | recursive = 'recursive=1' |
| 78 | } else { |
| 79 | recursive = 'recursive=0' |
| 80 | } |
| 81 | for (int i = 0; i < properties.size(); i++) { |
| 82 | // avoid serialization errors |
| 83 | key = properties.entrySet().toArray()[i].key |
| 84 | value = properties.entrySet().toArray()[i].value |
| 85 | properties_str += "${key}=${value}|" |
| 86 | } |
| 87 | def url = "${artifactUrl}?${properties_str}&${recursive}" |
| 88 | withCredentials([ |
| 89 | [$class : 'UsernamePasswordMultiBinding', |
| 90 | credentialsId : 'artifactory', |
| 91 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 92 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 93 | ]) { |
| 94 | sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"" |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get properties for specified artifact in Artifactory |
| 100 | * Returns LinkedHashMap of properties |
| 101 | * |
| 102 | * @param artifactUrl String, an URL to artifact in Artifactory repo |
| 103 | */ |
| 104 | def getPropertiesForArtifact(String artifactUrl) { |
| 105 | def url = "${artifactUrl}?properties" |
| 106 | def result |
| 107 | withCredentials([ |
| 108 | [$class : 'UsernamePasswordMultiBinding', |
| 109 | credentialsId : 'artifactory', |
| 110 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 111 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 112 | ]) { |
| 113 | result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"", |
| 114 | returnStdout: true).trim() |
| 115 | } |
| 116 | def properties = new groovy.json.JsonSlurperClassic().parseText(result) |
| 117 | return properties.get("properties") |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Upload docker image to Artifactory |
| 122 | * |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame^] | 123 | * @param server ArtifactoryServer, the instance of Artifactory server |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 124 | * @param registry String, the name of Docker registry |
| 125 | * @param image String, Docker image name |
| 126 | * @param version String, Docker image version |
| 127 | * @param repository String, The name of Artifactory Docker repository |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame^] | 128 | * @param buildInfo BuildInfo, the instance of a build-info object which can be published, |
| 129 | * if defined, then we publish BuildInfo |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 130 | */ |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame^] | 131 | def uploadImageToArtifactory (ArtifactoryServer server, String registry, String image, |
| 132 | String version, String repository, |
| 133 | BuildInfo buildInfo = null) { |
Denis Egorenko | edba5a5 | 2016-11-15 19:55:56 +0300 | [diff] [blame] | 134 | // TODO Switch to Artifactoy image' pushing mechanism once we will |
| 135 | // prepare automatical way for enabling artifactory build-proxy |
| 136 | //def artDocker |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 137 | withCredentials([ |
| 138 | [$class: 'UsernamePasswordMultiBinding', |
| 139 | credentialsId: 'artifactory', |
| 140 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 141 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 142 | ]) { |
| 143 | sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}") |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 144 | //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}") |
| 145 | } |
| 146 | |
Denis Egorenko | edba5a5 | 2016-11-15 19:55:56 +0300 | [diff] [blame] | 147 | sh ("docker push ${registry}/${image}:${version}") |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 148 | //artDocker.push("${registry}/${image}:${version}", "${repository}") |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame^] | 149 | def image_url = server.getUrl() + "/api/storage/${repository}/${image}/${version}" |
Denis Egorenko | edba5a5 | 2016-11-15 19:55:56 +0300 | [diff] [blame] | 150 | |
Sergey Kulanov | c70f1c2 | 2016-11-16 13:05:20 +0200 | [diff] [blame] | 151 | def properties = [ |
| 152 | 'com.mirantis.buildName':"${env.JOB_NAME}", |
| 153 | 'com.mirantis.buildNumber': "${env.BUILD_NUMBER}", |
| 154 | 'com.mirantis.gerritProject': "${env.GERRIT_PROJECT}", |
| 155 | 'com.mirantis.gerritChangeNumber': "${env.GERRIT_CHANGE_NUMBER}", |
| 156 | 'com.mirantis.gerritPatchsetNumber': "${env.GERRIT_PATCHSET_NUMBER}", |
| 157 | 'com.mirantis.gerritChangeId': "${env.GERRIT_CHANGE_ID}", |
| 158 | 'com.mirantis.gerritPatchsetRevision': "${env.GERRIT_PATCHSET_REVISION}", |
| 159 | 'com.mirantis.targetTag': "${version}" |
| 160 | ] |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 161 | |
| 162 | setProperties(image_url, properties) |
Sergey Kulanov | 8cd6d22 | 2016-11-17 13:42:47 +0200 | [diff] [blame^] | 163 | |
| 164 | if ( buildInfo != null ) { |
| 165 | buildInfo.env.capture = true |
| 166 | buildInfo.env.filter.addInclude("*") |
| 167 | buildInfo.env.filter.addExclude("*PASSWORD*") |
| 168 | buildInfo.env.filter.addExclude("*password*") |
| 169 | buildInfo.env.collect() |
| 170 | server.publishBuildInfo(buildInfo) |
| 171 | } |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Upload binaries to Artifactory |
| 176 | * |
| 177 | * @param server ArtifactoryServer, the instance of Artifactory server |
| 178 | * @param buildInfo BuildInfo, the instance of a build-info object which can be published |
| 179 | * @param uploadSpec String, a spec which is a JSON file that specifies which files should be |
| 180 | * uploaded or downloaded and the target path |
| 181 | * @param publishInfo Boolean, whether publish a build-info object to Artifactory |
| 182 | */ |
Sergey Kulanov | 91d8def | 2016-11-15 13:53:17 +0200 | [diff] [blame] | 183 | def uploadBinariesToArtifactory (ArtifactoryServer server, BuildInfo buildInfo, String uploadSpec, |
| 184 | Boolean publishInfo = false) { |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 185 | buildInfo.append(server.upload(uploadSpec)) |
| 186 | |
| 187 | if ( publishInfo ) { |
| 188 | buildInfo.env.capture = true |
| 189 | buildInfo.env.filter.addInclude("*") |
| 190 | buildInfo.env.filter.addExclude("*PASSWORD*") |
| 191 | buildInfo.env.filter.addExclude("*password*") |
| 192 | buildInfo.env.collect() |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 193 | server.publishBuildInfo(buildInfo) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Promote Docker image artifact to release repo |
| 199 | * |
| 200 | * @param artifactoryURL String, an URL to Artifactory |
| 201 | * @param artifactoryDevRepo String, the source dev repository name |
| 202 | * @param artifactoryProdRepo String, the target repository for the move or copy |
| 203 | * @param dockerRepo String, the docker repository name to promote |
| 204 | * @param artifactTag String, an image tag name to promote |
| 205 | * @param targetTag String, target tag to assign the image after promotion |
| 206 | * @param copy Boolean, an optional value to set whether to copy instead of move |
| 207 | * Default: false |
| 208 | */ |
| 209 | def promoteDockerArtifact(String artifactoryURL, String artifactoryDevRepo, |
| 210 | String artifactoryProdRepo, String dockerRepo, |
| 211 | String artifactTag, String targetTag, Boolean copy = false) { |
| 212 | def url = "${artifactoryURL}/api/docker/${artifactoryDevRepo}/v2/promote" |
| 213 | writeFile file: "query.json", |
| 214 | text: """{ |
| 215 | \"targetRepo\": \"${artifactoryProdRepo}\", |
| 216 | \"dockerRepository\": \"${dockerRepo}\", |
| 217 | \"tag\": \"${artifactTag}\", |
| 218 | \"targetTag\" : \"${targetTag}\", |
| 219 | \"copy\": \"${copy}\" |
| 220 | }""".stripIndent() |
| 221 | sh "cat query.json" |
| 222 | withCredentials([ |
| 223 | [$class : 'UsernamePasswordMultiBinding', |
| 224 | credentialsId : 'artifactory', |
| 225 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 226 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 227 | ]) { |
| 228 | sh "bash -c \"curl -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -H \"Content-Type:application/json\" -X POST -d @query.json ${url}\"" |
| 229 | } |
| 230 | } |