Introduced Docker image mirror pipeline
Change-Id: Ie25d83efc74c20e381c0576531437c36d1e0bf18
diff --git a/docker-mirror-images.groovy b/docker-mirror-images.groovy
new file mode 100644
index 0000000..91a65a6
--- /dev/null
+++ b/docker-mirror-images.groovy
@@ -0,0 +1,49 @@
+/**
+ *
+ * Mirror Docker images
+ *
+ * Expected parameters:
+ * TARGET_REGISTRY_CREDENTIALS_ID Credentials for target Docker Registry
+ * TARGET_REGISTRY Target Docker Registry name
+ * REGISTRY_URL Target Docker Registry URL
+ * IMAGE_TAG Tag to use when pushing images
+ * IMAGE_LIST List of images to mirror
+ *
+ */
+import java.util.regex.Pattern;
+
+def common = new com.mirantis.mk.Common()
+
+@NonCPS
+def getImageName(String image) {
+ def regex = Pattern.compile('(?:.+/)?([^:]+)(?::.+)?')
+ def matcher = regex.matcher(image)
+ if(matcher.find()){
+ def imageName = matcher.group(1)
+ return imageName
+ }else{
+ throw new IllegalFormatException("Wrong format of image name.")
+ }
+}
+
+node("docker") {
+ try {
+ stage("Mirror Docker Images"){
+ def creds = common.getPasswordCredentials(TARGET_REGISTRY_CREDENTIALS_ID)
+ sh "docker login --username=${creds.username} --password=${creds.password.toString()} ${REGISTRY_URL}"
+ def images = IMAGE_LIST.tokenize('\n')
+ def imageName
+ for (image in images){
+ sh "echo ${image}"
+ imageName = getImageName(image)
+ sh "docker pull ${image}"
+ sh "docker tag ${image} ${TARGET_REGISTRY}/${imageName}:${IMAGE_TAG}"
+ sh "docker push ${TARGET_REGISTRY}/${imageName}:${IMAGE_TAG}"
+ }
+ }
+ } catch (Throwable e) {
+ // If there was an error or exception thrown, the build failed
+ currentBuild.result = "FAILURE"
+ throw e
+ }
+}
\ No newline at end of file