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

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.

Related

jenkins trying to copyArtifacts from a build that I trigger

I have installed the copyArtifacts plugin and created two freestyle jobs: experiment-main and experiment-1
experiment-1 just creates a file called artifact.txt with the build # in it, and archives it.
experiment-main triggers experiment-1 and then tries to copy the artifact like this:
but this is the result:
Running as SYSTEM
Building on master in workspace /var/lib/jenkins/workspace/experiment-main
Waiting for the completion of experiment-1
experiment-1 #4 started.
experiment-1 #4 completed. Result was SUCCESS
Build step 'Trigger/call builds on other projects' changed build result to SUCCESS
ERROR: Unable to find a build for artifact copy from: experiment-1
Finished: FAILURE
which isn't what I expected (or at least what I was hoping for)
I hoped it would find the experiment-1 build that was downstream from the current build.
Any ideas?
I figured out that there are variables with the numbers of triggered builds that I can use. To figure out the variable, I just printed all the environment variables with env and then found the right variable in the list.
Then I configured the copy artifacts plugin to use that build number.
I couldn't do it how #alex-o suggested, just getting the last build of the subjob, because I might have more than one job using the subjob at once, but if you don't have that problem, that might work for you.
Yes, this is unexpected behavior indeed.
The reason why this won't work is hidden in the help text of the "Upstream Project Name" input field:
Downstream builds are found using fingerprints of files. That is, a build that is triggered from a build isn't always considered downstream, but you need to fingerprint files used in builds to let Jenkins track them.
So, the Copy-Artifact plugin relies on fingerprint data to determine job ancestry. For that reason, you can not use the "Downstream build of..." feature using the current job as a parent: fingerprints are recorded in a post-build step, so an ongoing build of example-master does not have any fingerprints associated to it by the time it is looking for a matching build of experiment-1.
It is possible to modify fingerprint information at build run-time (e.g., via Groovy), but then, it's probably best to avoid the Copy-Artifact plugin entirely and to implement the whole procedure in Groovy right away.
Your best bet is probably to refer to example-1 via "Last successful build" and to ensure that this is the build that you triggered before (usually this will be correct, but depending on your setup there can be race conditions).

Previous Build Number in Jenkins

I have a batch Job (not the pipeline Job) in Jenkins where I am using a plugin called "Naginator" which will check if the current build is failed/unstable - If the current build is failed/unstable it will run immediately the next build to confirm the error is genuine. If the error is not genuine then the run is successful and it will periodically set the next build to run. Along with this, I use some CMD commands to move my data into another folder with the Build number as my folder name.
Now here is the issue, if the previous build was unstable and Naginator runs the next build and that build is stable then what I need to do is delete the previous unstable build data from the folder manually. Is it possible to fetch the previous build number in Jenkins so that I can delete the file in an automated way - lets say in CMD Commands .BAT file.
Jenkins has it's own global variables. You can check whem in your pipeline job -> Pipeline Syntax -> Global Variables Reference.
Additionally check http://jenkins_url:port/env-vars.html/
For your purpose BUILD_NUMBER exist.
Just create new env var like this:
PREV_BUILD_NUMBER = $($BUILD_NUMBER -1)
Excuse me if this piece of code will not work, I'm not good about scripting) Just for example.
UPDATE:
also you can find in mentioned reference a list of variables:
previousBuild
previousBuildInProgress
previousBuiltBuild
previousCompletedBuild
previousFailedBuild
previousNotFailedBuild
previousSuccessfulBuild

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 Promoted Build Parameter Value

I have a jenkins job that runs some tests and promotes the build if all tests pass.
I then have a second job that has a 'Promoted build parameter' which is used for deployments.
THe idea is that the deply job should let you pick one of the prompted builds for deploying. The issue I'm having is that I can pick a promoted build, but I have not idea how I access information about the build.
I've named the build parameter
SELECTED_BUILD
And according to the docs this should then be available via the environment.
The problem is that it doesn't seem to be bound to anything.
If I run a build step to exectute this sheel script:
echo $SELECTED_BUILD
echo ${SELECTED_BUILD}
The values are not interpolated / set.
Any idea how I can access the values of this param?
Many Thanks,
Vackar
Inject the information as variable user jenkins plugin (eject evn variable).
while triggering parameterized build on other job, pass above variable as parameter to next job.

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