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) |
Jakub Josef | 0ae451e | 2018-04-26 15:38:52 +0200 | [diff] [blame] | 10 | * ARTIFACTORY_URL - URL to artifactory |
| 11 | * ARTIFACTORY_NAMESPACE - Artifactory namespace (oss, cicd,...) |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 12 | * UPLOAD_TO_DOCKER_HUB - True\False |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 13 | * REGISTRY_CREDENTIALS_ID - Docker hub credentials id |
| 14 | * |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 15 | **/ |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 16 | |
| 17 | def common = new com.mirantis.mk.Common() |
| 18 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 5607012 | 2017-06-29 15:30:15 +0200 | [diff] [blame] | 19 | def git = new com.mirantis.mk.Git() |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 20 | def dockerLib = new com.mirantis.mk.Docker() |
Jakub Josef | 0ae451e | 2018-04-26 15:38:52 +0200 | [diff] [blame] | 21 | def artifactory = new com.mirantis.mcp.MCPArtifactory() |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 22 | |
| 23 | slaveNode = env.SLAVE_NODE ?: 'docker' |
| 24 | uploadToDockerHub = env.UPLOAD_TO_DOCKER_HUB ?: false |
| 25 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 26 | timeout(time: 12, unit: 'HOURS') { |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 27 | node(slaveNode) { |
| 28 | def workspace = common.getWorkspace() |
| 29 | def imageTagsList = env.IMAGE_TAGS.tokenize(" ") |
| 30 | try { |
Filip Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 31 | |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 32 | def buildArgs = [] |
| 33 | try { |
| 34 | buildArgs = IMAGE_BUILD_PARAMS.tokenize(' ') |
| 35 | } catch (Throwable e) { |
| 36 | buildArgs = [] |
| 37 | } |
| 38 | def dockerApp |
| 39 | stage("checkout") { |
| 40 | git.checkoutGitRepository('.', IMAGE_GIT_URL, IMAGE_BRANCH, IMAGE_CREDENTIALS_ID) |
| 41 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 42 | |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 43 | if (IMAGE_BRANCH == "master") { |
| 44 | try { |
| 45 | def tag = sh(script: "git describe --tags --abbrev=0", returnStdout: true).trim() |
| 46 | def revision = sh(script: "git describe --tags --abbrev=4 | grep -oP \"^${tag}-\\K.*\" | awk -F\\- '{print \$1}'", returnStdout: true).trim() |
| 47 | imageTagsList << tag |
| 48 | revision = revision ? revision : "0" |
| 49 | if (Integer.valueOf(revision) > 0) { |
| 50 | imageTagsList << "${tag}-${revision}" |
| 51 | } |
| 52 | if (!imageTagsList.contains("latest")) { |
| 53 | imageTagsList << "latest" |
| 54 | } |
| 55 | } catch (Exception e) { |
| 56 | common.infoMsg("Impossible to find any tag") |
| 57 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 58 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 59 | |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 60 | stage("build") { |
| 61 | common.infoMsg("Building docker image ${IMAGE_NAME}") |
| 62 | dockerApp = dockerLib.buildDockerImage(IMAGE_NAME, "", "${workspace}/${DOCKERFILE_PATH}", imageTagsList[0], buildArgs) |
| 63 | if (!dockerApp) { |
| 64 | throw new Exception("Docker build image failed") |
| 65 | } |
Jakub Josef | 0ae451e | 2018-04-26 15:38:52 +0200 | [diff] [blame] | 66 | } |
azvyagintsev | cd02966 | 2018-09-19 16:32:09 +0300 | [diff] [blame] | 67 | stage("upload to docker hub") { |
| 68 | if (uploadToDockerHub) { |
| 69 | docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID) { |
| 70 | for (int i = 0; i < imageTagsList.size(); i++) { |
| 71 | common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTagsList[i]} to dockerhub") |
| 72 | dockerApp.push(imageTagsList[i]) |
| 73 | } |
| 74 | } |
| 75 | } else { |
| 76 | common.infoMsg('upload to docker hub skipped') |
| 77 | } |
| 78 | } |
| 79 | stage("upload to artifactory") { |
| 80 | if (common.validInputParam("ARTIFACTORY_URL") && common.validInputParam("ARTIFACTORY_NAMESPACE")) { |
| 81 | def artifactoryName = "mcp-ci"; |
| 82 | def artifactoryServer = Artifactory.server(artifactoryName) |
| 83 | def shortImageName = IMAGE_NAME |
| 84 | if (IMAGE_NAME.contains("/")) { |
| 85 | shortImageName = IMAGE_NAME.tokenize("/")[1] |
| 86 | } |
| 87 | for (imageTag in imageTagsList) { |
| 88 | sh "docker tag ${IMAGE_NAME}:${imageTagsList[0]} ${ARTIFACTORY_URL}/mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}:${imageTag}" |
| 89 | for (artifactoryRepo in ["docker-dev-local", "docker-prod-local"]) { |
| 90 | common.infoMsg("Uploading image ${IMAGE_NAME} with tag ${imageTag} to artifactory ${artifactoryName} using repo ${artifactoryRepo}") |
| 91 | artifactory.uploadImageToArtifactory(artifactoryServer, ARTIFACTORY_URL, |
| 92 | "mirantis/${ARTIFACTORY_NAMESPACE}/${shortImageName}", |
| 93 | imageTag, artifactoryRepo) |
| 94 | } |
| 95 | } |
| 96 | } else { |
| 97 | common.warningMsg("ARTIFACTORY_URL not given, upload to artifactory skipped") |
| 98 | } |
| 99 | } |
| 100 | } catch (Throwable e) { |
| 101 | // If there was an error or exception thrown, the build failed |
| 102 | currentBuild.result = "FAILURE" |
| 103 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 104 | throw e |
| 105 | } finally { |
| 106 | common.sendNotification(currentBuild.result, "", ["slack"]) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 107 | } |
Filip Pytloun | 8a3530b | 2017-06-30 17:28:37 +0200 | [diff] [blame] | 108 | } |
Jakub Josef | c4064bb | 2017-04-03 19:25:19 +0200 | [diff] [blame] | 109 | } |