blob: 3a33da6e16bd659b86c23d208a00a5513d352924 [file] [log] [blame]
Alexander Evseev8b9d67d2017-12-11 17:12:45 +01001#!groovy
2
3/**
4 *
5 * Promote docker image from one artifactory repository (development) to
6 * another (production)
7 *
8 * Expected parameters:
9 * REPO_SRC Source Artifactory repository (default 'docker-dev-local')
10 * REPO_DST Destination Artifactory repository (default 'docker-prod-local')
11 * IMAGE_SRC Source image name (without docker registry!) to promote (required)
12 * IMAGE_DST Destination image (default same as IMAGE_SRC)
13 *
14 * COPY_IMAGE Copy image instead of moving (default 'true')
15 *
16 * ARTIFACTORY_URL Base URL of Artifactory instance, i.e. without `/api/...` path.
17 * (default 'https://artifactory.mcp.mirantis.net/artifactory/')
18 * ARTIFACTORY_CREDS Credentials to login into Artifactory (default 'artifactory')
19 *
20 * SLAVE_LABEL Label of the slave to run job (default 'master')
21 *
22 * Slave requirements: curl installed
23 *
24 */
25
26import groovy.json.JsonOutput
27
28String repo_src = env.REPO_SRC ?: 'docker-dev-local'
29String repo_dst = env.REPO_DST ?: 'docker-prod-local'
30String image_src = env.IMAGE_SRC
31String image_dst = env.IMAGE_DST ?: env.IMAGE_SRC
32
33boolean copy_image = env.COPY_IMAGE.asBoolean() ?: true
34
35String artifactory_url = env.ARTIFACTORY_URL ?: 'https://artifactory.mcp.mirantis.net/artifactory/'
36String artifactory_creds = env.ARTIFACTORY_CREDS ?: 'artifactory'
37
38String slave_label = env.SLAVE_LABEL ?: 'master'
39
40// Delimiter for splitting docker image name and tag (to avoid codeNarc DRY warning)
41String _colon = ':'
42
43String img_src_name, img_src_tag
44String img_dst_name, img_dst_tag
Jakub Josefa63f9862018-01-11 17:58:38 +010045timeout(time: 12, unit: 'HOURS') {
46 node(slave_label) {
47 (img_src_name, img_src_tag) = image_src.tokenize(_colon)
48 (img_dst_name, img_dst_tag) = image_dst.tokenize(_colon)
Alexander Evseev8b9d67d2017-12-11 17:12:45 +010049
Jakub Josefa63f9862018-01-11 17:58:38 +010050 String api_req = JsonOutput.toJson([
51 targetRepo: repo_dst,
52 dockerRepository: img_src_name,
53 targetDockerRepository: img_dst_name,
54 tag: img_src_tag,
55 targetTag: img_dst_tag,
56 copy: copy_image,
57 ])
Alexander Evseev8b9d67d2017-12-11 17:12:45 +010058
Jakub Josefa63f9862018-01-11 17:58:38 +010059 withCredentials([usernameColonPassword(credentialsId: artifactory_creds, variable: 'USERPASS')]) {
60 sh """
61 curl -fLsS \
62 -u \$USERPASS \
63 -X POST -d '${api_req}' -H 'Content-Type: application/json' \
64 '${artifactory_url}api/docker/${repo_src}/v2/promote'
65 """
66 }
Alexander Evseev8b9d67d2017-12-11 17:12:45 +010067 }
68}