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() |
| 18 | node("docker") { |
| 19 | def workspace = common.getWorkspace() |
| 20 | def imageTagsList = IMAGE_TAGS.tokenize(" ") |
| 21 | try{ |
Filip Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 22 | |
| 23 | def buildArgs = [] |
| 24 | try { |
Filip Pytloun | cc3f8cf | 2017-06-30 17:52:33 +0200 | [diff] [blame] | 25 | buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ') |
Filip Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 26 | } catch (Throwable e) { |
| 27 | buildArgs = [] |
| 28 | } |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 29 | def dockerApp |
| 30 | docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) { |
| 31 | stage("checkout") { |
Filip Pytloun | 5607012 | 2017-06-29 15:30:15 +0200 | [diff] [blame] | 32 | git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID) |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 33 | } |
| 34 | stage("build") { |
Jakub Josef | 37942b8 | 2017-04-06 17:34:25 +0200 | [diff] [blame] | 35 | common.infoMsg("Building docker image ${IMAGE_NAME}") |
Filip Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 36 | dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs) |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 37 | if(!dockerApp){ |
| 38 | throw new Exception("Docker build image failed") |
| 39 | } |
| 40 | } |
| 41 | stage("upload to docker hub"){ |
| 42 | for(int i=0;i<imageTagsList.size();i++){ |
Jakub Josef | 37942b8 | 2017-04-06 17:34:25 +0200 | [diff] [blame] | 43 | common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]}") |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 44 | dockerApp.push(imageTagsList[i]) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } catch (Throwable e) { |
| 49 | // If there was an error or exception thrown, the build failed |
| 50 | currentBuild.result = "FAILURE" |
| 51 | throw e |
| 52 | } finally { |
| 53 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 54 | } |
| 55 | } |