在 Jenkins 中后期构建后执行 Shell 脚本

我正在尝试执行一个 shell 脚本,如果构建通过或失败后,在 Jenkins 后构建。除了运行目标之外,我在后期构建中看不到这个选项来执行某些 shell 脚本。

147573 次浏览

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.

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.

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.

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)

Very easily done with Post build task plugin.

Jenkins - "Post build task" option