blob: a39051ff05d9c8072422a2ea42441bd6c21be8d5 [file] [log] [blame]
Jakub Josefc4064bb2017-04-03 19:25:19 +02001/**
2 * Docker image build pipeline
3 * IMAGE_NAME - Image name
4 * IMAGE_GIT_URL - Image git repo URL
5 * IMAGE_BRANCH - Image repo branch
6 * IMAGE_CREDENTIALS_ID - Image repo credentials id
7 * IMAGE_TAGS - Image tags
8 * DOCKERFILE_PATH - Relative path to docker file in image repo
9 * REGISTRY_URL - Docker registry URL (can be empty)
Jakub Josef0ae451e2018-04-26 15:38:52 +020010 * ARTIFACTORY_URL - URL to artifactory
11 * ARTIFACTORY_NAMESPACE - Artifactory namespace (oss, cicd,...)
azvyagintsevcd029662018-09-19 16:32:09 +030012 * UPLOAD_TO_DOCKER_HUB - True\False
Jakub Josefc4064bb2017-04-03 19:25:19 +020013 * REGISTRY_CREDENTIALS_ID - Docker hub credentials id
14 *
azvyagintsevcd029662018-09-19 16:32:09 +030015 **/
Jakub Josefc4064bb2017-04-03 19:25:19 +020016
17def common = new com.mirantis.mk.Common()
18def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun56070122017-06-29 15:30:15 +020019def git = new com.mirantis.mk.Git()
Jakub Josefc4064bb2017-04-03 19:25:19 +020020def dockerLib = new com.mirantis.mk.Docker()
Jakub Josef0ae451e2018-04-26 15:38:52 +020021def artifactory = new com.mirantis.mcp.MCPArtifactory()
azvyagintsevcd029662018-09-19 16:32:09 +030022
23slaveNode = env.SLAVE_NODE ?: 'docker'
24uploadToDockerHub = env.UPLOAD_TO_DOCKER_HUB ?: false
25
Jakub Josefa63f9862018-01-11 17:58:38 +010026timeout(time: 12, unit: 'HOURS') {
azvyagintsevcd029662018-09-19 16:32:09 +030027 node(slaveNode) {
28 def workspace = common.getWorkspace()
29 def imageTagsList = env.IMAGE_TAGS.tokenize(" ")
30 try {
Filip Pytloun8a3530b2017-06-30 17:28:37 +020031
azvyagintsevcd029662018-09-19 16:32:09 +030032 def buildArgs = []
33 try {
34 buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ')
35 } catch (Throwable e) {
36 buildArgs = []
37 }
38 def dockerApp
39 stage("checkout") {
40 git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID)
41 }
Jakub Josefa63f9862018-01-11 17:58:38 +010042
azvyagintsevcd029662018-09-19 16:32:09 +030043 if (IMAGE_BRANCH == "master") {
44 try {
45 def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim()
46 def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim()
47 imageTagsList << tag
48 revision = revision ? revision : "0"
49 if (Integer.valueOf(revision) > 0) {
50 imageTagsList << "${tag}-${revision}"
51 }
52 if (!imageTagsList.contains("latest")) {
53 imageTagsList << "latest"
54 }
55 } catch (Exception e) {
56 common.infoMsg("Impossible to find any tag")
57 }
Jakub Josefa63f9862018-01-11 17:58:38 +010058 }
Jakub Josefa63f9862018-01-11 17:58:38 +010059
azvyagintsevcd029662018-09-19 16:32:09 +030060 stage("build") {
61 common.infoMsg("Building docker image ${IMAGE_NAME}")
62 dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
63 if (!dockerApp) {
64 throw new Exception("Docker build image failed")
65 }
Jakub Josef0ae451e2018-04-26 15:38:52 +020066 }
azvyagintsevcd029662018-09-19 16:32:09 +030067 stage("upload to docker hub") {
68 if (uploadToDockerHub) {
69 docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
70 for (int i = 0; i < imageTagsList.size(); i++) {
71 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]} to dockerhub")
72 dockerApp.push(imageTagsList[i])
73 }
74 }
75 } else {
76 common.infoMsg('upload to docker hub skipped')
77 }
78 }
79 stage("upload to artifactory") {
80 if (common.validInputParam("ARTIFACTORY_URL") && common.validInputParam("ARTIFACTORY_NAMESPACE")) {
81 def artifactoryName = "mcp-ci";
82 def artifactoryServer = Artifactory.server(artifactoryName)
83 def shortImageName = IMAGE_NAME
84 if (IMAGE_NAME.contains("/")) {
85 shortImageName = IMAGE_NAME.tokenize("/")[1]
86 }
87 for (imageTag in imageTagsList) {
88 sh "docker tag ${IMAGE_NAME}:${imageTagsList[0]} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}"
89 for (artifactoryRepo in ["docker-dev-local", "docker-prod-local"]) {
90 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTag} to artifactory ${artifactoryName} using repo ${artifactoryRepo}")
91 artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL,
92 "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}",
93 imageTag, artifactoryRepo)
94 }
95 }
96 } else {
97 common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped")
98 }
99 }
100 } catch (Throwable e) {
101 // If there was an error or exception thrown, the build failed
102 currentBuild.result = "FAILURE"
103 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
104 throw e
105 } finally {
106 common.sendNotification(currentBuild.result, "", ["slack"])
Jakub Josefa63f9862018-01-11 17:58:38 +0100107 }
Filip Pytloun8a3530b2017-06-30 17:28:37 +0200108 }
Jakub Josefc4064bb2017-04-03 19:25:19 +0200109}