blob: 8d76d8aa83c586755ff3879ed7e0f9801104aacc [file] [log] [blame]
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03001package com.mirantis.mcp
2
3/**
4 * Return string of mandatory build properties for binaries
5 * User can also add some custom properties.
6 *
7 * @param customProperties a Array of Strings that should be added to mandatory props
8 * in format ["prop1=value1", "prop2=value2"]
9 * */
10def getBinaryBuildProperties(ArrayList customProperties) {
11 def namespace = "com.mirantis."
12 def properties = [
13 "gerritProject=${env.GERRIT_PROJECT}",
14 "gerritChangeNumber=${env.GERRIT_CHANGE_NUMBER}",
15 "gerritPatchsetNumber=${env.GERRIT_PATCHSET_NUMBER}",
16 "gerritChangeId=${env.GERRIT_CHANGE_ID}",
17 "gerritPatchsetRevision=${env.GERRIT_PATCHSET_REVISION}"
18 ]
19
20 if (customProperties) {
21 properties.addAll(customProperties)
22 }
23
24 def common = new com.mirantis.mcp.Common()
25
26 return common.constructString(properties, namespace, ";")
27}
28
29/**
30 * Get URL to artifact by properties
31 * Returns String with URL to found artifact or null if nothing
32 *
33 * @param artifactoryURL String, an URL to Artifactory
34 * @param properties LinkedHashMap, a Hash of properties (key-value) which
35 * which should determine artifact in Artifactory
36 */
37def uriByProperties(String artifactoryURL, LinkedHashMap properties) {
38 def key, value
39 def properties_str = ''
40 for (int i = 0; i < properties.size(); i++) {
41 // avoid serialization errors
42 key = properties.entrySet().toArray()[i].key
43 value = properties.entrySet().toArray()[i].value
44 properties_str += "${key}=${value}&"
45 }
46 def search_url = "${artifactoryURL}/api/search/prop?${properties_str}"
47
48 def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"",
49 returnStdout: true).trim()
50 def content = new groovy.json.JsonSlurperClassic().parseText(result)
51 def uri = content.get("results")
52 if (uri) {
53 return uri.last().get("uri")
54 } else {
55 return null
56 }
57}
58
59/**
60* Get URL to artifact by properties
61* Returns String with URL to found artifact or null if nothing
62*
63* @param artifactoryURL String, an URL to Artifactory
64* @param properties String, URI in format prop1=val1&prop2=val2&prop3val3
65* which should determine artifact in Artifactory
66*/
67def uriByProperties(String artifactoryURL, String properties) {
68 def search_url = "${artifactoryURL}/api/search/prop?${properties}"
69
70 def result = sh(script: "bash -c \"curl -X GET \'${search_url}\'\"",
71 returnStdout: true).trim()
72 def content = new groovy.json.JsonSlurperClassic().parseText(result)
73 def uri = content.get("results")
74 if ( uri ) {
75 return uri.last().get("uri")
76 } else {
77 return null
78 }
79}
80
81/**
82 * Set properties for artifact in Artifactory repo
83 *
84 * @param artifactUrl String, an URL to artifact in Artifactory repo
85 * @param properties LinkedHashMap, a Hash of properties (key-value) which
86 * should be assigned for choosen artifact
87 * @param recursive Boolean, if artifact_url is a directory, whether to set
88 * properties recursively or not
89 */
90def setProperties(String artifactUrl, LinkedHashMap properties, Boolean recursive = false) {
91 def properties_str = 'properties='
92 def key, value
93 if (recursive) {
94 recursive = 'recursive=1'
95 } else {
96 recursive = 'recursive=0'
97 }
98 for (int i = 0; i < properties.size(); i++) {
99 // avoid serialization errors
100 key = properties.entrySet().toArray()[i].key
101 value = properties.entrySet().toArray()[i].value
102 properties_str += "${key}=${value}|"
103 }
104 def url = "${artifactUrl}?${properties_str}&${recursive}"
105 withCredentials([
106 [$class : 'UsernamePasswordMultiBinding',
107 credentialsId : 'artifactory',
108 passwordVariable: 'ARTIFACTORY_PASSWORD',
109 usernameVariable: 'ARTIFACTORY_LOGIN']
110 ]) {
111 sh "bash -c \"curl -X PUT -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\""
112 }
113}
114
115/**
116 * Get properties for specified artifact in Artifactory
117 * Returns LinkedHashMap of properties
118 *
119 * @param artifactUrl String, an URL to artifact in Artifactory repo
120 */
121def getPropertiesForArtifact(String artifactUrl) {
122 def url = "${artifactUrl}?properties"
123 def result
124 withCredentials([
125 [$class : 'UsernamePasswordMultiBinding',
126 credentialsId : 'artifactory',
127 passwordVariable: 'ARTIFACTORY_PASSWORD',
128 usernameVariable: 'ARTIFACTORY_LOGIN']
129 ]) {
130 result = sh(script: "bash -c \"curl -X GET -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} \'${url}\'\"",
131 returnStdout: true).trim()
132 }
133 def properties = new groovy.json.JsonSlurperClassic().parseText(result)
134 return properties.get("properties")
135}
136
137/**
138 * Upload docker image to Artifactory
139 *
140 * @param artifactoryURL String, an URL to Artifactory
141 * @param registry String, the name of Docker registry
142 * @param image String, Docker image name
143 * @param version String, Docker image version
144 * @param repository String, The name of Artifactory Docker repository
145 */
146def uploadImageToArtifactory(registry, image, version, repository) {
147 def server = Artifactory.server('mcp-ci')
148 def artDocker
149 withCredentials([
150 [$class: 'UsernamePasswordMultiBinding',
151 credentialsId: 'artifactory',
152 passwordVariable: 'ARTIFACTORY_PASSWORD',
153 usernameVariable: 'ARTIFACTORY_LOGIN']
154 ]) {
155 sh ("docker login -u ${ARTIFACTORY_LOGIN} -p ${ARTIFACTORY_PASSWORD} ${registry}")
156 sh ("docker push ${registry}/${image}:${version}")
157 //artDocker = Artifactory.docker("${env.ARTIFACTORY_LOGIN}", "${env.ARTIFACTORY_PASSWORD}")
158 }
159
160 //artDocker.push("${registry}/${image}:${version}", "${repository}")
161 def image_url = "${env.ARTIFACTORY_URL}/api/storage/${repository}/${image}/${version}"
162 def properties = ['com.mirantis.build_name':"${env.JOB_NAME}",
163 'com.mirantis.build_id': "${env.BUILD_NUMBER}",
164 'com.mirantis.changeid': "${env.GERRIT_CHANGE_ID}",
165 'com.mirantis.patchset_number': "${env.GERRIT_PATCHSET_NUMBER}",
166 'com.mirantis.target_tag': "${version}"]
167
168 setProperties(image_url, properties)
169}
170
171/**
172 * Upload binaries to Artifactory
173 *
174 * @param server ArtifactoryServer, the instance of Artifactory server
175 * @param buildInfo BuildInfo, the instance of a build-info object which can be published
176 * @param uploadSpec String, a spec which is a JSON file that specifies which files should be
177 * uploaded or downloaded and the target path
178 * @param publishInfo Boolean, whether publish a build-info object to Artifactory
179 */
180def uploadBinariesToArtifactory(uploadSpec, buildInfo, publishInfo=false) {
181 def server = Artifactory.server('mcp-ci')
182 buildInfo.append(server.upload(uploadSpec))
183
184 if ( publishInfo ) {
185 buildInfo.env.capture = true
186 buildInfo.env.filter.addInclude("*")
187 buildInfo.env.filter.addExclude("*PASSWORD*")
188 buildInfo.env.filter.addExclude("*password*")
189 buildInfo.env.collect()
190
191 server.publishBuildInfo(buildInfo)
192 }
193}
194
195/**
196 * Promote Docker image artifact to release repo
197 *
198 * @param artifactoryURL String, an URL to Artifactory
199 * @param artifactoryDevRepo String, the source dev repository name
200 * @param artifactoryProdRepo String, the target repository for the move or copy
201 * @param dockerRepo String, the docker repository name to promote
202 * @param artifactTag String, an image tag name to promote
203 * @param targetTag String, target tag to assign the image after promotion
204 * @param copy Boolean, an optional value to set whether to copy instead of move
205 * Default: false
206 */
207def promoteDockerArtifact(String artifactoryURL, String artifactoryDevRepo,
208 String artifactoryProdRepo, String dockerRepo,
209 String artifactTag, String targetTag, Boolean copy = false) {
210 def url = "${artifactoryURL}/api/docker/${artifactoryDevRepo}/v2/promote"
211 writeFile file: "query.json",
212 text: """{
213 \"targetRepo\": \"${artifactoryProdRepo}\",
214 \"dockerRepository\": \"${dockerRepo}\",
215 \"tag\": \"${artifactTag}\",
216 \"targetTag\" : \"${targetTag}\",
217 \"copy\": \"${copy}\"
218 }""".stripIndent()
219 sh "cat query.json"
220 withCredentials([
221 [$class : 'UsernamePasswordMultiBinding',
222 credentialsId : 'artifactory',
223 passwordVariable: 'ARTIFACTORY_PASSWORD',
224 usernameVariable: 'ARTIFACTORY_LOGIN']
225 ]) {
226 sh "bash -c \"curl -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -H \"Content-Type:application/json\" -X POST -d @query.json ${url}\""
227 }
228}