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

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.

Related

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.

JMeter & Jenkins - passing jmeter parameters to downstream build

The Setup - A jenkins job using jenkins parameters testApp and testEnv. The Execution Batch looks like this:
C:\jmeter\apache-jmeter-3.2\bin\jmeter.bat -n -t
C:\JMeter\Scripts\API_scripts\%testApp%.jmx -Jtestenv=%testEnv% -JtestApp=%testApp% -JtestBrowser=NA -l
C:\AUTO_Results\jtl\%testApp%_%testEnv%.jtl
Post-build Actions
Console output (build lob) parsing with a global rule so that the Failures that are logged in the Jenkins Console window will consider the JMeter script failing. (discussed Jenkins shows JMeter script failure even though the script actually passed)
Triggered parameterized build - this is a separate jmeter script that updates a wiki page with either PASS/FAIL and uploads the JMeter report.
The Issue - How do I get the downstream Triggered build to use the parameters from the upstream script? I set the Parameter = Current build parameters but it's not applying those. Also, I wont know the value of the testResult parameter until the upstream build finishes. I tried adding %testResult%=PASS to the 'Predefined parameters' box
As per Parameterized Trigger Plugin page:
The parameters section can contain a combination of one or more of the following:
a set of predefined properties
properties from a properties file read from the workspace of the triggering build
the parameters of the current build
Subversion revision: makes sure the triggered projects are built with the same revision(s) of the triggering build. You still have to make sure those projects are actually configured to checkout the right Subversion URLs.
Restrict matrix execution to a subset: allows you to specify the same combination filter expression as you use in the matrix project configuration and further restricts the subset of the downstream matrix builds to be run.
So you basically need to copy over the parameters you would like to have in the "downstream" job from the current one.
As a workaround to current performance plugin limitations you can consider running JMeter using Taurus tool as a wrapper, it has flexible and powerful pass/fail criteria subsystem which will basically return to Jenkins non-zero exit code triggering build failure in case of issue in the test. If everything goes well Taurus exit code will be 0 which is considered successful by Jenkins. Check out How to Run Taurus with the Jenkins Performance Plugin article for more details.

Jenkins-cli's set-build-parameter command not working anymore, how to modify the value of a build parameter across build steps?

In our Jenkins job, we need to sent an email containing a url in a post-build step (we also post the url to a Slack channel, but that's just about the same thing). The value of the url is dependent on the result of the build. Modification to an environment variable doesn't persist across build step. Writing the url to a file in one step and reading it in another is not an option either since the email/slack plugin has no ability of shell script execution. As a result, we've been using build parameter to solve this issue and the set-build-parameter command of jenkins-cli works fine.
After a recent upgrade of Jenkins (we're now at 2.75), set-build-parameter fails to work anymore. It reports the following error:
ERROR: Failed to identify the build being executed
According to Jenkins-cli's help command, set-build-parameter seems to be deprecated:
set-build-parameter
Update/set the build parameter of the current build in progress. [deprecated]
So, what's the correct way to modify the value of a build parameter across build steps in the latest version of Jenkins?
I frequently manipulate build parameters in shell script blocks, you have to edit them, save them in a file f. e.:
buildParamter=true>variable.txt
Now you can use the envInject Plugin and reference the the newly created file.

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

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

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.

Resources