jenkinsgroovypublisherpost-build

Jenkins publishers postBuildScripts doesn't work


I have a groovy script to setup a scheduled job in Jenkins.
I want to execute some shell scripts on failed build.
If I had the scripts manually after the job creation after the job is updated by groovy script, they run.
But the groovy script does not add it:

job('TestingAnalysis') {

  triggers {
    cron('H 8 28 * *')
  }

  steps {
    shell('some jiberish to create error')
  }
  publishers {
    postBuildScripts {
        steps {
          shell('echo "fff"')
          shell('echo "FFDFDF"')
          }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(true)
    }
    retryBuild {
        rerunIfUnstable()
        retryLimit(3)
        fixedDelay(600)
    }
  }
}

Every thing works fine except:

    postBuildScripts {
        steps {
          shell('echo "fff"')
          shell('echo "FFDFDF"')
          }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(true)
    }

This is my result: enter image description here

I tried postBuildSteps and also got error.

I tried also with error:

    postBuildScripts {
        steps {
          sh' echo "ggg" '
          }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(true)
    }

Solution

  • Take a look at JENKINS-66189 seems like there is an issue with version 3.0 of the PostBuildScript in which the old syntax (that you are using) is no longer supported. In order to use the new version it in a Job Dsl script you will need to use Dynamic DSL syntax.

    Use the following link in your own Jenkins instance to see the correct usage:
    YOUR_JENKINS_URL/plugin/job-dsl/api-viewer/index.html#path/freeStyleJob-publishers-postBuildScript.
    it will help you build the correct command. In your case it will be:

    job('TestingAnalysis') {
        triggers {
            cron('H 8 28 * *')
        }
        steps {
            shell('some jiberish to create error')
        }
        publishers {
            postBuildScript {
                buildSteps {
                    postBuildStep {
                        stopOnFailure(false) // Mandatory setting
                        results(['FAILURE']) // Replaces onlyIfBuildFails(true)
                        buildSteps {
                            shell {
                                command('echo "fff"')
                            }
                            shell {
                                command('echo "FFDFDF"')
                            }
                        }
                    }
                }
                markBuildUnstable(false)  // Mandatory setting
            }
        }
    }
    

    Notice that instead of using functions like onlyIfBuildSucceeds and onlyIfBuildFails you now just pass a list of relevant build results to the results function. (SUCCESS,UNSTABLE,FAILURE,NOT_BUILT,ABORTED)