blob: 7f91a545c8c9ef22613e5d68f5039b5e8c31bd15 [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
5 * CREDENTIALS_ID - gerrit credentials id
6 * DOCKERFILE_PATH - path to dockerfile in repository
7 * DOCKER_REGISTRY - url to registry
8 * PROJECT_NAMESPACE - in which namespace will be stored
9**/
10def artifactory = new com.mirantis.mcp.MCPArtifactory()
11def common = new com.mirantis.mk.Common()
12def gerrit = new com.mirantis.mk.Gerrit()
13
14
15node("docker") {
16 def artifactoryServer = Artifactory.server("mcp-ci")
17 def buildInfo = Artifactory.newBuildInfo()
18
19 def projectNamespace = "mirantis/${PROJECT_NAMESPACE}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040020
21 def dockerRepository = DOCKER_REGISTRY
22 def docker_dev_repo = "docker-dev-local"
23 def docker_prod_repo = "docker-prod-local"
24
25 def imageTagsList = IMAGE_TAGS.tokenize(" ")
26 def workspace = common.getWorkspace()
27
28 gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
29
30 try{
31 stage("checkout") {
32 gerrit.gerritPatchsetCheckout([
33 credentialsId : CREDENTIALS_ID,
34 withWipeOut : true,
35 ])
36 }
37 stage("build image"){
Mikhail Ivanove2533a22017-05-11 16:58:34 +040038 containerId = sh(
39 script: "docker build -f ${DOCKERFILE_PATH}/Dockerfile -q --rm . | awk -F':' '{print \$2}'",
40 returnStdout: true
41 ).trim().take(12)
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040042 imageTagsList << "${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}"
43 for (imageTag in imageTagsList) {
Ilya Kharinffee83f2017-06-01 16:11:56 +040044 sh "docker tag ${containerId} ${dockerRepository}/${projectNamespace}/${IMAGE_NAME}:${imageTag}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040045 }
46 }
47 stage("publish image"){
48 if (gerritChange.status != "MERGED"){
49 for (imageTag in imageTagsList) {
50 artifactory.uploadImageToArtifactory(artifactoryServer,
51 dockerRepository,
Ilya Kharinffee83f2017-06-01 16:11:56 +040052 "${projectNamespace}/${IMAGE_NAME}",
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040053 imageTag,
54 docker_dev_repo,
55 buildInfo)
56 currentBuild.description = "image: ${IMAGE_NAME}:${imageTag}<br>"
57 }
58 } else {
59 def properties = [
60 'com.mirantis.gerritChangeId': "${GERRIT_CHANGE_ID}",
61 'com.mirantis.gerritPatchsetNumber': "${GERRIT_PATCHSET_NUMBER}",
62 'com.mirantis.gerritChangeNumber' : "${GERRIT_CHANGE_NUMBER}"
63 ]
64 // Search for an artifact with required properties
65 def artifactURI = artifactory.uriByProperties(artifactoryServer.getUrl(),
66 properties)
67 // Get build info: build id and job name
68 if ( artifactURI ) {
69 def buildProperties = artifactory.getPropertiesForArtifact(artifactURI)
70 //promote docker image
71 artifactory.promoteDockerArtifact(artifactoryServer.getUrl(),
72 docker_dev_repo,
73 docker_prod_repo,
Ilya Kharinffee83f2017-06-01 16:11:56 +040074 "${projectNamespace}/${IMAGE_NAME}",
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040075 buildProperties.get('com.mirantis.targetTag').join(','),
76 'latest')
77 } else {
78 throw new RuntimeException("Artifacts were not found, nothing to promote")
79 }
80 }
81 }
82 } catch (Throwable e) {
83 currentBuild.result = 'FAILURE'
84 common.errorMsg("Build failed due to error: ${e}")
85 throw e
86 } finally {
87 common.sendNotification(currentBuild.result, "",["slack"])
88 }
89}