Is there information available when executing jenkins postbuild script about job status - jenkins

I need to run certain shell command if build fails. Is there any variable passed to shell that contains such info? Or maybe I can find it in file?
I need something like that:
if [ $build_status == "FAIL" ]
then
do_the_magic
fi
I have already printed env and there is nothing that would directly say that build failed.

You are looking for the Conditional Build Step Plugin. Add a "Conditional step (single)" as your last build step and use "Current Build Status" for your condition. I like this plugin, but havent't used this particular condition yet.

Same solution explained step by step..Really easy, 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
3: Do the Same for any kind of other status (in this case I catch from abort to unstable)
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...)
Hope it can help!

Related

How to set a variable inside Jenkins Shell Command

I have a Jenkins build job. It has section for shell command where I read "version" of the current application that i am building.
Now, i want to set the Jenkins custom variable "VERSION" with the "version" in the same shell command section.
I need to pass this value as parameter to some other job being triggered after successful build of this job.
Now, I would pass the Jenkins variable VERSION to some other Job.
Please suggest how can I do this.
Use EnvInject Plugin to inject runtime variables into Jenkins build process so that you can use in other build steps or you can pass it on to other Job's as input parameter.
I could get some clue from Suresh's answer. I did the following steps and it worked well.
Step 1: Install Plugin “EnvInject Plugin”.
Step 2: Set the custom env variable and store that in env.properties
Step 3: Add POST Build Action, “Trigger parameterized build on other projects”
Step 4: Add the variables that the Post Build Job is expecting:
Here the BUILD_NUMBER and FILE_VERSION are two parameterized variables
that the next Job is expecting. FILE_VERSION is passed as parameters
from properties file which was stored in STEP 2.

How to use the output of shell script in jenkins

I am trying to execute the following shell command
timeStamp=$(date +%s)
echo "###### TIMESTAMP IS #$timeStamp #######"
I wanted to pass this variable timeStamp to my second job that I am trying to trigger using "Trigger parameterized build on other projects"
timeStamp=${timeStamp}
I am not getting the value of timestamp in the variable timeStamp.
I have restrictions that EnvInjectPlugin cannot be used. Is there any other option available to pass the shell script output to another parameter.
Thanks,
Ramya.
I suggest you use the EnvInjectPlugin plugin. But since you have restrictions that EnvInjectPlugin cannot be used, you can choose to use property file and here is how.
Step 1:
Step 2:
Add Post-build action ==> Trigger parameterized build on other projects ==> Add Parameters ==> Parameters from preperties file
After that, you can use TIMESTAMP parameter in the downstreamJob.

How can I use Groovy Postbuild to force a job to success?

I am trying to force a unit test job to success if the main build returned a failed exit code. Here is how I configured it and it is not working (see screenshot). Does anyone know any reason why this shouldn't work? In this example I just want to show that a failing job can be changed to a passing job by a groovy postbuild step. The plugin doc implies that this should work.
The main build runs a batch script with "EXIT 1" to fail the build.
A Groovy Postbuild step runs manager.buildSuccess() in an attempt to force the build to success (but it fails to do so with no error).
I found a discussion about that problem on jenkins-JIRA.
As a workaround it is proposed to use reflection to make the build successful:
manager.build.#result = hudson.model.Result.SUCCESS
This workaround doesn't work for me, but perhaps it can help you
Install Groovy Post Build Plug in.
Then add the below Groovy Script.
manager.build.#result = hudson.model.Result.SUCCESS
Uncheck the Use Groovy Sandbox. This worked for me.

How to pass parameters to Hudson job's shell commands

I have a Hudson job that execute shell script on a remote server.
Its shell command is:
/usr/bin/deployWar.sh ${warfileName}
I marked this build as parameterized, and added a string parameter:
name: warFileName
default value: none
description: name of war file
When I run it, the parameter gets assigned, but it get passed into the shell script.
Parameterized Build Jenkins plugin documentation states that
all the environment variables added by parameters are in upper case
In your case this should work:
/usr/bin/deployWar.sh ${WARFILENAME}
There is nothing wrong in your approach. How do you know it's not passed to the shell script? The console log will show the "execute shell" command line.
Please paste the whole console log in here
For me the accepted answer did not work. ${WARFILENAME} using the brackets did not find the parameter. The way it seems to work only for shell scripts execution in Jenkins is $WARFILENAME (without the brackets).
This tutorial helped.
Everywhere else in jenkins options used ${WARFILENAME}.

How to run a conditional step in Jenkins only when a previous step fails

I'm trying to create a 2 step job in Jenkins. I want the second step only to run if the first step fails. (The first step executes a unit test to see if the code I'm compiling is good - if it is not then i want to run some diagnostics in the second step).
The conditional step plug seems a good choice for this. However, I can't work out how to use the conditional step plug in to cause the second step to run when the first step fails.
The conditional step plug in offers a list of conditions such as 'Never', 'Boolean condition' ... and 'Current Build Status'. I would have thought I should use a 'Current Build Status' condition and set the worst status to 'failed' and the best status to 'unstable'. Then if the first step fails, the second step will run.
However there seems to be a problem with this. When the first step fails, then Jenkin's stops the job at the point. So the second step never has a chance to evaluate its condition and see whether or not it should run.
So I can't see how the use of a 'Current build status' of failed could ever be used in the conditional step plugin - as if the build has already failed, we never get to the Conditional Step. How can one mark the build status as failed in step 1 but not have Jenkins then stop the job at that point?
many thanks
Dave Sinclair
An inelegant way out will be to use the "Post Build task plugin".
It can parse the build log. So you can set it to check for failure logs statements. Once found you can launch a script. Maybe you can trigger second build via this script.
I am sure that there will be some other elegant options out there in post build tasks ecosystem. I am watching this question to see Jenkins experts to answer it.
You can create a script build step that always succeeds. In that script you could create one file when everything is right, another when something went wrong. You can check for the existence of the files in two separate conditional build steps. For instance:
REM Clean up flags
del build_failed
del build_ok
REM This should be the actual buildstep
REM Foobar does not exist, so it will return an error. dir will always work. Exchange the REM between the two lines to test both scenarios
dir foobar
REM dir
REM Set the flags based on the build step status
IF NOT %ERRORLEVEL% == 0 (
echo > build_failed
) ELSE (
echo > build_ok
)
REM Ensure that this step will always pass
EXIT 0
https://plugins.jenkins.io/postbuildscript/ should do the job. It allows to run a script if the build failed or even got aborted.

Resources