blob: 08ac439e2ed7ccfa70fd19ae18a160bd94ed36ab [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}
Jakub Josefa63f9862018-01-11 17:58:38 +010028timeout(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 Felkl7c920d02017-12-11 15:28:18 +010047 }
Richard Felkle90ef1c2017-12-08 00:13:56 +010048 }
Jakub Josefa63f9862018-01-11 17:58:38 +010049 } catch (Throwable e) {
50 // If there was an error or exception thrown, the build failed
51 currentBuild.result = "FAILURE"
52 throw e
Richard Felkle90ef1c2017-12-08 00:13:56 +010053 }
Richard Felkle90ef1c2017-12-08 00:13:56 +010054 }
55}