blob: 9443ed5105186c0f0ff8cca20ba8fb595b92acc3 [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 = [
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 */
40def 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*/
70def 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 */
93def 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 */
124def 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 */
Denis Egorenkoedba5a52016-11-15 19:55:56 +0300149def uploadImageToArtifactory (String artifactoryURL, String registry, String image,
150 String version, String repository) {
151 // TODO Switch to Artifactoy image' pushing mechanism once we will
152 // prepare automatical way for enabling artifactory build-proxy
153 //def artDocker
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300154 withCredentials([
155 [$class: 'UsernamePasswordMultiBinding',
156 credentialsId: 'artifactory',
157 passwordVariable: 'ARTIFACTORY_PASSWORD',
158 usernameVariable: 'ARTIFACTORY_LOGIN']
159 ]) {
160 sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}")
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300161 //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}")
162 }
163
Denis Egorenkoedba5a52016-11-15 19:55:56 +0300164 sh ("docker push ${registry}/${image}:${version}")
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300165 //artDocker.push("${registry}/${image}:${version}", "${repository}")
Denis Egorenkoedba5a52016-11-15 19:55:56 +0300166 def image_url = "${artifactoryURL}/api/storage/${repository}/${image}/${version}"
167
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300168 def properties = ['com.mirantis.build_name':"${env.JOB_NAME}",
169 'com.mirantis.build_id': "${env.BUILD_NUMBER}",
170 'com.mirantis.changeid': "${env.GERRIT_CHANGE_ID}",
171 'com.mirantis.patchset_number': "${env.GERRIT_PATCHSET_NUMBER}",
172 'com.mirantis.target_tag': "${version}"]
173
174 setProperties(image_url, properties)
175}
176
177/**
178 * Upload binaries to Artifactory
179 *
180 * @param server ArtifactoryServer, the instance of Artifactory server
181 * @param buildInfo BuildInfo, the instance of a build-info object which can be published
182 * @param uploadSpec String, a spec which is a JSON file that specifies which files should be
183 * uploaded or downloaded and the target path
184 * @param publishInfo Boolean, whether publish a build-info object to Artifactory
185 */
Sergey Kulanov91d8def2016-11-15 13:53:17 +0200186def uploadBinariesToArtifactory (ArtifactoryServer server, BuildInfo buildInfo, String uploadSpec,
187 Boolean publishInfo = false) {
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300188 buildInfo.append(server.upload(uploadSpec))
189
190 if ( publishInfo ) {
191 buildInfo.env.capture = true
192 buildInfo.env.filter.addInclude("*")
193 buildInfo.env.filter.addExclude("*PASSWORD*")
194 buildInfo.env.filter.addExclude("*password*")
195 buildInfo.env.collect()
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +0300196 server.publishBuildInfo(buildInfo)
197 }
198}
199
200/**
201 * Promote Docker image artifact to release repo
202 *
203 * @param artifactoryURL String, an URL to Artifactory
204 * @param artifactoryDevRepo String, the source dev repository name
205 * @param artifactoryProdRepo String, the target repository for the move or copy
206 * @param dockerRepo String, the docker repository name to promote
207 * @param artifactTag String, an image tag name to promote
208 * @param targetTag String, target tag to assign the image after promotion
209 * @param copy Boolean, an optional value to set whether to copy instead of move
210 * Default: false
211 */
212def promoteDockerArtifact(String artifactoryURL, String artifactoryDevRepo,
213 String artifactoryProdRepo, String dockerRepo,
214 String artifactTag, String targetTag, Boolean copy = false) {
215 def url = "${artifactoryURL}/api/docker/${artifactoryDevRepo}/v2/promote"
216 writeFile file: "query.json",
217 text: """{
218 \"targetRepo\": \"${artifactoryProdRepo}\",
219 \"dockerRepository\": \"${dockerRepo}\",
220 \"tag\": \"${artifactTag}\",
221 \"targetTag\" : \"${targetTag}\",
222 \"copy\": \"${copy}\"
223 }""".stripIndent()
224 sh "cat query.json"
225 withCredentials([
226 [$class : 'UsernamePasswordMultiBinding',
227 credentialsId : 'artifactory',
228 passwordVariable: 'ARTIFACTORY_PASSWORD',
229 usernameVariable: 'ARTIFACTORY_LOGIN']
230 ]) {
231 sh "bash -c \"curl -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -H \"Content-Type:application/json\" -X POST -d @query.json ${url}\""
232 }
233}