blob: ebbfc861007e47703300f9835e00c7222ec6a52f [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
azvyagintsevd162b912018-07-26 10:52:14 +020010 * SOURCE_IMAGE_TAG Tag to use when pulling images(optional,if SOURCE_IMAGE_TAG has been found)
Richard Felkle90ef1c2017-12-08 00:13:56 +010011 * IMAGE_LIST List of images to mirror
12 *
13 */
14import java.util.regex.Pattern;
15
16def common = new com.mirantis.mk.Common()
17
18@NonCPS
19def 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 Felkl7c920d02017-12-11 15:28:18 +010026 throw new IllegalArgumentException("Wrong format of image name.")
Richard Felkle90ef1c2017-12-08 00:13:56 +010027 }
28}
Jakub Josefa63f9862018-01-11 17:58:38 +010029timeout(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]
azvyagintsevd162b912018-07-26 10:52:14 +020043 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 Josefa63f9862018-01-11 17:58:38 +010047 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 Felkl7c920d02017-12-11 15:28:18 +010052 }
Richard Felkle90ef1c2017-12-08 00:13:56 +010053 }
Jakub Josefa63f9862018-01-11 17:58:38 +010054 } catch (Throwable e) {
55 // If there was an error or exception thrown, the build failed
56 currentBuild.result = "FAILURE"
57 throw e
Richard Felkle90ef1c2017-12-08 00:13:56 +010058 }
Richard Felkle90ef1c2017-12-08 00:13:56 +010059 }
azvyagintsevd162b912018-07-26 10:52:14 +020060}