blob: 2ec0ab689bf7df0cef4efd8a44e26700bf779103 [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()
Jakub Josefa63f9862018-01-11 17:58:38 +010018timeout(time: 12, unit: 'HOURS') {
19 node("docker") {
20 def workspace = common.getWorkspace()
21 def imageTagsList = IMAGE_TAGS.tokenize(" ")
22 try{
Filip Pytloun8a3530b2017-06-30 17:28:37 +020023
Jakub Josefa63f9862018-01-11 17:58:38 +010024 def buildArgs = []
25 try {
26 buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ')
27 } catch (Throwable e) {
28 buildArgs = []
29 }
30 def dockerApp
31 docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) {
32 stage("checkout") {
33 git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID)
34 }
35
36 if (IMAGE_BRANCH == "master") {
37 try {
38 def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim()
39 def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim()
40 imageTagsList << tag
41 revision = revision ? revision : "0"
42 if(Integer.valueOf(revision) > 0){
43 imageTagsList << "${tag}-${revision}"
44 }
45 if (!imageTagsList.contains("latest")) {
46 imageTagsList << "latest"
47 }
48 } catch (Exception e) {
49 common.infoMsg("Impossible to find any tag")
50 }
51 }
52
53 stage("build") {
54 common.infoMsg("Building docker image ${IMAGE_NAME}")
55 dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs)
56 if(!dockerApp){
57 throw new Exception("Docker build image failed")
58 }
59 }
60 stage("upload to docker hub"){
61 for(int i=0;i<imageTagsList.size();i++){
62 common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]}")
63 dockerApp.push(imageTagsList[i])
64 }
65 }
66 }
Filip Pytloun8a3530b2017-06-30 17:28:37 +020067 } catch (Throwable e) {
Jakub Josefa63f9862018-01-11 17:58:38 +010068 // If there was an error or exception thrown, the build failed
69 currentBuild.result = "FAILURE"
70 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
71 throw e
72 } finally {
73 common.sendNotification(currentBuild.result,"",["slack"])
Filip Pytloun8a3530b2017-06-30 17:28:37 +020074 }
Jakub Josefc4064bb2017-04-03 19:25:19 +020075 }
76}