Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Mirror Docker images |
| 4 | * |
| 5 | * Expected parameters: |
| 6 | * TARGET_REGISTRY_CREDENTIALS_ID Credentials for target Docker Registry |
| 7 | * TARGET_REGISTRY Target Docker Registry name |
| 8 | * REGISTRY_URL Target Docker Registry URL |
| 9 | * IMAGE_TAG Tag to use when pushing images |
azvyagintsev | d162b91 | 2018-07-26 10:52:14 +0200 | [diff] [blame] | 10 | * SOURCE_IMAGE_TAG Tag to use when pulling images(optional,if SOURCE_IMAGE_TAG has been found) |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 11 | * SET_DEFAULT_ARTIFACTORY_PROPERTIES Add extra props. directly to artifactory, |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 12 | * IMAGE_LIST List of images to mirror |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 13 | * Example: docker.elastic.co/elasticsearch/elasticsearch:5.4.1 docker-prod-local.docker.mirantis.net/mirantis/external/docker.elastic.co/elasticsearch |
| 14 | * docker.elastic.co/elasticsearch/elasticsearch:SUBS_SOURCE_IMAGE_TAG docker-prod-local.docker.mirantis.net/mirantis/external/elasticsearch:${IMAGE_TAG}* Will be proceed like: |
| 15 | * docker tag docker.elastic.co/elasticsearch/elasticsearch:5.4.1 docker-prod-local.docker.mirantis.net/mirantis/external/docker.elastic.co/elasticsearch/elasticsearch:5.4.1 |
| 16 | * |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 17 | * |
| 18 | */ |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 19 | import java.util.regex.Pattern |
| 20 | import groovy.json.JsonSlurper |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 21 | |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 22 | common = new com.mirantis.mk.Common() |
| 23 | external = false |
| 24 | externalMarker = '/mirantis/external/' |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 25 | |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 26 | slaveNode = env.SLAVE_NODE ?: 'docker' |
| 27 | setDefaultArtifactoryProperties = env.SET_DEFAULT_ARTIFACTORY_PROPERTIES ?: true |
| 28 | |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 29 | def getImageName(String image) { |
| 30 | def regex = Pattern.compile('(?:.+/)?([^:]+)(?::.+)?') |
| 31 | def matcher = regex.matcher(image) |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 32 | if (matcher.find()) { |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 33 | def imageName = matcher.group(1) |
| 34 | return imageName |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 35 | } else { |
| 36 | error("Wrong format of image name.") |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 37 | } |
| 38 | } |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 39 | |
| 40 | timeout(time: 4, unit: 'HOURS') { |
| 41 | node(slaveNode) { |
Denis Egorenko | 899ee32 | 2018-11-19 17:35:50 +0400 | [diff] [blame] | 42 | def user = '' |
| 43 | wrap([$class: 'BuildUser']) { |
| 44 | user = env.BUILD_USER_ID |
| 45 | } |
| 46 | currentBuild.description = "${user}: [${env.SOURCE_IMAGE_TAG} => ${env.IMAGE_TAG}]\n${env.IMAGE_LIST}" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 47 | try { |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 48 | stage("Mirror Docker Images") { |
| 49 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 50 | def images = IMAGE_LIST.tokenize('\n') |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 51 | def imageName, sourceImage, targetRegistryPath, imageArray |
| 52 | for (image in images) { |
| 53 | if (image.trim().indexOf(' ') == -1) { |
| 54 | error("Wrong format of image and target repository input") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 55 | } |
| 56 | imageArray = image.trim().tokenize(' ') |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 57 | sourceImage = imageArray[0] |
| 58 | if (sourceImage.contains('SUBS_SOURCE_IMAGE_TAG')) { |
| 59 | common.warningMsg("Replacing SUBS_SOURCE_IMAGE_TAG => ${env.SOURCE_IMAGE_TAG}") |
| 60 | sourceImage = sourceImage.replace('SUBS_SOURCE_IMAGE_TAG', env.SOURCE_IMAGE_TAG) |
azvyagintsev | d162b91 | 2018-07-26 10:52:14 +0200 | [diff] [blame] | 61 | } |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 62 | targetRegistryPath = imageArray[1] |
| 63 | targetRegistry = imageArray[1].split('/')[0] |
| 64 | imageName = getImageName(sourceImage) |
| 65 | targetImageFull = "${targetRegistryPath}/${imageName}:${env.IMAGE_TAG}" |
Denis Egorenko | 2ee651c | 2018-11-23 17:41:38 +0400 | [diff] [blame] | 66 | |
| 67 | def mcp_artifactory = new com.mirantis.mcp.MCPArtifactory() |
| 68 | if (targetImageFull.contains(externalMarker)) { |
| 69 | external = true |
| 70 | // check if images exists - raise error, as we don't want to rewrite existing one |
| 71 | def imageRepo = targetRegistryPath - targetRegistry |
Denis Egorenko | 1b207d1 | 2018-11-26 15:35:21 +0400 | [diff] [blame] | 72 | if (mcp_artifactory.imageExists(env.REGISTRY_URL, "${imageRepo}/${imageName}", env.IMAGE_TAG)) { |
Denis Egorenko | 2ee651c | 2018-11-23 17:41:38 +0400 | [diff] [blame] | 73 | error("Image ${targetImageFull} already exists!") |
| 74 | } |
| 75 | } |
| 76 | |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 77 | srcImage = docker.image(sourceImage) |
azvyagintsev | 86ba4a0 | 2018-10-03 19:21:28 +0300 | [diff] [blame] | 78 | common.retry(3, 5) { |
| 79 | srcImage.pull() |
| 80 | } |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 81 | // Use sh-docker call for tag, due magic code in plugin: |
| 82 | // https://github.com/jenkinsci/docker-workflow-plugin/blob/docker-workflow-1.17/src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy#L168-L170 |
| 83 | sh("docker tag ${srcImage.id} ${targetImageFull}") |
| 84 | common.infoMsg("Attempt to push docker image into remote registry: ${env.REGISTRY_URL}") |
azvyagintsev | 86ba4a0 | 2018-10-03 19:21:28 +0300 | [diff] [blame] | 85 | common.retry(3, 5) { |
| 86 | docker.withRegistry(env.REGISTRY_URL, env.TARGET_REGISTRY_CREDENTIALS_ID) { |
| 87 | sh("docker push ${targetImageFull}") |
| 88 | } |
Alexander Evseev | d70ef00 | 2018-10-02 13:01:27 +0200 | [diff] [blame] | 89 | } |
Denis Egorenko | a0f1b2f | 2018-11-23 17:24:35 +0000 | [diff] [blame] | 90 | def buildTime = new Date().format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC')) |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 91 | |
| 92 | if (setDefaultArtifactoryProperties) { |
| 93 | common.infoMsg("Processing artifactory props for : ${targetImageFull}") |
| 94 | LinkedHashMap artifactoryProperties = [:] |
| 95 | // Get digest of pushed image |
| 96 | String unique_image_id = sh( |
| 97 | script: "docker inspect --format='{{index .RepoDigests 0}}' '${targetImageFull}'", |
| 98 | returnStdout: true, |
| 99 | ).trim() |
| 100 | def image_sha256 = unique_image_id.tokenize(':')[1] |
| 101 | def ret = new URL("https://${targetRegistry}/artifactory/api/search/checksum?sha256=${image_sha256}").getText() |
| 102 | // Most probably, we would get many images, especially for external images. We need to guess |
| 103 | // exactly one, which we pushing now |
| 104 | guessImage = targetImageFull.replace(':', '/').replace(targetRegistry, '') |
| 105 | ArrayList img_data = new JsonSlurper().parseText(ret)['results'] |
Denis Egorenko | a0f1b2f | 2018-11-23 17:24:35 +0000 | [diff] [blame] | 106 | def imgUrl = img_data*.uri.find { it.contains(guessImage) } - '/manifest.json' |
| 107 | artifactoryProperties = [ |
| 108 | 'com.mirantis.targetTag' : env.IMAGE_TAG, |
| 109 | 'com.mirantis.uniqueImageId': unique_image_id, |
| 110 | ] |
| 111 | if (external) { |
| 112 | artifactoryProperties << ['com.mirantis.externalImage': external] |
| 113 | } |
Denis Egorenko | a0f1b2f | 2018-11-23 17:24:35 +0000 | [diff] [blame] | 114 | def existingProps = mcp_artifactory.getPropertiesForArtifact(imgUrl) |
| 115 | def historyProperties = [] |
| 116 | // check does image have already some props |
| 117 | if (existingProps) { |
| 118 | historyProperties = existingProps.get('com.mirantis.versionHistory', []) |
| 119 | } |
| 120 | // %5C - backslash symbol is needed |
| 121 | historyProperties.add("${buildTime}%5C=${sourceImage}") |
| 122 | artifactoryProperties << [ 'com.mirantis.versionHistory': historyProperties.join(',') ] |
| 123 | common.infoMsg("artifactoryProperties=> ${artifactoryProperties}") |
| 124 | common.retry(3, 5) { |
| 125 | mcp_artifactory.setProperties(imgUrl, artifactoryProperties) |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 126 | } |
| 127 | } |
Richard Felkl | 7c920d0 | 2017-12-11 15:28:18 +0100 | [diff] [blame] | 128 | } |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 129 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 130 | } catch (Throwable e) { |
azvyagintsev | 388a1bd | 2018-09-06 12:39:49 +0300 | [diff] [blame] | 131 | // Stub for future processing |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 132 | currentBuild.result = "FAILURE" |
| 133 | throw e |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 134 | } |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 135 | } |
azvyagintsev | d162b91 | 2018-07-26 10:52:14 +0200 | [diff] [blame] | 136 | } |