Advertisement
jitendar-singh

pipeline

Sep 22nd, 2022
2,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 5.35 KB | None | 0 0
  1. /**
  2.  * A Jenkins pipeline demonstrating how layered product QE teams can perform their OCP
  3.  * layered product testing when integrating into the Interop QE CI system using their
  4.  * triggering service offering.
  5.  */
  6.  
  7. import java.time.Instant
  8.  
  9. /**
  10.  * Get xUnit urls for the given build. Requires the use of the NonCPS
  11.  * annotation as getArtifacts method uses Jenkins API.
  12.  * @return xUnit urls for the build
  13. */
  14. @NonCPS
  15. def getXunitUrls() {
  16.     List xunitUrls = []
  17.     List artifacts = currentBuild.rawBuild.getArtifacts()
  18.     for (String artifact in artifacts) {
  19.         if (artifact.getFileName().endsWith('.xml')) {
  20.             xunitUrls.add(env.BUILD_URL + "artifact/${artifact}")
  21.         }
  22.     }
  23.     return xunitUrls
  24. }
  25.  
  26. node("master") {
  27.     // declare variables
  28.     String messageTopic = "product-scenario.test.complete"
  29.     def ocp_info
  30.     def message
  31.     def product_info
  32.     Boolean allowEmpty = false
  33.     String testStatus
  34.     def testRuntime
  35.     timestamps{
  36.         stage("parse message"){
  37.             try{
  38.                 sh "printenv"
  39.                 // get ci message and write to disk
  40.                 message = readJSON text: params.get("CI_MESSAGE")
  41.                 writeJSON file: "ci-message.json", json: message
  42.  
  43.                 // gather OpenShift details
  44.                 for (item in message['artifact']['products']) {
  45.                     if (item['name'].toLowerCase() == 'openshift') {
  46.                         ocp_info = item
  47.                     } else {
  48.                         product_info = item
  49.                     }
  50.                 }
  51.  
  52.                 println("Testing OCP build: ${ocp_info['nvr']}")
  53.             } catch (e){
  54.                 allowEmpty = true
  55.                 println("${e.toString()}")
  56.                 throw e
  57.             } finally {
  58.                 archiveArtifacts artifacts: "*.json", fingerprint: false, allowEmptyArchive: allowEmpty
  59.             }
  60.         }
  61.  
  62.         stage("setup test environment"){
  63.             println("Setting up test environment...")
  64.         }
  65.  
  66.         stage("Run Tests"){
  67.             dir("repo"){
  68.                 println("Running Tests")
  69.  
  70.                 // save start time
  71.                 startTime = new Date().getTime()
  72.  
  73.                 // clone mock_openshift repo to get sample xunit results
  74.                 git url: "https://github.com/openshift/jenkins.git", branch: "master"
  75.                 sh '''
  76.                oc login https://api.daily-4.12-092201.dev.openshiftappsvc.org:6443 -u kubeadmin -p 9NJXn-WQEUD-XkYdY-zuWdL --insecure-skip-tls-verify
  77.                export KUBECONFIG=~/.kube/config
  78.                ./scripts/test-jenkins-template-install.sh
  79.                '''
  80.                 // archive the xunit results from running the tests
  81.                 archiveArtifacts artifacts: "out/smoke/*.xml", fingerprint: false
  82.  
  83.                 // save stop time
  84.                 stopTime = new Date().getTime()
  85.             }
  86.  
  87.             // calculate test total runtime
  88.             testRuntime = ((stopTime - startTime) / 1000).toInteger()
  89.  
  90.             //analyze the results, and set the test status
  91.             testStatus = "passed"
  92.  
  93.             println("Finished test execution")
  94.         }
  95.  
  96.         stage("build ci message") {
  97.             println("Building product-scenario.test.complete ci message..")
  98.  
  99.             // build list of xunit urls
  100.             List xunitUrls = getXunitUrls()
  101.  
  102.             // override default message topic (development usage)
  103.             if (message['contact']['email'] != "pit-qe@redhat.com") {
  104.                 messageTopic += ".${message['contact']['email'].toString().split('@')[0]}"
  105.             }
  106.  
  107.             // build message
  108.             message['contact']['name'] = product_info['name']
  109.             message['contact']['team'] = product_info['name']
  110.             message['contact']['email'] = "jitsingh@redhat.com"
  111.             message['contact']['url'] = env.JENKINS_URL
  112.  
  113.             message['run'] = [:]
  114.             message['run']['url'] = env.BUILD_URL
  115.             message['run']['log'] = env.BUILD_URL + "console"
  116.  
  117.             message['test'] = [:]
  118.             message['test']['category'] = "interoperability"
  119.             message['test']['namespace'] = "interop"
  120.             message['test']['type'] = "layered-product"
  121.             message['test']['result'] = testStatus
  122.             message['test']['runtime'] = testRuntime
  123.             message['test']['xunit_urls'] = xunitUrls
  124.  
  125.             message['generated_at'] = Instant.now().toString()
  126.  
  127.             println("${message}")
  128.  
  129.             println("CI message product-scenario.test.complete finished building!")
  130.  
  131.             // archive product-scenario.test.complete message
  132.             archiveArtifacts artifacts: "*.json", fingerprint: false
  133.         }
  134.  
  135.         stage("send ci message") {
  136.             println("Sending product-scenario.test.complete ci message..")
  137.  
  138.             // set topic
  139.             String topic = String.join(".", "VirtualTopic.qe.ci", "${messageTopic}")
  140.  
  141.             //MESSAGE_PROVIDER = Red Hat UMB or Red Hat UMB (stage) -> if accessible
  142.             sendCIMessage providerName: params.MESSAGE_PROVIDER,
  143.                     overrides: [topic: topic],
  144.                     messageType: "Custom",
  145.                     messageContent: message.toString()
  146.  
  147.             println("CI message product-scenario.test.complete finished sending!")
  148.         }
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement