Search code examples
jenkinscronjenkins-groovyjenkins-job-dsl

Add post build shell script to Groovy job executioner


I have a script that looks like this:

cronjobs.groovy :

job('MYJOB') {

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

  steps {
    shell('some shell script')
    shell('some othre shell')
  }
}

I want to add another shell to run here on failure of MYJOB execution. Is it possible? Or must I implement a pipeline? Can I implement this in this manner? And do I need any plugin for that?

job('MYJOB') {

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

  steps {
    shell('some shell script')
    shell('some other shell script')
  }

  post {
    failure {
      shell('some shell script')
      shell('some more shell script')
    }
}

Solution

  • Well according to job dsl docs, job have post-build action using publishers directive, but you can't specify failure condition and also you can't execute shell commands directly though you can invoke a groovy script.

    So it's better to go for a pipeline instead