blob: 83fe66bdbbbcff4951b6582e84f140f7bf59a72e [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}"
20 def projectModule = "${GERRIT_PROJECT}"
21
22 def dockerRepository = DOCKER_REGISTRY
23 def docker_dev_repo = "docker-dev-local"
24 def docker_prod_repo = "docker-prod-local"
25
26 def imageTagsList = IMAGE_TAGS.tokenize(" ")
27 def workspace = common.getWorkspace()
28
29 gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
30
31 try{
32 stage("checkout") {
33 gerrit.gerritPatchsetCheckout([
34 credentialsId : CREDENTIALS_ID,
35 withWipeOut : true,
36 ])
37 }
38 stage("build image"){
Mikhail Ivanove2533a22017-05-11 16:58:34 +040039 containerId = sh(
40 script: "docker build -f ${DOCKERFILE_PATH}/Dockerfile -q --rm . | awk -F':' '{print \$2}'",
41 returnStdout: true
42 ).trim().take(12)
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040043 imageTagsList << "${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}"
44 for (imageTag in imageTagsList) {
Mikhail Ivanove2533a22017-05-11 16:58:34 +040045 sh "docker tag ${containerId} ${dockerRepository}/${projectNamespace}/${projectModule}:${imageTag}"
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040046 }
47 }
48 stage("publish image"){
49 if (gerritChange.status != "MERGED"){
50 for (imageTag in imageTagsList) {
51 artifactory.uploadImageToArtifactory(artifactoryServer,
52 dockerRepository,
53 "${projectNamespace}/${projectModule}",
54 imageTag,
55 docker_dev_repo,
56 buildInfo)
57 currentBuild.description = "image: ${IMAGE_NAME}:${imageTag}<br>"
58 }
59 } else {
60 def properties = [
61 'com.mirantis.gerritChangeId': "${GERRIT_CHANGE_ID}",
62 'com.mirantis.gerritPatchsetNumber': "${GERRIT_PATCHSET_NUMBER}",
63 'com.mirantis.gerritChangeNumber' : "${GERRIT_CHANGE_NUMBER}"
64 ]
65 // Search for an artifact with required properties
66 def artifactURI = artifactory.uriByProperties(artifactoryServer.getUrl(),
67 properties)
68 // Get build info: build id and job name
69 if ( artifactURI ) {
70 def buildProperties = artifactory.getPropertiesForArtifact(artifactURI)
71 //promote docker image
72 artifactory.promoteDockerArtifact(artifactoryServer.getUrl(),
73 docker_dev_repo,
74 docker_prod_repo,
75 "${projectNamespace}/${projectModule}",
76 buildProperties.get('com.mirantis.targetTag').join(','),
77 'latest')
78 } else {
79 throw new RuntimeException("Artifacts were not found, nothing to promote")
80 }
81 }
82 }
83 } catch (Throwable e) {
84 currentBuild.result = 'FAILURE'
85 common.errorMsg("Build failed due to error: ${e}")
86 throw e
87 } finally {
88 common.sendNotification(currentBuild.result, "",["slack"])
89 }
90}