blob: db448eabfd86b68f7ea37c6f098f208462fb6ea5 [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")) {
72 def artifactoryServer = Artifactory.server("mcp-ci")
73 def shortImageName = IMAGE_NAME
74 if (IMAGE_NAME.contains("/")) {
75 shortImageName = IMAGE_NAME.tokenize("/")[1]
76 }
77 for (imageTag in imageTagsList) {
78 sh "docker tag ${IMAGE_NAME} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}"
79 artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL,
80 "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}",
81 imageTag, "docker-dev-local")
82 }
83 }else{
84 common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped")
85 }
86 }
Filip Pytloun8a3530b2017-06-30 17:28:37 +020087 } catch (Throwable e) {
Jakub Josefa63f9862018-01-11 17:58:38 +010088 // If there was an error or exception thrown, the build failed
89 currentBuild.result = "FAILURE"
90 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
91 throw e
92 } finally {
93 common.sendNotification(currentBuild.result,"",["slack"])
Filip Pytloun8a3530b2017-06-30 17:28:37 +020094 }
Jakub Josefc4064bb2017-04-03 19:25:19 +020095 }
96}
Jakub Josef0ae451e2018-04-26 15:38:52 +020097