blob: 2973621497b372b780cf69440a7d9b10ceaebc95 [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"){
39 imageTagsList << "${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}"
40 for (imageTag in imageTagsList) {
Mikhail Ivanov2a4f3132017-05-04 14:37:12 +040041 sh "docker build -f ${DOCKERFILE_PATH}/Dockerfile -t ${dockerRepository}/${projectNamespace}/${projectModule}:${imageTag} --rm ."
Mikhail Ivanovff29afa2017-05-02 16:54:14 +040042 }
43 }
44 stage("publish image"){
45 if (gerritChange.status != "MERGED"){
46 for (imageTag in imageTagsList) {
47 artifactory.uploadImageToArtifactory(artifactoryServer,
48 dockerRepository,
49 "${projectNamespace}/${projectModule}",
50 imageTag,
51 docker_dev_repo,
52 buildInfo)
53 currentBuild.description = "image: ${IMAGE_NAME}:${imageTag}<br>"
54 }
55 } else {
56 def properties = [
57 'com.mirantis.gerritChangeId': "${GERRIT_CHANGE_ID}",
58 'com.mirantis.gerritPatchsetNumber': "${GERRIT_PATCHSET_NUMBER}",
59 'com.mirantis.gerritChangeNumber' : "${GERRIT_CHANGE_NUMBER}"
60 ]
61 // Search for an artifact with required properties
62 def artifactURI = artifactory.uriByProperties(artifactoryServer.getUrl(),
63 properties)
64 // Get build info: build id and job name
65 if ( artifactURI ) {
66 def buildProperties = artifactory.getPropertiesForArtifact(artifactURI)
67 //promote docker image
68 artifactory.promoteDockerArtifact(artifactoryServer.getUrl(),
69 docker_dev_repo,
70 docker_prod_repo,
71 "${projectNamespace}/${projectModule}",
72 buildProperties.get('com.mirantis.targetTag').join(','),
73 'latest')
74 } else {
75 throw new RuntimeException("Artifacts were not found, nothing to promote")
76 }
77 }
78 }
79 } catch (Throwable e) {
80 currentBuild.result = 'FAILURE'
81 common.errorMsg("Build failed due to error: ${e}")
82 throw e
83 } finally {
84 common.sendNotification(currentBuild.result, "",["slack"])
85 }
86}