Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-33969

claim-plugin cannot be used with pipelines (workflows)

      It seems that the claim plugin cannot be used anymore with the pipeline jobs in Jenkins (pipeline plugin, github-organization-plugin) – those using the Jenkinsfile.

      As these are quickly becoming the standard way of defining Jenkins jobs, we do need a way to enable claiming for them.

          [JENKINS-33969] claim-plugin cannot be used with pipelines (workflows)

          Mark Jones added a comment -

          To clarify I can successfully add the step in my JenkinsFile:

          {{step([$class: 'ClaimPublisher']) }}

          and I get the message on the build page 'This build was not claimed' but there is no link to claim the build. I am logged in as admin and can claim non pipeline builds.

          plugin version: V0.8
          Jenkins version: V2.12

          Mark Jones added a comment - To clarify I can successfully add the step in my JenkinsFile: {{step( [$class: 'ClaimPublisher'] ) }} and I get the message on the build page 'This build was not claimed' but there is no link to claim the build. I am logged in as admin and can claim non pipeline builds. plugin version: V0.8 Jenkins version: V2.12

          Hi is there a way to workaround this issue?

          I add this in my Jenkinsfile:
          step([$class: 'ClaimPublisher'])

          But i see nothing after the build faliure.

          Jenkins version: 2.20
          Claim Plugin Version: 2.8

          Ramya Authappan added a comment - Hi is there a way to workaround this issue? I add this in my Jenkinsfile: step( [$class: 'ClaimPublisher'] ) But i see nothing after the build faliure. Jenkins version: 2.20 Claim Plugin Version: 2.8

          I tested this script with latest plugin version

          pipeline {
            agent any
            stages {
              stage('Build') {
                steps {
                  bat 'exit 1;'
                }
              }
            }
            post {
              always {
                step([$class: 'ClaimPublisher'])
              }
            }
          }

          And it works.

          Can you confirm issue is still present?

          Arnaud TAMAILLON added a comment - I tested this script with latest plugin version pipeline { agent any stages { stage( 'Build' ) { steps { bat 'exit 1;' } } } post { always { step([$class: 'ClaimPublisher' ]) } } } And it works. Can you confirm issue is still present?

          Hi, looks like for the declarative pipeline everything works just fine. For the scripted pipeline you'll need to add some lines of code, esp. in case you want to claim your job in case an exception happened. You may do something like. Unfortunately you won't be able to catch exceptions outside withClaim to keep the build status SUCCESS. But catching your exceptions still can be done inside withClaim.

          def withClaim(Closure closure) {
            try {
            closure()
            node {
              // workaround for older Jenkins / claim plugin versions. Won't need the following line in latest Jenkins.
              currentBuild.result = currentBuild.currentResult
              step([$class: 'ClaimPublisher'])
            }
          } catch (Exception err) {
            currentBuild.result = "FAILURE"
            node {
              step([$class: 'ClaimPublisher'])
            }
            throw err
           }
          }
          
          withClaim {
            echo currentBuild.result
            < your code >
            currentBuild.result = 'UNSTABLE'
            // currentBuild.result = 'FAILURE'
            // error "Failed"
          }
          

          Joerg Schwaerzler added a comment - Hi, looks like for the declarative pipeline everything works just fine. For the scripted pipeline you'll need to add some lines of code, esp. in case you want to claim your job in case an exception happened. You may do something like. Unfortunately you won't be able to catch exceptions outside withClaim to keep the build status SUCCESS . But catching your exceptions still can be done inside withClaim. def withClaim(Closure closure) { try { closure() node { // workaround for older Jenkins / claim plugin versions. Won't need the following line in latest Jenkins. currentBuild.result = currentBuild.currentResult step([$class: 'ClaimPublisher' ]) } } catch (Exception err) { currentBuild.result = "FAILURE" node { step([$class: 'ClaimPublisher' ]) } throw err } } withClaim { echo currentBuild.result < your code > currentBuild.result = 'UNSTABLE' // currentBuild.result = 'FAILURE' // error "Failed" }

          As a side note, the ClaimPublisher step needs a node to work meaning it will fail if you are

          • using the declarative pipeline
          • using stage-based agents (setting agent none under the global pipeline declaration)

          you will need to put the step in a node closure

          pipeline {
            agent none
            stages {
              stage('Build') {
                agent any
                steps {
                  bat 'exit 1;'
                }
              }
            }
            post {
              always {
                // need the node closure here because agent none was set above
                node {
                  step([$class: 'ClaimPublisher'])
                }
              }
            }
          }
          

          Steve Boardwell added a comment - As a side note, the ClaimPublisher  step needs a node to work meaning it will fail if you are using the declarative pipeline using stage-based agents (setting agent none under the global pipeline declaration) you will need to put the step in a node closure pipeline { agent none stages { stage('Build') { agent any steps { bat 'exit 1;' } } } post { always { // need the node closure here because agent none was set above node { step([$class: 'ClaimPublisher']) } } } }

          Nate Tunes added a comment - - edited

          Claim plugin works perfect with scripted pipeline.

          All you need to do is add this step to the catch scope: step([$class: 'ClaimPublisher'])

          Use the folowing example:

           

          def jobs = ["1", "2", "3"]
          node {
              jobs.each {job ->
                  stage("${job}") {
                      try {
                          echo "${job}"
                          if ("${job}" == '2') { // illustrate a failure
                              throw -1
                          }
                          currentBuild.result = 'SUCCESS'
                      }
                      catch (exc) {
                          currentBuild.result = 'FAILURE'
                          echo "Job ${job} failed'"
                          step([$class: 'ClaimPublisher'])
                      }
                  }
              }
          }
          

           

           

          Nate Tunes added a comment - - edited Claim plugin works perfect with scripted pipeline. All you need to do is add this step to the catch scope: step( [$class: 'ClaimPublisher'] ) Use the folowing example:   def jobs = [ "1" , "2" , "3" ] node { jobs.each {job -> stage( "${job}" ) { try { echo "${job}" if ( "${job}" == '2' ) { // illustrate a failure throw -1 } currentBuild.result = 'SUCCESS' } catch (exc) { currentBuild.result = 'FAILURE' echo "Job ${job} failed'" step([$class: 'ClaimPublisher' ]) } } } }    

          Tor Selden added a comment -

          nathun Thanks for your example. It works like a charm. On more complex scripted pipelines it becomes very messy to have try/catch blocks for every stage. Also, some scripts may cause multiple exceptions causing the job page to have more than one 'claim it' link.

          Is there a way to add the Claim plugin for the overall job result (e.g 'FAILURE')

          My pipeline is outlined like this:

           

           

          timestamps {    
           timeout(time: 20, unit: 'HOURS') {        
             stage('') {            
               node('') {
                stage('') {}
                stage('') {}                
                stage('') {}                
                stage('') {}                
                stage('') {}
              }        
              stage('') {
                node('') {                
                 stage('') {}                
                 stage('') {}                
                 try { 
                   stage("") {}                
                 }
                 catch () {}  
                 finally {
                   stage("") {
                     try {}  
                     catch () {}  
                   }                    
                 }
               }
             } 
           } 
           stage('') {
             node('') {
               stage('') {}
               stage('') {}      
               stage('') {  
                 try {}  
                 finally {}
                  }
                }  
              } 
            }
          }
          

           

          Tor Selden added a comment - nathun Thanks for your example. It works like a charm. On more complex scripted pipelines it becomes very messy to have try/catch blocks for every stage. Also, some scripts may cause multiple exceptions causing the job page to have more than one 'claim it' link. Is there a way to add the Claim plugin for the overall job result (e.g 'FAILURE') My pipeline is outlined like this:     timestamps {    timeout(time: 20, unit: 'HOURS' ) {        stage('') {            node('') {       stage('') {}       stage('') {}                stage('') {}                stage('') {}                stage('') {}     }        stage('') { node('') {                stage('') {}                stage('') {}                try { stage("") {}                }   catch () {}  finally {       stage("") {         try {}  catch () {}           }                    }      }    }   }   stage('') {   node('') {     stage('') {}      stage('') {}          stage('') {  try {}  finally {}         }       }      }  } }  

            greybird Arnaud TAMAILLON
            ssbarnea Sorin Sbarnea
            Votes:
            17 Vote for this issue
            Watchers:
            23 Start watching this issue

              Created:
              Updated: