blob: 926fd886b4777e1f4bd3710c577caea2fc17e2c6 [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)
10 * REGISTRY_CREDENTIALS_ID - Docker hub credentials id
11 *
12**/
13
14def common = new com.mirantis.mk.Common()
15def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun56070122017-06-29 15:30:15 +020016def git = new com.mirantis.mk.Git()
Jakub Josefc4064bb2017-04-03 19:25:19 +020017def dockerLib = new com.mirantis.mk.Docker()
18node("docker") {
19 def workspace = common.getWorkspace()
20 def imageTagsList = IMAGE_TAGS.tokenize(" ")
21 try{
Filip Pytloun8a3530b2017-06-30 17:28:37 +020022
23 def buildArgs = []
24 try {
Filip Pytlouncc3f8cf2017-06-30 17:52:33 +020025 buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ')
Filip Pytloun8a3530b2017-06-30 17:28:37 +020026 } catch (Throwable e) {
27 buildArgs = []
28 }
Jakub Josefc4064bb2017-04-03 19:25:19 +020029 def dockerApp
30 docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
31 stage("checkout") {
Filip Pytloun56070122017-06-29 15:30:15 +020032 git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID)
Jakub Josefc4064bb2017-04-03 19:25:19 +020033 }
chnyda0474c4b2017-10-23 15:46:48 +020034
35 if (IMAGE_BRANCH == "master") {
36 try {
37 def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim()
38 def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim()
39 imageTagsList << tag
chnydaf8c07622017-10-31 11:19:07 +010040 revision = revision ? revision : "0"
41 imageTagsList << "${tag}-${revision}"
42
chnyda0474c4b2017-10-23 15:46:48 +020043 if (!imageTagsList.contains("latest")) {
44 imageTagsList << "latest"
45 }
46 } catch (Exception e) {
47 common.infoMsg("Impossible to find any tag")
48 }
49 }
50
Jakub Josefc4064bb2017-04-03 19:25:19 +020051 stage("build") {
Jakub Josef37942b82017-04-06 17:34:25 +020052 common.infoMsg("Building docker image ${IMAGE_NAME}")
Filip Pytloun8a3530b2017-06-30 17:28:37 +020053 dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
Jakub Josefc4064bb2017-04-03 19:25:19 +020054 if(!dockerApp){
55 throw new Exception("Docker build image failed")
56 }
57 }
58 stage("upload to docker hub"){
59 for(int i=0;i<imageTagsList.size();i++){
Jakub Josef37942b82017-04-06 17:34:25 +020060 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]}")
Jakub Josefc4064bb2017-04-03 19:25:19 +020061 dockerApp.push(imageTagsList[i])
62 }
63 }
64 }
65 } catch (Throwable e) {
66 // If there was an error or exception thrown, the build failed
67 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +020068 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Jakub Josefc4064bb2017-04-03 19:25:19 +020069 throw e
70 } finally {
71 common.sendNotification(currentBuild.result,"",["slack"])
72 }
73}