Add pipeline promoting docker images
Change-Id: I3df87e1f1aa08fb7f23020215d404db55e45713a
See: https://mirantis.jira.com/browse/PROD-16261
diff --git a/artifactory-promote-docker-image.groovy b/artifactory-promote-docker-image.groovy
new file mode 100644
index 0000000..e278f05
--- /dev/null
+++ b/artifactory-promote-docker-image.groovy
@@ -0,0 +1,67 @@
+#!groovy
+
+/**
+ *
+ * Promote docker image from one artifactory repository (development) to
+ * another (production)
+ *
+ * Expected parameters:
+ * REPO_SRC Source Artifactory repository (default 'docker-dev-local')
+ * REPO_DST Destination Artifactory repository (default 'docker-prod-local')
+ * IMAGE_SRC Source image name (without docker registry!) to promote (required)
+ * IMAGE_DST Destination image (default same as IMAGE_SRC)
+ *
+ * COPY_IMAGE Copy image instead of moving (default 'true')
+ *
+ * ARTIFACTORY_URL Base URL of Artifactory instance, i.e. without `/api/...` path.
+ * (default 'https://artifactory.mcp.mirantis.net/artifactory/')
+ * ARTIFACTORY_CREDS Credentials to login into Artifactory (default 'artifactory')
+ *
+ * SLAVE_LABEL Label of the slave to run job (default 'master')
+ *
+ * Slave requirements: curl installed
+ *
+ */
+
+import groovy.json.JsonOutput
+
+String repo_src = env.REPO_SRC ?: 'docker-dev-local'
+String repo_dst = env.REPO_DST ?: 'docker-prod-local'
+String image_src = env.IMAGE_SRC
+String image_dst = env.IMAGE_DST ?: env.IMAGE_SRC
+
+boolean copy_image = env.COPY_IMAGE.asBoolean() ?: true
+
+String artifactory_url = env.ARTIFACTORY_URL ?: 'https://artifactory.mcp.mirantis.net/artifactory/'
+String artifactory_creds = env.ARTIFACTORY_CREDS ?: 'artifactory'
+
+String slave_label = env.SLAVE_LABEL ?: 'master'
+
+// Delimiter for splitting docker image name and tag (to avoid codeNarc DRY warning)
+String _colon = ':'
+
+String img_src_name, img_src_tag
+String img_dst_name, img_dst_tag
+
+node(slave_label) {
+ (img_src_name, img_src_tag) = image_src.tokenize(_colon)
+ (img_dst_name, img_dst_tag) = image_dst.tokenize(_colon)
+
+ String api_req = JsonOutput.toJson([
+ targetRepo: repo_dst,
+ dockerRepository: img_src_name,
+ targetDockerRepository: img_dst_name,
+ tag: img_src_tag,
+ targetTag: img_dst_tag,
+ copy: copy_image,
+ ])
+
+ withCredentials([usernameColonPassword(credentialsId: artifactory_creds, variable: 'USERPASS')]) {
+ sh """
+ curl -fLsS \
+ -u \$USERPASS \
+ -X POST -d '${api_req}' -H 'Content-Type: application/json' \
+ '${artifactory_url}api/docker/${repo_src}/v2/promote'
+ """
+ }
+}