#359 Use new functions from c3i library
Merged a year ago by mikeb. Opened a year ago by mkovarik.
mkovarik/waiverdb c3i-updates  into  master

@@ -291,16 +291,12 @@ 

                '-p', "WAIVERDB_IMAGESTREAM_NAME=${params.WAIVERDB_IMAGESTREAM_NAME}",

                '-p', "WAIVERDB_IMAGESTREAM_NAMESPACE=${params.WAIVERDB_IMAGESTREAM_NAMESPACE}",

              )

-             def created = openshift.apply(processed)

-             def bc = created.narrow('bc')

-             echo 'Starting a container build from the created BuildConfig...'

-             buildSelector = bc.startBuild()

-             c3i.wait(buildSelector.name())

+             def build = c3i.buildAndWait(script: this, objs: processed)

              echo 'Container build succeeds.'

-             def ocpBuild = buildSelector.object()

+             def ocpBuild = build.object()

              env.RESULTING_IMAGE_REF = ocpBuild.status.outputDockerImageReference

              env.RESULTING_IMAGE_DIGEST = ocpBuild.status.output.to.imageDigest

-             def imagestream= created.narrow('is').object()

+             def imagestream = openshift.selector('is', ['app': env.BUILDCONFIG_INSTANCE_ID]).object()

              env.RESULTING_IMAGE_REPO = imagestream.status.dockerImageRepository

              env.RESULTING_TAG = env.TEMP_TAG

            }
@@ -328,15 +324,13 @@ 

          script {

            openshift.withCluster() {

              openshift.withProject(params.WAIVERDB_INTEGRATION_TEST_BUILD_CONFIG_NAMESPACE) {

-               def testBcSelector = openshift.selector('bc', params.WAIVERDB_INTEGRATION_TEST_BUILD_CONFIG_NAME)

                echo 'Starting a functional test for the built container image...'

-               def buildSelector = testBcSelector.startBuild(

-                   '-e', "WAIVERDB_GIT_REPO=${params.WAIVERDB_GIT_REPO}",

-                   '-e', "IMAGE=${env.RESULTING_IMAGE_REPO}:${env.RESULTING_TAG}",

-                   '-e', "WAIVERDB_GIT_REF=${env.PR_NO ? env.WAIVERDB_GIT_REF : env.WAIVERDB_GIT_COMMIT}",

-                   '-e', "IMAGE_IS_SCRATCH=${params.WAIVERDB_GIT_REF != params.WAIVERDB_MAIN_BRANCH}",

-                 )

-               c3i.wait(buildSelector.name())

+               c3i.buildAndWait(script: this, objs: "bc/${params.WAIVERDB_INTEGRATION_TEST_BUILD_CONFIG_NAME}",

+                 '-e', "WAIVERDB_GIT_REPO=${params.WAIVERDB_GIT_REPO}",

+                 '-e', "IMAGE=${env.RESULTING_IMAGE_REPO}:${env.RESULTING_TAG}",

+                 '-e', "WAIVERDB_GIT_REF=${env.PR_NO ? env.WAIVERDB_GIT_REF : env.WAIVERDB_GIT_COMMIT}",

+                 '-e', "IMAGE_IS_SCRATCH=${params.WAIVERDB_GIT_REF != params.WAIVERDB_MAIN_BRANCH}"

+               )

                echo "Functional test passed."

              }

            }

@@ -1,3 +1,5 @@ 

+ library identifier: 'c3i@master', changelog: false,

+   retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git'])

  pipeline {

    agent {

      kubernetes {
@@ -56,8 +58,7 @@ 

            // but trigger the integration test job in the c3i project.

            openshift.withCluster() {

              openshift.withProject(params.BACKEND_INTEGRATION_TEST_JOB_NAMESPACE) {

-               def testBcSelector = openshift.selector('bc', params.BACKEND_INTEGRATION_TEST_JOB)

-               def buildSelector = testBcSelector.startBuild(

+               c3i.buildAndWait(script: this, objs: "bc/${params.BACKEND_INTEGRATION_TEST_JOB}",

                  '-e', "WAIVERDB_IMAGE=${env.IMAGE}",

                  '-e', "TARGET_IMAGE_REPO=factory2/waiverdb",

                  '-e', "TARGET_IMAGE_DIGEST=${env.IMAGE_DIGEST}",
@@ -65,7 +66,6 @@ 

                  '-e', "TARGET_IMAGE_VERREL=${env.BUILD_TAG}",

                  '-e', "TESTCASE_CATEGORY=${env.ENVIRONMENT}",

                  )

-               waitForBuild(buildSelector)

                echo "Integration test passed."

              }

            }
@@ -83,40 +83,3 @@ 

    def matcher = (env.IMAGE =~ /@(sha256:\w+)$/)

    return matcher ? matcher[0][1] : ''

  }

- 

- // Wait for a build to complete.

- // Taken from https://pagure.io/c3i-library/blob/master/f/src/com/redhat/c3i/util/Builder.groovy

- // Note: We can't use `c3i.wait()` here because that function switches to the default project.

- //  Filed a PR for this issue: https://pagure.io/c3i-library/pull-request/19

- def waitForBuild(build) {

-   echo "Waiting for ${build.name()} to start..."

-   timeout(5) {

-     build.watch {

-       return !(it.object().status.phase in ["New", "Pending", "Unknown"])

-     }

-   }

-   def buildobj = build.object()

-   def buildurl = buildobj.metadata.annotations['openshift.io/jenkins-build-uri']

-   if (buildurl) {

-     echo "Details: ${buildurl}"

-   }

-   if (buildobj.spec.strategy.type == "JenkinsPipeline") {

-     echo "Waiting for ${build.name()} to complete..."

-     build.logs("--tail=1")

-     timeout(60) {

-       build.watch {

-         it.object().status.phase != "Running"

-       }

-     }

-   } else {

-     echo "Following build logs..."

-     while (build.object().status.phase == "Running") {

-       build.logs("--tail=1", "--timestamps=true", "-f")

-     }

-   }

-   buildobj = build.object()

-   if (buildobj.status.phase != "Complete") {

-     error "Build ${buildobj.metadata.name} ${buildobj.status.phase}"

-   }

-   return build.name()

- }

@@ -1,4 +1,7 @@ 

  // Use scripted syntax because CIBuildTrigger currently doesn't support the declarative syntax

+ library identifier: 'c3i@master', changelog: false,

+   retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git'])

+ 

  properties([

    disableConcurrentBuilds(),

    pipelineTriggers([
@@ -66,19 +69,15 @@ 

        // e.g. If decision_context == 'c3i_promote_dev_to_stage', the image will be promoted to 'stage'.

        def targetTag = params.TARGET_TAG

        def promotionJob = params.IMAGE_PROMOTION_JOB ?: "waiverdb-promoting-to-${targetTag}"

- 

        echo "Starting a new build to promote image ${image} to :${targetTag}..."

        openshift.withCluster() {

-         def bcSelector = openshift.selector('bc', promotionJob)

-         def buildSelector = bcSelector.startBuild(

-             '-e', "IMAGE=${image}",

-             '-e', "DEST_TAG=${targetTag}",

-           )

-           bcSelector.watch {

-             return !(it.object().status.phase in ["New", "Pending"])

-           }

-           buildInfo = buildSelector.object()

-           echo "Build ${buildInfo.metadata.annotations['openshift.io/jenkins-build-uri'] ?: buildInfo.metadata.name} started."

+         def build = c3i.build(script: this, objs: "bc/${promotionJob}",

+           '-e', "IMAGE=${image}",

+           '-e', "DEST_TAG=${targetTag}",

+         )

+         c3i.waitForBuildStart(script: this, build: build)

+         buildInfo = build.object()

+         echo "Build ${buildInfo.metadata.annotations['openshift.io/jenkins-build-uri'] ?: buildInfo.metadata.name} started."

        }

      }

    }

@@ -1,4 +1,7 @@ 

  import java.text.*

+ 

+ library identifier: 'c3i@master', changelog: false,

+   retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git'])

  pipeline {

    agent {

      kubernetes {
@@ -101,33 +104,11 @@ 

                '-p', "WAIVERDB_APP_IMAGE=${params.IMAGE}",

                '-p', "WAIVERDB_REPLICAS=${webPodReplicas}",

              )

-             def objects = openshift.apply(models)

-             echo "Waiting for test pods with label environment=${env.ENVIRONMENT_LABEL} to become Ready"

-             //def rm = dcSelector.rollout()

-             def dcs = openshift.selector('dc', ['environment': env.ENVIRONMENT_LABEL])

-             def rm = dcs.rollout()

-             def pods = openshift.selector('pods', ['environment': env.ENVIRONMENT_LABEL])

-             timeout(15) {

-               pods.untilEach(webPodReplicas + 1) {

-                 def pod = it.object()

-                 if (pod.status.phase in ["New", "Pending", "Unknown"]) {

-                   return false

-                 }

-                 if (pod.status.phase == "Running") {

-                   for (cond in pod.status.conditions) {

-                       if (cond.type == 'Ready' && cond.status == 'True') {

-                           return true

-                       }

-                   }

-                   return false

-                 }

-                 error("Test pod ${pod.metadata.name} is not running. Current phase is ${pod.status.phase}.")

-               }

-             }

+             c3i.deployAndWait(script: this, objs: models, timeout: 15)

              def appPod = openshift.selector('pods', ['environment': env.ENVIRONMENT_LABEL, 'service': 'web']).object()

              env.IMAGE_DIGEST = appPod.status.containerStatuses[0].imageID.split('@')[1]

              // Run functional tests

-             def route_hostname = objects.narrow('route').object().spec.host

+             def route_hostname = openshift.selector('routes', ['environment': env.ENVIRONMENT_LABEL]).object().spec.host

              echo "Running tests against https://${route_hostname}/"

              withEnv(["WAIVERDB_TEST_URL=https://${route_hostname}/"]) {

                sh 'py.test-3 -v --junitxml=junit-functional-tests.xml functional-tests/'

@@ -90,6 +90,8 @@ 

        jenkinsPipelineStrategy:

          jenkinsfile: |-

            // Don't use external Jenkinsfile here, or Jenkins will also poll on that repo and branch

+           library identifier: 'c3i@master', changelog: false,

+           retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git'])

            pipeline {

              agent {

                kubernetes {
@@ -209,16 +211,12 @@ 

                  steps {

                    script {

                      openshift.withCluster() {

-                       def bcSelector = openshift.selector('bc',

-                         env.PAGURE_POLLING_FOR_PR == 'true' ? env.PREMERGE_JOB_NAME : env.POSTMERGE_JOB_NAME)

-                       echo 'Starting a WaiverDB build run...'

-                       def devBuild = bcSelector.startBuild(

+                       def bc = env.PAGURE_POLLING_FOR_PR == 'true' ? env.PREMERGE_JOB_NAME : env.POSTMERGE_JOB_NAME

+                       def build = c3i.build(script: this, objs: "bc/${bc}",

                          '-e', "WAIVERDB_GIT_REF=${env.WAIVERDB_GIT_BRANCH}",

                        )

-                       devBuild.watch {

-                         return !(it.object().status.phase in ["New", "Pending"])

-                       }

-                       def devBuildInfo = devBuild.object()

+                       c3i.waitForBuildStart(script: this, build: build)

+                       def devBuildInfo = build.object()

                        def downstreamBuildName = devBuildInfo.metadata.name

                        def downstreamBuildUrl = devBuildInfo.metadata.annotations['openshift.io/jenkins-build-uri']

                        echo "Downstream build ${downstreamBuildName}(${downstreamBuildUrl}) started."

@@ -1,4 +1,7 @@ 

  // Use scripted syntax because CIBuildTrigger currently doesn't support the declarative syntax

+ library identifier: 'c3i@master', changelog: false,

+   retriever: modernSCM([$class: 'GitSCMSource', remote: 'https://pagure.io/c3i-library.git'])

+ 

  properties([

    disableConcurrentBuilds(),

    pipelineTriggers([
@@ -55,15 +58,12 @@ 

        echo "Triggering a job to test if $image meets all criteria of desired tag :${message.tag}"

        def buildInfo = null

        openshift.withCluster() {

-         def testBcSelector = openshift.selector('bc', params.TEST_JOB_NAME)

-         def buildSelector = testBcSelector.startBuild(

-             '-e', "IMAGE=${image}",

-             '-e', 'IMAGE_IS_SCRATCH=false',

-           )

-           buildSelector.watch {

-             return !(it.object().status.phase in ["New", "Pending"])

-           }

-           buildInfo = buildSelector.object()

+         def build = c3i.build(script: this, objs: "bc/${params.TEST_JOB_NAME}",

+           '-e', "IMAGE=${image}",

+           '-e', 'IMAGE_IS_SCRATCH=false',

+         )

+         c3i.waitForBuildStart(script: this, build: build)

+         buildInfo = build.object()

        }

        echo "Build ${buildInfo.metadata.annotations['openshift.io/jenkins-build-uri'] ?: buildInfo.metadata.name} started."

      }