SHARE
TWEET

vladislavnaumov

a guest Nov 17th, 2019 84 in 10 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * pipeline for promote kaas openstack cluster-api docker images
  3.  * CREDENTIALS_ID - gerrit credentials id
  4.  * GERRIT_HOST - Gerrit host
  5.  * KAAS_PIPELINE_REFSPEC - Refspec for kaas related pipelines, for testing additional pipeline changes, generally using GERRIT_REFSPEC
  6.  * GERRIT_REFSPEC - Parameter also used for fetching groovy pipeline from kaas repo [KAAS_PIPELINE_REFSPEC],
  7.  * default value used in case of non-triggered run, otherwise gerrit trigger will overwrite it
  8.  * DRY_RUN                         - (bool) if true, dont push charts in PROD
  9.  *
  10.  * dockerRegistry - url to docker registry
  11.  * devDockerRepo - url to dev docker repo
  12.  * prodDockerRepo - url to prod docker repo [WARNING! prod artifactory promoting temporally disabled until KaaS release, promoting only in dev*]
  13. **/
  14.  
  15. def common = new com.mirantis.mk.Common()
  16. def jUtils = new com.mirantis.mk.JenkinsUtils()
  17. def gerrit = new com.mirantis.mk.Gerrit()
  18. def artifactory = new com.mirantis.mcp.MCPArtifactory()
  19. def artifactoryServer = Artifactory.server('mcp-ci')
  20. def buildInfo = Artifactory.newBuildInfo()
  21. jobCatchedErrors = 'No build errors'
  22. dryRun = env.DRY_RUN ? env.DRY_RUN.toBoolean() : false
  23.  
  24. // Artifact repo management
  25. dockerRegistry = 'docker-kaas-local.docker.mirantis.net'
  26. devDockerRepo = 'docker-dev-kaas-local'
  27. prodDockerRepo = 'docker-kaas-local'
  28.  
  29.  
  30. node('docker') {
  31.     def workspace = common.getWorkspace()
  32.     // Define Jenkins user id's for docker operations
  33.     def jenkinsUID = common.getJenkinsUid()
  34.     def jenkinsGID = common.getJenkinsGid()
  35.     jenkinsUser = "${jenkinsUID}:${jenkinsGID}"
  36.  
  37.     //avoid root-owned docker artifacts before kaasLibrary scm
  38.     sh(script: """\
  39.         docker run --rm \
  40.         -v ${workspace}:/code \
  41.         busybox sh -c 'chown -R ${jenkinsUser} /code'
  42.         """
  43.     )
  44.  
  45.     // Import external kaas groovy library
  46.     stage('Getting kaas-libraries') {
  47.          checkout scm
  48.          baseKaas = load 'hack/backend/kaasLibrary.groovy'
  49.     }
  50.  
  51.     // Check if dependency jobs passed successfully
  52.     depsResult = jUtils.checkDependencyJobs()
  53.     if (!depsResult.status){
  54.         return
  55.     }
  56.  
  57.     // Docker image parameters
  58.     def imageTag = ''
  59.     def promoteTag = 'NO_TAG'
  60.     def imageRepo = 'core'
  61.  
  62.     try {
  63.  
  64.         stage('Checkout') {
  65.             if (GERRIT_EVENT_TYPE == 'ref-updated' ){
  66.                 common.warningMsg('Gerrit-triggered run. Tagged release from tag refspec of mirantis branch in kaas/cluster-api-provider-openstack repo')
  67.                 gerrit.gerritPatchsetCheckout('ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/kaas/cluster-api-provider-openstack' , 'mirantis', GERRIT_REFNAME, CREDENTIALS_ID)
  68.             } else {
  69.                 common.warningMsg("Gerrit-triggered run. Product code and groovy pipeline fetched from ${GERRIT_REFSPEC} in kaas/cluster-api-provider-openstack repo")
  70.                 gerrit.gerritPatchsetCheckout([
  71.                     credentialsId: CREDENTIALS_ID,
  72.                     withWipeOut: true,
  73.                 ])
  74.             }
  75.         }
  76.  
  77.         stage('Getting valid image tags and names for promote') {
  78.             imageNamesList = sh(script: "make DEST=${workspace} GOOS=linux image-names", returnStdout: true).trim().split().collect{ it as String }
  79.             imageTag = sh(script: "make DEST=${workspace} GOOS=linux version", returnStdout: true).trim()
  80.  
  81.             if (GERRIT_EVENT_TYPE == 'ref-updated'){
  82.                 def gitTag = GERRIT_REFNAME.replace('refs/tags/v', '').trim()
  83.                 def additionalReleaseImages = ['kind-cache', 'squid-cache']
  84.                 imageNamesList.addAll(additionalReleaseImages)
  85.                 imageTag = gitTag
  86.  
  87.                 common.warningMsg("""PAY ATTENTION: Tagged release!
  88.                 ${additionalReleaseImages} images will be promoted.
  89.                 all release artifacts will be tagged with git tag ${imageTag} instead of 'make version'""")
  90.             }
  91.             promoteTag = imageTag
  92.         }
  93.  
  94.         stage('Promote KaaS product images in PROD') {
  95.             imageNamesList.each {
  96.                 try {
  97.                     common.infoMsg("Image ${it} will be promoted in production repo with tag ${imageTag}")
  98.                     if (dryRun) {
  99.                         common.warningMsg('DRY_RUN enabled, skipping push')
  100.                     } else {
  101.                         artifactory.promoteDockerArtifact(artifactoryServer.getUrl(),
  102.                                                           devDockerRepo,
  103.                                                           prodDockerRepo,
  104.                                                           "${imageRepo}/${it}",
  105.                                                           imageTag,
  106.                                                           promoteTag,
  107.                                                           true)
  108.  
  109.                     }
  110.                 } catch (e) {
  111.                     jobCatchedErrors = jobCatchedErrors + "${e.message}\n"
  112.                     currentBuild.result = 'UNSTABLE'
  113.                     common.errorMsg("Some images may not be promoted properly. Errors during promotion process: ${e}")
  114.                 }
  115.             }
  116.         }
  117.  
  118.         // PAY attention, below stages is additional and may be removed in future
  119.         if (GERRIT_EVENT_TYPE == 'ref-updated'){
  120.             // Additional images will be built only in case of tagged release, otherwise we keep previous `latest` versions in repos
  121.             stage('Promote custom images for internal purposes') {
  122.                 try {
  123.                     common.infoMsg("""Latest test/frontend ui-infra image
  124.                         will be promoted in ${prodDockerRepo} repo with tag ${imageTag}
  125.                         for integration testing.""")
  126.                     sh('docker pull docker-dev-kaas-local.docker.mirantis.net/core/test/frontend:latest')
  127.                     sh("docker tag docker-dev-kaas-local.docker.mirantis.net/core/test/frontend:latest ${dockerRegistry}/${imageRepo}/test/frontend:${imageTag}")
  128.                     if (dryRun) {
  129.                         common.warningMsg('DRY_RUN enabled, skipping push')
  130.                     } else {
  131.                         artifactory.uploadImageToArtifactory(
  132.                             artifactoryServer,
  133.                             dockerRegistry,
  134.                             "${imageRepo}/test/frontend",
  135.                             imageTag,
  136.                             prodDockerRepo,
  137.                             buildInfo)
  138.                     }
  139.  
  140.                     common.infoMsg("kind-cache image will be promoted in ${devDockerRepo} repo with tag: latest")
  141.                     if (dryRun) {
  142.                         common.warningMsg('DRY_RUN enabled, skipping push')
  143.                     } else {
  144.                         artifactory.promoteDockerArtifact(artifactoryServer.getUrl(),
  145.                                                           devDockerRepo,
  146.                                                           devDockerRepo,
  147.                                                           "${imageRepo}/kind-cache",
  148.                                                           imageTag,
  149.                                                           'latest',
  150.                                                           true)
  151.                     }
  152.                 } catch (e) {
  153.                     jobCatchedErrors = jobCatchedErrors + "${e.message}\n"
  154.                     common.errorMsg("Main build actions passed successfully, additional steps failed due: ${e}")
  155.                     currentBuild.result = 'UNSTABLE'
  156.                 }
  157.             }
  158.         }
  159.  
  160.     } catch (e) {
  161.         // If there was an error or exception thrown, the build failed
  162.         currentBuild.result = 'FAILURE'
  163.         jobCatchedErrors = jobCatchedErrors + "${e.message}\n"
  164.        throw e
  165.     } finally {
  166.         def imageFullNamesList = []
  167.         imageNamesList.each {
  168.             imageFullNamesList.add("${dockerRegistry}/${imageRepo}/${it}:${imageTag}")
  169.         }
  170.         def prettyHtmlPackages = ''
  171.         if (currentBuild.result != 'FAILURE'){
  172.             imageFullNamesList.each {
  173.                 prettyHtmlPackages = "${prettyHtmlPackages} docker pull ${it}<br/>"
  174.             }
  175.         }
  176.         currentBuild.description = """
  177.         <p>
  178.           <b>DRY RUN enabled: </b>${dryRun}<br/>
  179.           <b>Images were built</b>:<br/>
  180.           ${prettyHtmlPackages}<br/>
  181.           <b>Errors</b>: ${jobCatchedErrors}<br/>
  182.         </p>
  183.         """
  184.  
  185.         stage('Cleanup') {
  186.             // cleanup workspace
  187.             deleteDir()
  188.         }
  189.     }
  190. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top