Alexander Evseev | 8b9d67d | 2017-12-11 17:12:45 +0100 | [diff] [blame] | 1 | #!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 | |
| 26 | import groovy.json.JsonOutput |
| 27 | |
| 28 | String repo_src = env.REPO_SRC ?: 'docker-dev-local' |
| 29 | String repo_dst = env.REPO_DST ?: 'docker-prod-local' |
| 30 | String image_src = env.IMAGE_SRC |
| 31 | String image_dst = env.IMAGE_DST ?: env.IMAGE_SRC |
| 32 | |
| 33 | boolean copy_image = env.COPY_IMAGE.asBoolean() ?: true |
| 34 | |
| 35 | String artifactory_url = env.ARTIFACTORY_URL ?: 'https://artifactory.mcp.mirantis.net/artifactory/' |
| 36 | String artifactory_creds = env.ARTIFACTORY_CREDS ?: 'artifactory' |
| 37 | |
| 38 | String slave_label = env.SLAVE_LABEL ?: 'master' |
| 39 | |
| 40 | // Delimiter for splitting docker image name and tag (to avoid codeNarc DRY warning) |
| 41 | String _colon = ':' |
| 42 | |
| 43 | String img_src_name, img_src_tag |
| 44 | String img_dst_name, img_dst_tag |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 45 | timeout(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 Evseev | 8b9d67d | 2017-12-11 17:12:45 +0100 | [diff] [blame] | 49 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 50 | 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 Evseev | 8b9d67d | 2017-12-11 17:12:45 +0100 | [diff] [blame] | 58 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 59 | 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 Evseev | 8b9d67d | 2017-12-11 17:12:45 +0100 | [diff] [blame] | 67 | } |
| 68 | } |