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