Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 1 | /** |
| 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 Ivanov | d959b31 | 2017-06-20 13:58:20 +0400 | [diff] [blame] | 5 | * CONTEXT_PATH - Path to build context directory |
Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 6 | * 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 | **/ |
| 11 | def artifactory = new com.mirantis.mcp.MCPArtifactory() |
| 12 | def common = new com.mirantis.mk.Common() |
| 13 | def gerrit = new com.mirantis.mk.Gerrit() |
| 14 | |
| 15 | |
| 16 | node("docker") { |
| 17 | def artifactoryServer = Artifactory.server("mcp-ci") |
| 18 | def buildInfo = Artifactory.newBuildInfo() |
| 19 | |
| 20 | def projectNamespace = "mirantis/${PROJECT_NAMESPACE}" |
Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 21 | |
| 22 | def dockerRepository = DOCKER_REGISTRY |
| 23 | def docker_dev_repo = "docker-dev-local" |
| 24 | def docker_prod_repo = "docker-prod-local" |
Mikhail Ivanov | d959b31 | 2017-06-20 13:58:20 +0400 | [diff] [blame] | 25 | 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 Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 36 | |
| 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 Ivanov | d959b31 | 2017-06-20 13:58:20 +0400 | [diff] [blame] | 50 | sh "${buildCmd}" |
Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 51 | imageTagsList << "${GERRIT_CHANGE_NUMBER}_${GERRIT_PATCHSET_NUMBER}" |
| 52 | for (imageTag in imageTagsList) { |
Mikhail Ivanov | d959b31 | 2017-06-20 13:58:20 +0400 | [diff] [blame] | 53 | sh "docker tag ${buildTag} ${dockerRepository}/${projectNamespace}/${IMAGE_NAME}:${imageTag}" |
Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | stage("publish image"){ |
| 57 | if (gerritChange.status != "MERGED"){ |
| 58 | for (imageTag in imageTagsList) { |
| 59 | artifactory.uploadImageToArtifactory(artifactoryServer, |
| 60 | dockerRepository, |
Ilya Kharin | ffee83f | 2017-06-01 16:11:56 +0400 | [diff] [blame] | 61 | "${projectNamespace}/${IMAGE_NAME}", |
Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 62 | 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 Kharin | ffee83f | 2017-06-01 16:11:56 +0400 | [diff] [blame] | 83 | "${projectNamespace}/${IMAGE_NAME}", |
Mikhail Ivanov | ff29afa | 2017-05-02 16:54:14 +0400 | [diff] [blame] | 84 | 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 | } |