82

I am trying to execute a shell script if either the build pass or fails after post-build in Jenkins. I cannot see this option in post build to execute some shell script except for running a target.

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
topgun
  • 2,253
  • 7
  • 28
  • 44

5 Answers5

68

Very easily done with Post build task plugin.

Jenkins - "Post build task" option

kenorb
  • 118,428
  • 63
  • 588
  • 624
Daniel Magnusson
  • 8,951
  • 2
  • 36
  • 43
  • 48
    I'd recommend the [PostBuildScript plugin](https://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin) instead for simply running a script. It's simpler and more robust if you don't need to conditionalize on the log output. For example, it allows running any build steps in the post-build actions. – Sampo Aug 01 '13 at 10:00
  • 9
    Also note that the Post Build Task plugin puts the entire console output into memory to do the conditional test... that can take up a lot of memory if have hundreds of jobs producing a lot of output... Our jenkins master just crashed due to this plugin... – grayaii Mar 13 '15 at 11:59
  • 1
    PostBuildScript plugin has been suspended. – Upen Aug 09 '17 at 05:03
  • @Upen can you provide a link proving that? – Vadim Kotov Feb 21 '18 at 13:16
10

You can also run arbitrary commands using the Groovy Post Build - and that will give you a lot of control over when they run and so forth. We use that to run a 'finger of blame' shell script in the case of failed or unstable builds.

if (manager.build.result.isWorseThan(hudson.model.Result.SUCCESS)) {
  item = hudson.model.Hudson.instance.getItem("PROJECTNAMEHERE")
  lastStableBuild = item.getLastStableBuild()
  lastStableDate = lastStableBuild.getTime()
  formattedLastStableDate = lastStableDate.format("MM/dd/yyyy h:mm:ss a")
  now = new Date()
  formattedNow = now.format("MM/dd/yyyy h:mm:ss a")
  command = ['/appframe/jenkins/appframework/fob.ksh', "${formattedLastStableDate}", "${formattedNow}"]
  manager.listener.logger.println "FOB Command: ${command}"
  manager.listener.logger.println command.execute().text
}

(Our command takes the last stable build date and the current time as parameters so it can go investigate who might have broken the build, but you could run whatever commands you like in a similar fashion)

kenorb
  • 118,428
  • 63
  • 588
  • 624
crovers
  • 329
  • 4
  • 12
6

If I'm reading your question right, you want to run a script in the post build actions part of the build.

I myself use PostBuildScript Plugin for running git clean -fxd after the build has archived artifacts and published test results. My Jenkins slaves have SSD disks, so I do not have the room keep generated files in the workspace.

kenorb
  • 118,428
  • 63
  • 588
  • 624
sti
  • 10,865
  • 1
  • 24
  • 25
3

You should be able to do that with the Batch Task plugin.

  1. Create a batch task in the project.
  2. Add a "Invoke batch tasks" post-build option selecting the same project.

An alternative can also be Post build task plugin.

kenorb
  • 118,428
  • 63
  • 588
  • 624
1

You'd have to set up the post-build shell script as a separate Jenkins job and trigger it as a post-build step. It looks like you will need to use the Parameterized Trigger Plugin as the standard "Build other projects" option only works if your triggering build is successful.

gareth_bowles
  • 19,908
  • 5
  • 52
  • 79
  • how can I trigger the script if the build fails exclusively based on either executable shell script I have under my job tasks? Thanks – topgun Jun 22 '12 at 17:46
  • Thinking about this, it might be easier to control in the build script itself. What build framework are you using ? – gareth_bowles Jun 22 '12 at 17:53