blob: 8f0373c96c07248b84c638c1c03bcd9785fbcd14 [file] [log] [blame]
Richard Felkle90ef1c2017-12-08 00:13:56 +01001/**
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 */
13import java.util.regex.Pattern;
14
15def common = new com.mirantis.mk.Common()
16
17@NonCPS
18def 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 Felkl7c920d02017-12-11 15:28:18 +010025 throw new IllegalArgumentException("Wrong format of image name.")
Richard Felkle90ef1c2017-12-08 00:13:56 +010026 }
27}
28
29node("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')
Richard Felkl7c920d02017-12-11 15:28:18 +010035 def imageName, imagePath, targetRegistry, imageArray
Richard Felkle90ef1c2017-12-08 00:13:56 +010036 for (image in images){
Richard Felkl7c920d02017-12-11 15:28:18 +010037 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]
Richard Felkl8e674892017-12-12 18:23:31 +010043 imageName = getImageName(imagePath)
Richard Felkl7c920d02017-12-11 15:28:18 +010044 sh """docker pull ${imagePath}
45 docker tag ${imagePath} ${targetRegistry}/${imageName}:${IMAGE_TAG}
46 docker push ${targetRegistry}/${imageName}:${IMAGE_TAG}"""
Richard Felkle90ef1c2017-12-08 00:13:56 +010047 }
48 }
49 } catch (Throwable e) {
50 // If there was an error or exception thrown, the build failed
51 currentBuild.result = "FAILURE"
52 throw e
53 }
54}