#50 Get message from datagrepper when not triggered by user
Merged 4 years ago by mkovarik. Opened 4 years ago by mkovarik.
mkovarik/c3i-library workauround_empty_ci_message  into  master

@@ -54,6 +54,11 @@ 

  c3i_quay_address: quay.io

  c3i_quay_namespace: factory2

  

+ c3i_datagrepper_url: https://datagrepper.engineering.redhat.com

+ c3i_messaging_topic_consumer_part: "Consumer.rh-jenkins-ci-plugin"

+ c3i_messaging_greenwave_topic_virtualtopic_part: "VirtualTopic.eng.greenwave.decision.update"

+ c3i_messaging_repotracker_topic_virtualtopic_part: "VirtualTopic.eng.repotracker.container.tag.>"

+ 

  c3i_dev_image_tag: "latest"

  

  c3i_lib_branch: master

@@ -43,7 +43,29 @@ 

              // e.g. quay.io/factory2/waiverdb@sha256:35201c572fc8a137862b7a256476add8d7465fa5043d53d117f4132402f8ef6b

              env.IMAGE = "${params.SOURCE_CONTAINER_REPO}@${digest}"

            } else if (!params.IMAGE) {

-             error("This build is not started by a CI message. Only configurations were done.")

+             // Triggred by CI_MESSAGE but it's empty - CID-5291

+             if (!currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')) {

+               def datagrepperdata = sh(returnStdout: true, script: "curl -k {{ c3i_datagrepper_url }}/raw?delta=300&topic=/topic/{{ c3i_messaging_greenwave_topic_virtualtopic_part }}&contains=${params.DECISION_CONTEXT_REGEX}")

+               def messages = readJSON text: datagrepperdata

+               messages.raw_messages.any {

+                 def msg = it.msg

+                 if ( msg?.decision_context.find(/${env.DECISION_CONTEXT_REGEX}/) &&

+                      msg?.subject_type == 'container-image' &&

+                      msg?.policies_satisfied == true &&

+                      msg?.subject_identifier.find(/${env.SUBJECT_IDENTIFIER_REGEX}/)

+                    ){

+                   def digest = (msg.subject_identifier =~ /@(sha256:\w+)$/)[0][1]

+                   env.IMAGE = "${params.SOURCE_CONTAINER_REPO}@${digest}"

+                   return true

+                 }

+               }

+               if (!env.IMAGE) {

+                 error('Triggered by CI_MESSAGE but message is empty and not found in datagrepper')

+               }

+             }

+             else {

+               error("This build is not started by a CI message. Only configurations were done.")

+             }

            }

            echo "Starting promotion of image ${env.IMAGE} to ${env.PROMOTING_DESTINATION}:${params.TARGET_TAG}..."

            // Setting up registry credentials

@@ -36,7 +36,7 @@ 

        - name: SOURCE_CONTAINER_REPO

          value: {{ job_vars.source_container_repo_prefix }}/{{ item }}

        - name: MESSAGING_TOPIC

-         value: {{ job_vars.messaging_greenwave_topic_pre }}-{{ item }}{{ c3i_test_subscriber }}-{{ job_vars.messaging_greenwave_topic_post }}

+         value: {{ c3i_messaging_topic_consumer_part }}.c3i-{{ item }}-{{ job_vars.name_post }}{{ c3i_test_subscriber }}.{{ c3i_messaging_greenwave_topic_virtualtopic_part }}

        - name: MAIL_ADDRESS

          value: {{ c3i_mail_address }}

        # CI_MESSAGE is used internally by JMS messaging plugin

@@ -1,8 +1,6 @@ 

  ---

  name_post: "greenwave-promote-to-prod"

  template: greenwave-promote.yml

- messaging_greenwave_topic_pre: "Consumer.rh-jenkins-ci-plugin.c3i"

- messaging_greenwave_topic_post: "greenwave-promote-to-prod.VirtualTopic.eng.greenwave.decision.update"

  decision_context_regex: c3i_promote_stage_to_prod

  target_tag: prod

  promoting_destination_prefix: "{{ c3i_quay_address }}/{{ c3i_quay_namespace }}"

@@ -1,8 +1,6 @@ 

  ---

  name_post: "greenwave-promote-to-stage"

  template: greenwave-promote.yml

- messaging_greenwave_topic_pre: "Consumer.rh-jenkins-ci-plugin.c3i-"

- messaging_greenwave_topic_post: "greenwave-promote-to-stage.VirtualTopic.eng.greenwave.decision.update"

  decision_context_regex: c3i_promote_dev_to_stage

  target_tag: stage

  promoting_destination_prefix: "{{ c3i_quay_address }}/{{ c3i_quay_namespace }}"

@@ -1,6 +1,5 @@ 

  ---

  name: "{{ c3i_component }}-trigger-on-latest-tag"

  template: trigger-on-tag.yml

- messaging_repotracker_topic: Consumer.rh-jenkins-ci-plugin.c3i-{{ c3i_test_subscriber | default(c3i_component) }}-trigger-on-latest-tag.VirtualTopic.eng.repotracker.container.tag.>

  environment: stage

  tracked_tag: latest

@@ -1,6 +1,5 @@ 

  ---

  name: "{{ c3i_component }}-trigger-on-stage-tag"

  template: trigger-on-tag.yml

- messaging_repotracker_topic: Consumer.rh-jenkins-ci-plugin.c3i-{{ c3i_test_subscriber | default(c3i_component) }}-trigger-on-stage-tag.VirtualTopic.eng.repotracker.container.tag.>

  environment: prod

  tracked_tag: stage

@@ -32,7 +32,26 @@ 

            } else if (params.IMAGE) {

              env.PIPELINE_ID = "c3i-{{ c3i_component }}-tag-${params.TRACKED_TAG}-manual-${UUID.randomUUID().toString().substring(0, 4)}"

            } else {

-             error("This build is not started by a CI message. Only configurations were done.")

+             // Triggred by CI_MESSAGE but it's empty - CID-5291

+             if (!currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')) {

+               def datagrepperdata = sh(returnStdout: true, script: "curl -k {{ c3i_datagrepper_url }}/raw?delta=300&category=repotracker&contains=${params.TRACKED_CONTAINER_REPO}")

+               def messages = readJSON text: datagrepperdata

+               messages.raw_messages.any {

+                 def msg = it.msg

+                 if ( msg?.action in ('added', 'updated') &&

+                      msg?.tag == params.TRACKED_TAG &&

+                      msg?.repo == params.TRACKED_CONTAINER_REPO

+                    ){

+                   echo "Tag :${msg.tag} is ${msg.action} in ${msg.repo}. New digest: ${msg.digest}"

+                   env.IMAGE = "${msg.repo}@${msg.digest}"

+                   env.PIPELINE_ID = "c3i-{{ c3i_component }}-tag-${params.TRACKED_TAG}-${msg.digest[-9..-1]}"

+                   return true

+                 }

+               }

+               if (!env.IMAGE) {

+                 error('Triggered by CI_MESSAGE but message is empty and not found in datagrepper')

+               }

+             }

            }

            echo "Triggering a job to test if ${env.IMAGE} meets all criteria of desired tag :${params.TRACKED_TAG}"

            env.IMAGE_IS_SCRATCH = false

@@ -16,7 +16,7 @@ 

        - name: GIT_REPO_REF

          value: {{ c3i_git_main_branch }}

        - name: MESSAGING_TOPIC

-         value: {{ job_vars.messaging_repotracker_topic }}

+         value: {{ c3i_messaging_topic_consumer_part }}.c3i-{{ job_vars.name }}{{ c3i_test_subscriber }}.{{ c3i_messaging_repotracker_topic_virtualtopic_part }}

        - name: IMAGE

          value:

        - name: OPENSHIFT_CLOUD_NAME