Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the build status in post-build script

I would like to have a post-build hook or similar, so that I can have the same output as e. g. the IRC plugin, but give that to a script. I was able to get all the info, except for the actual build status. This just doesn't work, neither as a "Post-build script", "Post-build task", "Parameterized Trigger" aso.

It is possible with some very ugly workarounds, but I wanted to ask, in case someone has a nicer option ... short of writing my own plugin.

like image 357
smlgbl Avatar asked Jun 20 '12 18:06

smlgbl


People also ask

How you will check whether build is success or not?

Check to see if a build is running or not If a build is in progress, a grep for result\":null will return 0. If a build is finished, a grep for result\":null will return 1.


3 Answers

It works as mentioned with the Groovy Post-Build Plugin, yet without any extra quoting within the string that gets executed. So I had to put the actual functionality into a shell script, that does a call to curl, which in turn needs quoting for the POST parameters aso.

def result = manager.build.result
def build_number = manager.build.number
def env = manager.build.getEnvironment(manager.listener)
def build_url = env['BUILD_URL']
def build_branch = env['SVN_BRANCH']
def short_branch = ( build_branch =~ /branches\//).replaceFirst("")
def host = env['NODE_NAME']
def svn_rev = env['SVN_REVISION']
def job_name = manager.build.project.getName()


"/usr/local/bin/skypeStagingNotify.sh Deployed ${short_branch} on ${host} - ${result} - ${build_url}".execute()
like image 107
smlgbl Avatar answered Sep 18 '22 17:09

smlgbl


Use Groovy script in post-build step via Groovy Post-Build plugin. You can then access Jenkins internals via Jenkins Java API. The plugin provides the script with variable manager that can be used to access important parts of the API (see Usage section in the plugin documentation).

For example, here's how you can execute a simple external Python script on Windows and output its result (as well as the build result) to build console:

def command =  """cmd /c python -c "for i in range(1,5): print i" """
manager.listener.logger.println command.execute().text

def result = manager.build.result
manager.listener.logger.println "And the result is: ${result}"
like image 39
malenkiy_scot Avatar answered Sep 20 '22 17:09

malenkiy_scot


For this I really like the Conditional Build Step plugin. It's very flexible, and you can choose which actions to take based on build failure or success. For instance, here's a case where I use conditional build step to send a notification on build failure:

enter image description here

You can also use conditional build step to set an environment variable or write to a log file that you use in subsequent "execute shell" steps. So for instance, you might create a build with three steps: one step to compile code/run tests, another to set a STATUS="failed" environment variable, and then a third step which sends an email like The build finished with a status: ${STATUS}

like image 38
Matt Korostoff Avatar answered Sep 19 '22 17:09

Matt Korostoff