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

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.

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.

how can I use a jenkins job to loop another jenkins job

I have a requirement where I need 2 Jenkins job, One job to take user parameters (eg: user chooses number 10 which means I need to run second job 10 times)
second job will be the actual execution of test automation.
The loop will continue irrespective of the result (pass/fail) of previous iteration
just trying to represent this as a Snippet,
Job A :
User input = 10
for(num<=10){
Job B(num)
}
Job B :
execute(num)
P.S: If there is a solution where I can achieve this in a single job, Please suggest
You can use Jenkins upstream/downstream concept, please refer below url:-
Jenkins Upstream/Downstream Linking
You can do it:
You can use the same solution for Linux and Windows machine (in windows write cmd syntax and in Linux Bash)
You create a loop for example in Bash:
!/bin/bash
for i in {1..5}
do
curl
done
If you do not know CURL please use the link below.
https://serverfault.com/questions/888176/how-to-trigger-jenkins-job-via-curl-command-remotely

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.

Jenkins - execute a script before building, then have the user confirm to build

I have a bash script that looks like this:
#!/bin/sh
previousRelease=`git describe --tags --match "release*" origin/release`
git diff --name-status $previousRelease..origin/release
Is there a way of having Jenkins execute it as part of a build process? The intention is to see a list of files that have changed since the last release, as a manual step to confirm that the release should go up. The user who has triggered the build needs to read the output and then confirm the release should go ahead.
Most things are possible to do in Jenkins but if it is the best way of doing it is another question.
To solve this I would use an approach with two jobs one for checking the diff (hock that one on to the git repository) The other job for doing the actual release.
The check diff job
1 Create a job of the type freestyle project with build type "execute shell" and run your script above. Add some prints at the end of the log to create a clickable link to manually start the release job with current git-id as argument.
Just printing an URL in console output will make it clickable so:
export GITID=`git log -n| grep and sed or awk something`
echo http://jenkins.example.com:8888/job/releaseme/buildWithParameters?label=$GITID&parameters=build
will create the accept changes user interface you requested.
The release job
2 Create another job(above I assumed you named it releaseme) let the job have one parameter as argument (tick "This build is parameterized") make let the argument be the git-id you would like to release. Create your release script in this job.

Resources