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) |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 11 | * IMAGE_LIST List of images to mirror |
| 12 | * |
| 13 | */ |
| 14 | import java.util.regex.Pattern; |
| 15 | |
| 16 | def common = new com.mirantis.mk.Common() |
| 17 | |
| 18 | @NonCPS |
| 19 | def getImageName(String image) { |
| 20 | def regex = Pattern.compile('(?:.+/)?([^:]+)(?::.+)?') |
| 21 | def matcher = regex.matcher(image) |
| 22 | if(matcher.find()){ |
| 23 | def imageName = matcher.group(1) |
| 24 | return imageName |
| 25 | }else{ |
Richard Felkl | 7c920d0 | 2017-12-11 15:28:18 +0100 | [diff] [blame] | 26 | throw new IllegalArgumentException("Wrong format of image name.") |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 27 | } |
| 28 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 29 | timeout(time: 12, unit: 'HOURS') { |
| 30 | node("docker") { |
| 31 | try { |
| 32 | stage("Mirror Docker Images"){ |
| 33 | def creds = common.getPasswordCredentials(TARGET_REGISTRY_CREDENTIALS_ID) |
| 34 | sh "docker login --username=${creds.username} --password=${creds.password.toString()} ${REGISTRY_URL}" |
| 35 | def images = IMAGE_LIST.tokenize('\n') |
| 36 | def imageName, imagePath, targetRegistry, imageArray |
| 37 | for (image in images){ |
| 38 | if(image.trim().indexOf(' ') == -1){ |
| 39 | throw new IllegalArgumentException("Wrong format of image and target repository input") |
| 40 | } |
| 41 | imageArray = image.trim().tokenize(' ') |
| 42 | imagePath = imageArray[0] |
azvyagintsev | d162b91 | 2018-07-26 10:52:14 +0200 | [diff] [blame] | 43 | if (imagePath.contains('SUBS_SOURCE_IMAGE_TAG')) { |
| 44 | common.warningMsg("Replacing SUBS_SOURCE_IMAGE_TAG => ${SOURCE_IMAGE_TAG}") |
| 45 | imagePath.replace('SUBS_SOURCE_IMAGE_TAG', SOURCE_IMAGE_TAG) |
| 46 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 47 | targetRegistry = imageArray[1] |
| 48 | imageName = getImageName(imagePath) |
| 49 | sh """docker pull ${imagePath} |
| 50 | docker tag ${imagePath} ${targetRegistry}/${imageName}:${IMAGE_TAG} |
| 51 | docker push ${targetRegistry}/${imageName}:${IMAGE_TAG}""" |
Richard Felkl | 7c920d0 | 2017-12-11 15:28:18 +0100 | [diff] [blame] | 52 | } |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 53 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 54 | } catch (Throwable e) { |
| 55 | // If there was an error or exception thrown, the build failed |
| 56 | currentBuild.result = "FAILURE" |
| 57 | throw e |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 58 | } |
Richard Felkl | e90ef1c | 2017-12-08 00:13:56 +0100 | [diff] [blame] | 59 | } |
azvyagintsev | d162b91 | 2018-07-26 10:52:14 +0200 | [diff] [blame] | 60 | } |