blob: b16dc062fe7fd90bf81fe400543650e8b7e6415f [file] [log] [blame]
Mikhail Ivanovff29afa2017-05-02 16:54:14 +04001/**
2 * Docker image build pipeline with push to JFrog
3 * IMAGE_NAME - Image name
4 * IMAGE_TAGS - Tag list for image, separated by space
Mikhail Ivanovd959b312017-06-20 13:58:20 +04005 * CONTEXT_PATH - Path to build context directory
Mikhail Ivanovff29afa2017-05-02 16:54:14 +04006 * CREDENTIALS_ID - gerrit credentials id
7 * DOCKERFILE_PATH - path to dockerfile in repository
8 * DOCKER_REGISTRY - url to registry
9 * PROJECT_NAMESPACE - in which namespace will be stored
10**/
11def artifactory = new com.mirantis.mcp.MCPArtifactory()
12def common = new com.mirantis.mk.Common()
13def gerrit = new com.mirantis.mk.Gerrit()
14
15
16node("docker") {
17 def artifactoryServer = Artifactory.server("mcp-ci")
18 def buildInfo = Artifactory.newBuildInfo()
19
20 def projectNamespace = "mirantis/${PROJECT_NAMESPACE}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040021
22 def dockerRepository = DOCKER_REGISTRY
23 def docker_dev_repo = "docker-dev-local"
24 def docker_prod_repo = "docker-prod-local"
Mikhail Ivanovd959b312017-06-20 13:58:20 +040025 def dockerFileOption
26
27 def buildTag = "oss-ci-docker-${BUILD_NUMBER}-${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER}"
28
29 if (DOCKERFILE_PATH.trim() == ''){
30 dockerFileOption = ''
31 }
32 else {
33 dockerFileOption = "--file ${DOCKERFILE_PATH}"
34 }
35 def buildCmd = "docker build --tag ${buildTag} ${dockerFileOption} --rm ${CONTEXT_PATH}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040036
37 def imageTagsList = IMAGE_TAGS.tokenize(" ")
38 def workspace = common.getWorkspace()
39
40 gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
41
42 try{
43 stage("checkout") {
44 gerrit.gerritPatchsetCheckout([
45 credentialsId : CREDENTIALS_ID,
46 withWipeOut : true,
47 ])
48 }
49 stage("build image"){
Mikhail Ivanovd959b312017-06-20 13:58:20 +040050 sh "${buildCmd}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040051 imageTagsList << "${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}"
52 for (imageTag in imageTagsList) {
Mikhail Ivanovd959b312017-06-20 13:58:20 +040053 sh "docker tag ${buildTag} ${dockerRepository}/${projectNamespace}/${IMAGE_NAME}:${imageTag}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040054 }
55 }
56 stage("publish image"){
57 if (gerritChange.status != "MERGED"){
58 for (imageTag in imageTagsList) {
59 artifactory.uploadImageToArtifactory(artifactoryServer,
60 dockerRepository,
Ilya Kharinffee83f2017-06-01 16:11:56 +040061 "${projectNamespace}/${IMAGE_NAME}",
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040062 imageTag,
63 docker_dev_repo,
64 buildInfo)
65 currentBuild.description = "image: ${IMAGE_NAME}:${imageTag}<br>"
66 }
67 } else {
68 def properties = [
69 'com.mirantis.gerritChangeId': "${GERRIT_CHANGE_ID}",
70 'com.mirantis.gerritPatchsetNumber': "${GERRIT_PATCHSET_NUMBER}",
71 'com.mirantis.gerritChangeNumber' : "${GERRIT_CHANGE_NUMBER}"
72 ]
73 // Search for an artifact with required properties
74 def artifactURI = artifactory.uriByProperties(artifactoryServer.getUrl(),
75 properties)
76 // Get build info: build id and job name
77 if ( artifactURI ) {
78 def buildProperties = artifactory.getPropertiesForArtifact(artifactURI)
79 //promote docker image
80 artifactory.promoteDockerArtifact(artifactoryServer.getUrl(),
81 docker_dev_repo,
82 docker_prod_repo,
Ilya Kharinffee83f2017-06-01 16:11:56 +040083 "${projectNamespace}/${IMAGE_NAME}",
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040084 buildProperties.get('com.mirantis.targetTag').join(','),
85 'latest')
86 } else {
87 throw new RuntimeException("Artifacts were not found, nothing to promote")
88 }
89 }
90 }
91 } catch (Throwable e) {
92 currentBuild.result = 'FAILURE'
93 common.errorMsg("Build failed due to error: ${e}")
94 throw e
95 } finally {
96 common.sendNotification(currentBuild.result, "",["slack"])
97 }
98}