blob: 1fbd9f0e86096830cb61f575e7841b7100e94567 [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"
Roman Vyalovfa63df72018-09-05 13:27:11 +030049 //workaround for all of our docker images
50 imageTagsList << "nightly"
Jakub Josefa63f9862018-01-11 17:58:38 +010051 }
52 } catch (Exception e) {
53 common.infoMsg("Impossible to find any tag")
54 }
55 }
56
57 stage("build") {
58 common.infoMsg("Building docker image ${IMAGE_NAME}")
59 dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
60 if(!dockerApp){
61 throw new Exception("Docker build image failed")
62 }
63 }
64 stage("upload to docker hub"){
Jakub Josef0ae451e2018-04-26 15:38:52 +020065 docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
66 for(int i=0;i<imageTagsList.size();i++){
67 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]} to dockerhub")
68 dockerApp.push(imageTagsList[i])
69 }
Jakub Josefa63f9862018-01-11 17:58:38 +010070 }
71 }
Jakub Josef0ae451e2018-04-26 15:38:52 +020072 stage("upload to artifactory"){
73 if(common.validInputParam("ARTIFACTORY_URL") && common.validInputParam("ARTIFACTORY_NAMESPACE")) {
Jakub Josefec44e532018-05-10 13:29:28 +020074 def artifactoryName = "mcp-ci";
75 def artifactoryServer = Artifactory.server(artifactoryName)
Jakub Josef0ae451e2018-04-26 15:38:52 +020076 def shortImageName = IMAGE_NAME
77 if (IMAGE_NAME.contains("/")) {
78 shortImageName = IMAGE_NAME.tokenize("/")[1]
79 }
80 for (imageTag in imageTagsList) {
81 sh "docker tag ${IMAGE_NAME} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}"
Jakub Josefec44e532018-05-10 13:29:28 +020082 for(artifactoryRepo in ["docker-dev-local", "docker-prod-local"]){
83 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTag} to artifactory ${artifactoryName} using repo ${artifactoryRepo}")
84 artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL,
85 "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}",
86 imageTag, artifactoryRepo)
87 }
Jakub Josef0ae451e2018-04-26 15:38:52 +020088 }
89 }else{
90 common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped")
91 }
92 }
Filip Pytloun8a3530b2017-06-30 17:28:37 +020093 } catch (Throwable e) {
Jakub Josefa63f9862018-01-11 17:58:38 +010094 // If there was an error or exception thrown, the build failed
95 currentBuild.result = "FAILURE"
96 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
97 throw e
98 } finally {
99 common.sendNotification(currentBuild.result,"",["slack"])
Filip Pytloun8a3530b2017-06-30 17:28:37 +0200100 }
Jakub Josefc4064bb2017-04-03 19:25:19 +0200101 }
102}