From 3f0d0d8501039446dfb0deacd96cb8808d1bb1a2 Mon Sep 17 00:00:00 2001
From: Yuxiang Zhu <yuxzhu@redhat.com>
Date: Mar 06 2019 03:54:19 +0000
Subject: CI/CD: repotracker trigger enhancement


1. Split waiverdb-repotracker-trigger into 2 jobs: `waiverdb-trigger-on-latest-tag` and `waiverdb-trigger-on-stage-tag`.
2. Allow to customize messaging provider (for pipeline debugging).
3. Fix an error about TRACKED_CONTAINER_REPO.

---

diff --git a/openshift/pipelines/jobs/waiverdb-repotracker-trigger.env b/openshift/pipelines/jobs/waiverdb-repotracker-trigger.env
deleted file mode 100644
index 1f18f42..0000000
--- a/openshift/pipelines/jobs/waiverdb-repotracker-trigger.env
+++ /dev/null
@@ -1 +0,0 @@
-NAME=waiverdb-repotracker-trigger
diff --git a/openshift/pipelines/jobs/waiverdb-repotracker-trigger.tmpl b/openshift/pipelines/jobs/waiverdb-repotracker-trigger.tmpl
deleted file mode 100644
index 9637c92..0000000
--- a/openshift/pipelines/jobs/waiverdb-repotracker-trigger.tmpl
+++ /dev/null
@@ -1 +0,0 @@
-waiverdb-repotracker-trigger.yaml
diff --git a/openshift/pipelines/jobs/waiverdb-trigger-on-latest-tag.env b/openshift/pipelines/jobs/waiverdb-trigger-on-latest-tag.env
new file mode 100644
index 0000000..a390db8
--- /dev/null
+++ b/openshift/pipelines/jobs/waiverdb-trigger-on-latest-tag.env
@@ -0,0 +1,4 @@
+NAME=waiverdb-trigger-on-latest-tag
+MESSAGING_TOPIC=Consumer.rh-jenkins-ci-plugin.c3i-waiverdb-trigger-on-latest-tag.VirtualTopic.eng.repotracker.container.tag.>
+TEST_JOB_NAME=waiverdb-stage-integration-test
+TRACKED_TAG=latest
diff --git a/openshift/pipelines/jobs/waiverdb-trigger-on-latest-tag.tmpl b/openshift/pipelines/jobs/waiverdb-trigger-on-latest-tag.tmpl
new file mode 100644
index 0000000..9637c92
--- /dev/null
+++ b/openshift/pipelines/jobs/waiverdb-trigger-on-latest-tag.tmpl
@@ -0,0 +1 @@
+waiverdb-repotracker-trigger.yaml
diff --git a/openshift/pipelines/jobs/waiverdb-trigger-on-stage-tag.env b/openshift/pipelines/jobs/waiverdb-trigger-on-stage-tag.env
new file mode 100644
index 0000000..d026a2c
--- /dev/null
+++ b/openshift/pipelines/jobs/waiverdb-trigger-on-stage-tag.env
@@ -0,0 +1,4 @@
+NAME=waiverdb-trigger-on-stage-tag
+MESSAGING_TOPIC=Consumer.rh-jenkins-ci-plugin.c3i-waiverdb-trigger-on-stage-tag.VirtualTopic.eng.repotracker.container.tag.>
+TEST_JOB_NAME=waiverdb-prod-integration-test
+TRACKED_TAG=stage
diff --git a/openshift/pipelines/jobs/waiverdb-trigger-on-stage-tag.tmpl b/openshift/pipelines/jobs/waiverdb-trigger-on-stage-tag.tmpl
new file mode 100644
index 0000000..9637c92
--- /dev/null
+++ b/openshift/pipelines/jobs/waiverdb-trigger-on-stage-tag.tmpl
@@ -0,0 +1 @@
+waiverdb-repotracker-trigger.yaml
diff --git a/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile b/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile
index a37e1ce..dab2883 100644
--- a/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile
+++ b/openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile
@@ -5,27 +5,12 @@ properties([
     // example: https://github.com/jenkinsci/jms-messaging-plugin/blob/9b9387c3a52f037ba0d019c2ebcf2a2796fc6397/src/test/java/com/redhat/jenkins/plugins/ci/integration/AmqMessagingPluginIntegrationTest.java
     [$class: 'CIBuildTrigger',
       providerData: [$class: 'ActiveMQSubscriberProviderData',
-        name: 'Red Hat UMB',
-        overrides: [topic: "Consumer.rh-jenkins-ci-plugin.c3i-${env.JOB_BASE_NAME}.VirtualTopic.eng.repotracker.container.tag.>"],
-        selector: "repo='${TRACKED_CONTAINER_REPO}'",
-        checks: [
-          [field: '$.action', expectedValue: 'added|updated'],
-        ],
+        name: params.MESSAGING_PROVIDER,
+        overrides: [topic: params.MESSAGING_TOPIC],
+        selector: "repo = '${params.TRACKED_CONTAINER_REPO}' AND action IN ('added', 'updated') AND tag = '${params.TRACKED_TAG}'",
       ],
     ],
   ]),
-  parameters([
-    stringParam(
-     defaultValue: '',
-     description: '(Used by CIBuildTrigger internally)',
-     name: 'CI_MESSAGE'
-    ),
-    stringParam(
-     defaultValue: '',
-     description: '(Used by CIBuildTrigger internally)',
-     name: 'MESSAGE_HEADERS'
-    ),
- ]),
 ])
 
 if (!params.CI_MESSAGE) {
@@ -64,22 +49,13 @@ podTemplate(
 ) {
   node(label) {
     stage('trigger test') {
-      def promotionMap = [
-        'latest': 'stage',
-        'stage': 'prod',
-      ]
       def message = readJSON text: params.CI_MESSAGE
-      def changedTag = message.tag
-      echo "Tag :$changedTag is ${message.action} in ${message.repo}. New digest: ${message.digest}"
-      def promoteTo = promotionMap[changedTag]
-      if (!promoteTo) {
-        error("I don't know where to promote :$changedTag. Aborting...")
-      }
+      echo "Tag :${message.tag} is ${message.action} in ${message.repo}. New digest: ${message.digest}"
       def image = "${message.repo}@${message.digest}"
-      echo "Triggering a job to test if $image meets all criteria of desired tag :${promoteTo}"
+      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', "waiverdb-${promoteTo}-integration-test")
+        def testBcSelector = openshift.selector('bc', params.TEST_JOB_NAME)
         def buildSelector = testBcSelector.startBuild(
             '-e', "IMAGE=${image}",
           )
diff --git a/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml b/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml
index ebd64f6..d4d1d2f 100644
--- a/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml
+++ b/openshift/pipelines/templates/waiverdb-repotracker-trigger.yaml
@@ -23,12 +23,24 @@ parameters:
 - name: TRACKED_CONTAINER_REPO
   displayName: Container repo to be tracked
   value: "quay.io/factory2/waiverdb"
+- name: TRACKED_TAG
+  displayName: Name of tag to be tracked
+  required: true
 - name: JENKINS_AGENT_IMAGE
   displayName: Container image for Jenkins slave pods
   value: docker-registry.engineering.redhat.com/factory2/waiverdb-jenkins-slave:latest
 - name: OPENSHIFT_CLOUD_NAME
   displayName: Name of OpenShift cloud in Jenkins master configuration
   value: openshift
+- name: MESSAGING_PROVIDER
+  displayName: Name of the JMS messaging provider
+  value: Red Hat UMB
+- name: MESSAGING_TOPIC
+  displayName: Name of the topic that the trigger subscribes to
+  value: "Consumer.rh-jenkins-ci-plugin.c3i-waiverdb-repotracker-trigger.VirtualTopic.eng.repotracker.container.tag.>"
+- name: TEST_JOB_NAME
+  displayName: Name of integration test job to trigger
+  required: true
 objects:
 - kind: ServiceAccount
   apiVersion: v1
@@ -64,10 +76,6 @@ objects:
       type: JenkinsPipeline
       jenkinsPipelineStrategy:
         env:
-        - name: "WAIVERDB_GIT_REPO"
-          value: "${WAIVERDB_GIT_REPO}"
-        - name: "WAIVERDB_GIT_REF"
-          value: "${WAIVERDB_GIT_REF}"
         - name: "OPENSHIFT_CLOUD_NAME"
           value: "${OPENSHIFT_CLOUD_NAME}"
         - name: "JENKINS_AGENT_IMAGE"
@@ -76,4 +84,17 @@ objects:
           value: "${NAME}-jenkins-slave"
         - name: "TRACKED_CONTAINER_REPO"
           value: "${TRACKED_CONTAINER_REPO}"
+        - name: "TRACKED_TAG"
+          value: "${TRACKED_TAG}"
+        - name: "TEST_JOB_NAME"
+          value: "${TEST_JOB_NAME}"
+        - name: MESSAGING_PROVIDER
+          value: "${MESSAGING_PROVIDER}"
+        - name: MESSAGING_TOPIC
+          value: "${MESSAGING_TOPIC}"
+        # CI_MESSAGE and MESSAGE_HEADERS are used internally by JMS messaging plugin
+        - name: CI_MESSAGE
+          value:
+        - name: MESSAGE_HEADERS
+          value:
         jenkinsfilePath: openshift/pipelines/templates/waiverdb-repotracker-trigger.Jenkinsfile

