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 = [ |
| 16 | "gerritProject=${env.GERRIT_PROJECT}", |
| 17 | "gerritChangeNumber=${env.GERRIT_CHANGE_NUMBER}", |
| 18 | "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}", |
| 19 | "gerritChangeId=${env.GERRIT_CHANGE_ID}", |
| 20 | "gerritPatchsetRevision=${env.GERRIT_PATCHSET_REVISION}" |
| 21 | ] |
| 22 | |
| 23 | if (customProperties) { |
| 24 | properties.addAll(customProperties) |
| 25 | } |
| 26 | |
| 27 | def common = new com.mirantis.mcp.Common() |
| 28 | |
| 29 | return common.constructString(properties, namespace, ";") |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get URL to artifact by properties |
| 34 | * Returns String with URL to found artifact or null if nothing |
| 35 | * |
| 36 | * @param artifactoryURL String, an URL to Artifactory |
| 37 | * @param properties LinkedHashMap, a Hash of properties (key-value) which |
| 38 | * which should determine artifact in Artifactory |
| 39 | */ |
| 40 | def uriByProperties(String artifactoryURL, LinkedHashMap properties) { |
| 41 | def key, value |
| 42 | def properties_str = '' |
| 43 | for (int i = 0; i < properties.size(); i++) { |
| 44 | // avoid serialization errors |
| 45 | key = properties.entrySet().toArray()[i].key |
| 46 | value = properties.entrySet().toArray()[i].value |
| 47 | properties_str += "${key}=${value}&" |
| 48 | } |
| 49 | def search_url = "${artifactoryURL}/api/search/prop?${properties_str}" |
| 50 | |
| 51 | def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"", |
| 52 | returnStdout: true).trim() |
| 53 | def content = new groovy.json.JsonSlurperClassic().parseText(result) |
| 54 | def uri = content.get("results") |
| 55 | if (uri) { |
| 56 | return uri.last().get("uri") |
| 57 | } else { |
| 58 | return null |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get URL to artifact by properties |
| 64 | * Returns String with URL to found artifact or null if nothing |
| 65 | * |
| 66 | * @param artifactoryURL String, an URL to Artifactory |
| 67 | * @param properties String, URI in format prop1=val1&prop2=val2&prop3val3 |
| 68 | * which should determine artifact in Artifactory |
| 69 | */ |
| 70 | def uriByProperties(String artifactoryURL, String properties) { |
| 71 | def search_url = "${artifactoryURL}/api/search/prop?${properties}" |
| 72 | |
| 73 | def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"", |
| 74 | returnStdout: true).trim() |
| 75 | def content = new groovy.json.JsonSlurperClassic().parseText(result) |
| 76 | def uri = content.get("results") |
| 77 | if ( uri ) { |
| 78 | return uri.last().get("uri") |
| 79 | } else { |
| 80 | return null |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set properties for artifact in Artifactory repo |
| 86 | * |
| 87 | * @param artifactUrl String, an URL to artifact in Artifactory repo |
| 88 | * @param properties LinkedHashMap, a Hash of properties (key-value) which |
| 89 | * should be assigned for choosen artifact |
| 90 | * @param recursive Boolean, if artifact_url is a directory, whether to set |
| 91 | * properties recursively or not |
| 92 | */ |
| 93 | def setProperties(String artifactUrl, LinkedHashMap properties, Boolean recursive = false) { |
| 94 | def properties_str = 'properties=' |
| 95 | def key, value |
| 96 | if (recursive) { |
| 97 | recursive = 'recursive=1' |
| 98 | } else { |
| 99 | recursive = 'recursive=0' |
| 100 | } |
| 101 | for (int i = 0; i < properties.size(); i++) { |
| 102 | // avoid serialization errors |
| 103 | key = properties.entrySet().toArray()[i].key |
| 104 | value = properties.entrySet().toArray()[i].value |
| 105 | properties_str += "${key}=${value}|" |
| 106 | } |
| 107 | def url = "${artifactUrl}?${properties_str}&${recursive}" |
| 108 | withCredentials([ |
| 109 | [$class : 'UsernamePasswordMultiBinding', |
| 110 | credentialsId : 'artifactory', |
| 111 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 112 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 113 | ]) { |
| 114 | sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"" |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get properties for specified artifact in Artifactory |
| 120 | * Returns LinkedHashMap of properties |
| 121 | * |
| 122 | * @param artifactUrl String, an URL to artifact in Artifactory repo |
| 123 | */ |
| 124 | def getPropertiesForArtifact(String artifactUrl) { |
| 125 | def url = "${artifactUrl}?properties" |
| 126 | def result |
| 127 | withCredentials([ |
| 128 | [$class : 'UsernamePasswordMultiBinding', |
| 129 | credentialsId : 'artifactory', |
| 130 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 131 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 132 | ]) { |
| 133 | result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"", |
| 134 | returnStdout: true).trim() |
| 135 | } |
| 136 | def properties = new groovy.json.JsonSlurperClassic().parseText(result) |
| 137 | return properties.get("properties") |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Upload docker image to Artifactory |
| 142 | * |
| 143 | * @param artifactoryURL String, an URL to Artifactory |
| 144 | * @param registry String, the name of Docker registry |
| 145 | * @param image String, Docker image name |
| 146 | * @param version String, Docker image version |
| 147 | * @param repository String, The name of Artifactory Docker repository |
| 148 | */ |
| 149 | def uploadImageToArtifactory(registry, image, version, repository) { |
| 150 | def server = Artifactory.server('mcp-ci') |
| 151 | def artDocker |
| 152 | withCredentials([ |
| 153 | [$class: 'UsernamePasswordMultiBinding', |
| 154 | credentialsId: 'artifactory', |
| 155 | passwordVariable: 'ARTIFACTORY_PASSWORD', |
| 156 | usernameVariable: 'ARTIFACTORY_LOGIN'] |
| 157 | ]) { |
| 158 | sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}") |
| 159 | sh ("docker push ${registry}/${image}:${version}") |
| 160 | //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}") |
| 161 | } |
| 162 | |
| 163 | //artDocker.push("${registry}/${image}:${version}", "${repository}") |
| 164 | def image_url = "${env.ARTIFACTORY_URL}/api/storage/${repository}/${image}/${version}" |
| 165 | def properties = ['com.mirantis.build_name':"${env.JOB_NAME}", |
| 166 | 'com.mirantis.build_id': "${env.BUILD_NUMBER}", |
| 167 | 'com.mirantis.changeid': "${env.GERRIT_CHANGE_ID}", |
| 168 | 'com.mirantis.patchset_number': "${env.GERRIT_PATCHSET_NUMBER}", |
| 169 | 'com.mirantis.target_tag': "${version}"] |
| 170 | |
| 171 | setProperties(image_url, properties) |
| 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 | } |