smlgbl
smlgbl

Reputation: 536

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.

Upvotes: 19

Views: 56347

Answers (7)

Karan Vyas
Karan Vyas

Reputation: 129

Groovy script solution:-

Here I am using groovy script plugin to take the build status and setting it to the environmental variable, so the environmental variable can be used in post-build scripts using post-build task plugin.

Groovy script:-

import hudson.EnvVars
import hudson.model.Environment

def build = Thread.currentThread().executable
def result = manager.build.result.toString()  
def vars = [BUILD_STATUS: result]

build.environments.add(0, Environment.create(new EnvVars(vars)))

Postscript:-

echo BUILD_STATUS="${BUILD_STATUS}"

Upvotes: 4

fearis
fearis

Reputation: 499

Basic solution (please don't laugh)

#!/bin/bash
STATUS='Not set'
if [ ! -z $UPSTREAM_BUILD_DIR ];then
  ISFAIL=$(ls -l /var/lib/jenkins/jobs/$UPSTREAM_BUILD_DIR/builds | grep "lastFailedBuild\|lastUnsuccessfulBuild" | grep $UPSTREAM_BUILD_NR)
  ISSUCCESS=$(ls -l /var/lib/jenkins/jobs/$UPSTREAM_BUILD_DIR/builds | grep "lastSuccessfulBuild\|lastStableBuild" | grep $UPSTREAM_BUILD_NR)
  if [ ! -z "$ISFAIL" ];then
     echo $ISFAIL
     STATUS='FAIL'
  elif [ ! -z "$ISSUCCESS" ]
  then
     STATUS='SUCCESS'
  fi
fi
echo $STATUS

where

$UPSTREAM_BUILD_DIR=$JOB_NAME

$UPSTREAM_BUILD_NR=$BUILD_NUMBER

passed from upstream build

Of course "/var/lib/jenkins/jobs/" depends of your jenkins installation

Upvotes: 0

ivoruJavaBoy
ivoruJavaBoy

Reputation: 1357

Really easy solution, maybe not to elegant, but it works!

1: Catch all the build result you want to catch (in this case SUCCESS).

2: Inject an env variable valued with the job status

Step 1

3: Do the Same for any kind of other status (in this case I catch from abort to unstable)

enter image description here

4: After you'll be able to use the value for whatever you wanna do.. in this case I'm passing it to an ANT script! (Or you can directly load it from ANT as Environment variable...)

enter image description here

Hope it can help!

Upvotes: 5

Matt Korostoff
Matt Korostoff

Reputation: 2132

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}

Upvotes: 5

ATOzTOA
ATOzTOA

Reputation: 35940

Try Post Build Task plugin...

It lets you specify conditions based on the log output...

Upvotes: 0

smlgbl
smlgbl

Reputation: 536

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()

Upvotes: 9

malenkiy_scot
malenkiy_scot

Reputation: 16605

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}"

Upvotes: 8

Related Questions