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