Is it possible to access Jenkins build status in execute shell? - jenkins

Is there a way to have a script being run from an execute shell step access the build status as well as other information about the build? (e.g. build number, start/end time, etc)
I need to update a wiki page with a script with the information about the build.
Thanks in advance!

Build status information gets saved into simple XML files. Take a look at
{jenkins-directory}/jobs/{job-name}/builds/{build-number}/build.xml. You can use symbolic links like lastSuccessfulBuild instead of specifying a build number if you want.
You'll find all information about the build there: build status, start time, duration, etc. Parse the XML file or just use grep in a bash script.
If you don't want to use the filesystem you can use Jenkins API.
But in any case, once you got a script that can determine build parameters and update your wiki page, you can put it into another Jenkins job and trigger it automatically (for example, with the BuildResultTrigger Plugin).

I'm using jenkins own API to get the build status while the job is running, which works like a charm. Be aware that i'm using JQ To parse the json response.
To get this to work simply add a shell script and execute the following command: BUILD_STATUS=$(curl --silent ${BUILD_URL}api/json | jq -r '.result')
Which results in the following:

While executing a build, Jenkins set environment variables you can use in your script.
also refer to the detailes on how to use it -
%VAR% in batch files and $VAR from inside the Jenkins job configuration page

Related

Jenkins - How to access Build number variable and use it as a Prefix/Suffix of a log name during Post build actions

I am trying to use Jenkins' Build Number in the naming of a Log that I would want to be saved as a post build action
Will the below format work
C:\Jenkins\workspace\Jmeter_Jenkins_Test_Job\Jenkins_Results\"${env.BUILD_NUMBER}"results.jtl
As per Building a software project wiki article the environment variable you're looking for is BUILD_NUMBER and in case of Windows operating system you can access it as:
%BUILD_NUMBER%
so if you want to amend JMeter result file name to include build number you can do something like:
jmeter -n -t /path/to/test.jmx -l /path/to/result-%BUILD_NUMBER%-.jtl
and in the runtime the variable will be evaluated to the current Jenkins build number:
More information just in case: Continuous Integration 101: How to Run JMeter With Jenkins
Without having laid my hands on a Jenkins installation for quite some time:
Yes, you can do that and it has been done before!
You could do something like:
pipeline {
agent any
stages {
stage('test') {
sh 'path/to/jmeter.bat -n -t ${env.WORKSPACE}my_test.jmx -l my_test${env.BUILD_ID}_${env.BUILD_NUMBER}.jtl'
}
}
}
I would propose to create the HTML dashboard report first though and then publish that in Jenkins - you could use https://jenkins.io/doc/pipeline/steps/htmlpublisher/ to do that. Further you should avoid absolute paths in favor of using the WORKSPACE environment variable (see https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables for reference).
If you need some general idea of how to run the test via Jenkins you could have a look at https://code-maven.com/jenkins-pipeline-running-external-programs and https://jmeter.apache.org/usermanual/get-started.html#non_gui
If you already tried to achieve something and need more specific help, please come forward with some more detail.

How to view Jenkins console output during a build from the terminal after invoking build using curl from the terminal?

I have built a Jenkins job to run automated ZAP-Proxy scans.
I used curl -X POST -u YOUR_USER:YOUR_USER_PASSWORD http://YOUR_JENKINS_URL/job/YOUR_JOB to build the job from the terminal. Is there a way to display the console output in the terminal while the job is building?
It is possible, but it's a little more complicated for two reasons:
When you trigger a new build via curl, then the build will not start immediately. The build will enter the build queue, and it will only start executing once Jenkins found a suitable executor. Before that time, there is no build URL at all.
Technically, the (continuous) console output is delivered in multiple fragments that must be retrieved individually via HTTP.
So, once you triggered the build, you need to find its URL after if left the build queue. It can be done nicely in Groovy -- as a simpler heuristic, you could just wait for certain time and then use the lastBuild reference.
For fetching the console log fragments, you'll use the <buildUrl>/logText/progressiveText end point. Create a loop fetching that URL and checking the X_More_Data and X_Text_Size HTTP headers for information on whether (and what) console output is available. You can do that in bash with curl in a loop; I found this Groovy example on the Web.
In the end, the most elegant solution is probably to
trigger a new build by submitting a Groovy script that will trigger the build and then waits for the build to leave the build queue, returning the build URL.
Then use another script that will poll/fetch/display that build's console output.
If you use the CLI interface for submitting the script, then you can do all those steps in a single script. If you use the REST API, then second part ("continuous output") probably won't work due to output buffering on REST API side.

is there any way to get jenkins build number using jenkins-cli

I have builds numbered 1, 2, 3 & 4 for my job named TEST_JOB. With jenkins-cli.jar, am planning to execute a command where I can get build numbers of my TEST_JOB.
Have tried using cli commands such as console (which prints the outputs console of the last build/ current build) & build (which runs the job) but neither of which returns a build number.
Am planning to do this via command prompt. Any idea as to how should I proceed?
Why using Jenkins-cli jar? I mean you can do it using that but I think the better option is to access job/build metadata using Jenkins API.
How to get started with Jenkins API? you can see job metadata in JSON/XML format in the following URL:
JENKINS_URL/job/JOB_NAME/api/json
JENKINS_URL/job/JOB_NAME/api/xml
In the job level, you can see there details as: parameters, builds list and more
In the build level, you can see there details as: who started the build, parameters, duration, result
And then from code perspective, you can access JSON/XML using curl command or wrap it with python code.

Jenkins - BuildWith parameters

Currently I have a python script which reads and returns the build version from the Build Server.
But I couldn't find an easy mechanism of passing the version value as an input value when jobs are run with "Build With Parameter" option.
The Current job is supposed to run on a scheduled manner. So it should automatically run the script, pick the version, auto populate and trigger.
I have done this by using the Jenkins REST API with buildwithparameters option, but I would like to know if this can be done without the REST.
Modify your python script to create text file containing version number(instead of returning). Then in build script, read this file to get version info.

Script to build a set of jobs, wait until they finish and write a report about them

I'm planning to write a script that would choose a set of Jenkins jobs with a regexp, start their builds and wait until they finish. If most of them fail, I want to change some global env vars (I already know how to do this) and build them again. Then I want to collect the test results and format it into a nice report.
I can't put this into the individual jobs' post-build actions, I need info about all of them to write the report and to do the rebuilding.
My current idea is to use the Jenkins REST API, but before I do so: is this already implemented somewhere?
Give a look at Multi-Job plugin and see if it fits your requirement.
Else go with REST API.
Because unique requirement of our build process I had to use jenkins cli :
java -jar jenkins-cli.jar -s http://<jenkinsURL> build <job-name> -s <parameters if any>
The trailing -s makes it wait till the job is complete.

Resources