blob: b94928ea9470b3562cbd18057e6c9cc2146fa3a0 [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,...)
Jakub Josefc4064bb2017-04-03 19:25:19 +020012 * REGISTRY_CREDENTIALS_ID - Docker hub credentials id
13 *
14**/
15
16def common = new com.mirantis.mk.Common()
17def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun56070122017-06-29 15:30:15 +020018def git = new com.mirantis.mk.Git()
Jakub Josefc4064bb2017-04-03 19:25:19 +020019def dockerLib = new com.mirantis.mk.Docker()
Jakub Josef0ae451e2018-04-26 15:38:52 +020020def artifactory = new com.mirantis.mcp.MCPArtifactory()
Jakub Josefa63f9862018-01-11 17:58:38 +010021timeout(time: 12, unit: 'HOURS') {
22 node("docker") {
23 def workspace = common.getWorkspace()
24 def imageTagsList = IMAGE_TAGS.tokenize(" ")
25 try{
Filip Pytloun8a3530b2017-06-30 17:28:37 +020026
Jakub Josefa63f9862018-01-11 17:58:38 +010027 def buildArgs = []
28 try {
29 buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ')
30 } catch (Throwable e) {
31 buildArgs = []
32 }
33 def dockerApp
Jakub Josefa63f9862018-01-11 17:58:38 +010034 stage("checkout") {
35 git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID)
36 }
37
38 if (IMAGE_BRANCH == "master") {
39 try {
40 def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim()
41 def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim()
42 imageTagsList << tag
43 revision = revision ? revision : "0"
44 if(Integer.valueOf(revision) > 0){
45 imageTagsList << "${tag}-${revision}"
46 }
47 if (!imageTagsList.contains("latest")) {
48 imageTagsList << "latest"
49 }
50 } catch (Exception e) {
51 common.infoMsg("Impossible to find any tag")
52 }
53 }
54
55 stage("build") {
56 common.infoMsg("Building docker image ${IMAGE_NAME}")
57 dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
58 if(!dockerApp){
59 throw new Exception("Docker build image failed")
60 }
61 }
62 stage("upload to docker hub"){
Jakub Josef0ae451e2018-04-26 15:38:52 +020063 docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
64 for(int i=0;i<imageTagsList.size();i++){
65 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]} to dockerhub")
66 dockerApp.push(imageTagsList[i])
67 }
Jakub Josefa63f9862018-01-11 17:58:38 +010068 }
69 }
Jakub Josef0ae451e2018-04-26 15:38:52 +020070 stage("upload to artifactory"){
71 if(common.validInputParam("ARTIFACTORY_URL") && common.validInputParam("ARTIFACTORY_NAMESPACE")) {
Jakub Josefec44e532018-05-10 13:29:28 +020072 def artifactoryName = "mcp-ci";
73 def artifactoryServer = Artifactory.server(artifactoryName)
Jakub Josef0ae451e2018-04-26 15:38:52 +020074 def shortImageName = IMAGE_NAME
75 if (IMAGE_NAME.contains("/")) {
76 shortImageName = IMAGE_NAME.tokenize("/")[1]
77 }
78 for (imageTag in imageTagsList) {
79 sh "docker tag ${IMAGE_NAME} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}"
Jakub Josefec44e532018-05-10 13:29:28 +020080 for(artifactoryRepo in ["docker-dev-local", "docker-prod-local"]){
81 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTag} to artifactory ${artifactoryName} using repo ${artifactoryRepo}")
82 artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL,
83 "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}",
84 imageTag, artifactoryRepo)
85 }
Jakub Josef0ae451e2018-04-26 15:38:52 +020086 }
87 }else{
88 common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped")
89 }
90 }
Filip Pytloun8a3530b2017-06-30 17:28:37 +020091 } catch (Throwable e) {
Jakub Josefa63f9862018-01-11 17:58:38 +010092 // If there was an error or exception thrown, the build failed
93 currentBuild.result = "FAILURE"
94 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
95 throw e
96 } finally {
97 common.sendNotification(currentBuild.result,"",["slack"])
Filip Pytloun8a3530b2017-06-30 17:28:37 +020098 }
Jakub Josefc4064bb2017-04-03 19:25:19 +020099 }
100}
Jakub Josef0ae451e2018-04-26 15:38:52 +0200101