Mani Vannan
Mani Vannan

Reputation: 394

Raise Abort in Jenkins Job from Batch script

I have a Jenkins job, which do Git syncs and build the source code.

I added and created a "Post build task" option.

In 'post build task', I am searching for keyword "TIMEOUT:" in console output (this part is done) and want to declare job as Failed and Aborted if keyword matches.

How can I raise / declare the Job as Aborted from batch script if keyword matches. Something like echo ABORT?

Upvotes: 2

Views: 3359

Answers (2)

Luc
Luc

Reputation: 1433

you can simply exit the flow and raise the error code that you want:

echo "Timeout detected!"
exit 1

Jenkins should detect the error and set-up the build as failed.

The error code must be between 1 and 255. You can chose whatever your want, just be aware that some code are reserved: http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF

You can also consider using the time-out plugin: https://wiki.jenkins.io/display/JENKINS/Build-timeout+Plugin

And another option is to build a query to BUILD ID URL/stop. Which is exactly what is done when you manually abort a build.

echo "Timeout detected!"
curl yourjenkins/job_name/11/stop

Upvotes: 1

Tim
Tim

Reputation: 2066

It is easier if you want mark it as "FAIL"

Just exit 1 will do that.

It is tricky to achieve "Abort" from post build task plugin, it is much easier to use Groovy post build plugin.

The groovy post build provide rich functions to help you.

Such as match function:

def matcher = manager.getLogMatcher(".*Total time: (.*)\$")
if(matcher?.matches()) {
    manager.addShortText(matcher.group(1), "grey", "white", "0px", "white")
}

Abort function:

  def executor = build.executor ?: build.oneOffExecutor;
  if (executor != null){
      executor.interrupt(Result.ABORTED)
  }

Br,

Tim

Upvotes: 1

Related Questions