Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
| 14 | def common = new com.mirantis.mk.Common() |
| 15 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 5607012 | 2017-06-29 15:30:15 +0200 | [diff] [blame] | 16 | def git = new com.mirantis.mk.Git() |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 17 | def dockerLib = new com.mirantis.mk.Docker() |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 18 | timeout(time: 12, unit: 'HOURS') { |
| 19 | node("docker") { |
| 20 | def workspace = common.getWorkspace() |
| 21 | def imageTagsList = IMAGE_TAGS.tokenize(" ") |
| 22 | try{ |
Filip Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 23 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 24 | 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 Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 67 | } catch (Throwable e) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 68 | // 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 Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 74 | } |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 75 | } |
| 76 | } |