In jenkins how can I see last time build ran, by his xml file? - jenkins

I Have more than 1000 jobs in Jenkins,
And I would like to go through all of them in order to clean unused jobs.
What is the recommended way to do so?
I guess in every job "xml" file there is an indication to when it last ran,
Can anyone point me where this file is located?

I ended up filter the jobs by the "View job Filters" plugin,
You can use "Filter by Build Trend" option as follows:
Create a view for "All jobs" -> go to edit view -> in "add job filter " choose "Build Trend Filter" -> choose the filter you desire.
This is what I did:

I don't think you can do this in one step. But you can do this in 2 steps.
Find the URLs of all jobs with this:
https://jenkins-server/api/json?tree=jobs[url]
Get more info about each job by using the urls returned from step 1:
url-from-step1/api/json
This will give you the healthreport, last failed/successful build etc. If you need more info about these builds you can make a new request with :
url-from-step1/last-build-number/api/json
I recommend using JSON, and using JQ (http://stedolan.github.io/jq/, https://jqplay.org/) to parse your JSON
Happy coding!

You can leverage the REST API. The following urls might be relevant for you:
https://ci.jenkins-ci.org/api/xml?tree=jobs[name] -- to get a list of jobs
https://ci.jenkins-ci.org/job/{jobName}/lastBuild/buildTimestamp?format=yyyy-MM-dd-HH-mm-ss -- to get the time of last build of job {jobName}
Feel free to change xml to json/python...
I can provide a following shell script as a rough example:
#!/bin/bash
jenkinsUrlBase='https://ci.jenkins-ci.org'
callJenkins() {
curl --silent --show-error -g "$jenkinsUrlBase${1}"
}
callJenkins '/api/xml?tree=jobs[name]' | xmlstarlet sel -t -v '//hudson/job/name' | while read projectName ; do
timestamp=$(callJenkins "/job/${projectName}/lastBuild/buildTimestamp?format=yyyy-MM-dd-HH-mm-ss")
echo "Last build of ${projectName}: ${timestamp}"
done

You can exploit directory and file structure in ${JENKINS_HOME}:
cd ${JENKINS_HOME}/jobs/${JOB_NAME}/builds
ls -lt | head -2 | tail -1 | awk '{print $9}'
Example output:
2015-08-13_11-48-25

Related

klocwork in parallel: how append kwtables into existing build or how kwadmin publish results from mutiple kwtable folders

Due to our projects is really huge and combined all targets into single compilation + analysis + publish will take much time to finished. So I'd like to running the klocwork analysis in parallel.
Here what I've got right now (split the targets in various sub tasks):
kw-analysis -> kw-analysis-sub-1
| -> kw-analysis-sub-2
| -> ...
| -> kw-analysis-sub-n
| |
| + the sub-task will handle:
| 1. compile single target and generate spec: kwinject_<target_name>.out :
| $ export KWWRAP_HOOKS_DIR='/temp/kw/hooks'
| $ export PATH=${KWWRAP_HOOKS_DIR}:$PATH
| $ make <target_name>
| $ kwinject --trace-in "/temp/kw/kwwrap.trace" --output "kwinject_<target_name>.out"
| 2. trace and anslysis for each target:
| $ kwbuildproject --url "<https://url:port>/<project_name>" [-I] --table-directory kwtable_<target_name> kwinject_<target_name>.out
| 3. archive kwtable_<target_name> folder
|
+ leading job will do:
1. copy all kwtable_<target_name> from sub-analysis jobs (downstream jobs)
2. deploy and publish the result into klocwork sever once for all
<<<<<< this is the key point of parallel analysis
As I know to publish single kwtable can be:
$ kwadmin --url <https://url:port> load --name <build_name> <project_name> kwtable_<target_name>
However, seems kwadmin neither support multiple kwtabels via:
kwadmin load --name <build_name> ... kwtable_<target_name_1> kwtable_<target_name_2> ...
nor support add additional result in exists build via
$ kwadmin load --name <build_name> ... kwtable_<target_name_1>
|
+ create build first
$ kwadmin "append" --name <build_name> ... kwtable_<target_name_2>
*
+ append new result in <build_name> for anothers kwtables folder
So, is there any way I can run klocwork analysis in parallel. Btw I'm using the Jenkins as integration tool
Running the parallel analysis by breaking the project into multiple pieces may take more time than building it as a single project with Klocwork sometimes. the reason behind this is, Klocwork is going to analyze all the dependent files multiple times as you perform multiple builds/analyses which are actually a part of a single project. (Parallel analysis can be a benefit when you do not have dependencies on different modules/files/pieces that you are building in parallel).
Klocwork can perform incremental/delta analysis when you pass --incremental argument as part kwbuildproject command. This should save the build time.

Cannot get Description field of a Jenkins Job or Parameter

Is it possible to obtain the Job Description or the Job Parameter Description in run-time or later like the BUILD_ID or JOB_NAME?
I search for plugins or workarounds and nothing.
Thanks.
This would be Tip/workaround
https://<<yourjenkinsdomain>>/job/<<yourjobname>>/configure (will open the configuration of your job)
However
https://<<yourjenkinsdomain>>/job/<<yourjobname>>/config.xml (will give the job configuration in an xml format)
You can download this xml via curl at run time or using jenkins cli and use a grep with -B option to find description per value.
Considering you have copied the with name "config.xml"
cat config.xml | grep -B 1 "description"
Will give you description and build parameter name
Grep command
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
Sample output :
cat config.xml | grep -B 1 "description"
<actions/>
<description>Job description : Automation </description>
--
<name>branch</name>
<description>mandatory parameter , used for automation</description>
--
Alternative :
jenkins cli has an option to set value
set-build-description Sets the description of a build.
set-build-parameter Update/set the build parameter of the current build in progress. [deprecated]
you can write a small script and get the values into variables and use them

Jenkins API - launch a build and get build number in one atomic action

I want to launch a build using the Jenkins API and get the build number of the launched build.
However, because of synchronizations considerations, I don't want 2 separate calls (like: launchJobBuild(); getJobLatestBuildNumber()), but instead I am looking for an API call that gets in return the specific buildNumber that was just created (in the reply content for example)
Does such an API call exist?
This question has already been asked:
Retrieve id of remotely triggered jenkins job
If you're on v1.598 or above, the reponse will contain the build ID in the Location. Check out #morgwai's answer
You can get build no from response headers, it gives queueNo which is buildNo+1.
headerName = location
location →http://:/queue/item/54/
here 54 is queueItemNo.
BuildNo would be queueItemNo-1 = 53
You will get this response when triggering job.
http://:/job/<job_name>/buildWithParameters?repository=<repo_name>&branch=<branch_name>
For above url to work you would be requiring jenkins-crumb.
http://13.232.201.103:8080/crumbIssuer/api/json
use basic authentication with postman.
In reponse to this u will get
{
"_class": "hudson.security.csrf.DefaultCrumbIssuer",
"crumb": "61b6dd4325d000f8b76e9d830fcab88e12d38315e4a7a858c70b838cf9f07d20",
"crumbRequestField": "Jenkins-Crumb"
}
Use Jenkins Crumb as header in point 1.
Jenkins-Crumb:61b6dd4325d000f8b76e9d830fcab88e12d38315e4a7a858c70b838cf9f07d20
JobNo = response.getHeaders().get("location").get(0).split("/")[5]-1;
Shell script I developed to trigger a job, and get the build number :
JENKINS_EMAIL=<Email>
JENKINS_TOKEN=<API Key>
JENKINS_URL=<Jenkins Server URL>
JENKINS_JOB=<JOB>
# Trigger Job and get queue location
location=$(curl -X POST -s -I -u $JENKINS_EMAIL:$JENKINS_TOKEN "${JENKINS_URL}${JENKINS_JOB}/buildWithParameters?pass=ok" | grep location | awk '{ print $NF }')
location2=${location//[$'\t\r\n']}
# Wait till build number is generated
while true ; do
buildnumber=$(curl -X GET -s -u $JENKINS_EMAIL:$JENKINS_TOKEN "${location2}api/json" | jq '.executable.number')
if [[ $buildnumber != "null" ]]; then
echo "Build Started. Build number is : "$buildnumber"
break
else
echo "Still in Queue"
sleep 1
fi
done
This is how i trigger a job, and get the build number. The while loop exists because, the build number will not be generated immediately. First the build will be queued. We need to get that queue location from the first api response and then using it get the build number using that while loop.
Modify the parameters used in first API as you require

Jenkins REST API: get a build number of a job that just started

I'm starting the job by issuing this request
/POST /jenkins/job/jobName/build
but it returns nothing. I want to get the build number that just has started.
Although late , posting an answer so it can benefit any other seekers :
We can get the latest/current build no. using the following API :
<JENKINS_URI>/job/<JOB_NAME>/lastBuild/buildNumber
This will provide the current executing or the last successful build.
Example :
curl -X GET <JENKINS_URI>/<JOB_NAME>/lastBuild/buildNumber --user <user>:<key>
o/p : 20
This is how i do it, First trigger the Job with the rest api call. From this call's response, get the queue location. And using this queue location, fetch the build number. The build number will not be generated immediately, thats why the while loop.
JENKINS_EMAIL=<Email>
JENKINS_TOKEN=<API Key>
JENKINS_URL=<Jenkins Server URL>
JENKINS_JOB=<JOB>
# Trigger Job and get queue location
location=$(curl -X POST -s -I -u $JENKINS_EMAIL:$JENKINS_TOKEN "${JENKINS_URL}${JENKINS_JOB}/buildWithParameters?pass=ok" | grep location | awk '{ print $NF }')
location2=${location//[$'\t\r\n']}
# Wait till build number is generated
while true ; do
buildnumber=$(curl -X GET -s -u $JENKINS_EMAIL:$JENKINS_TOKEN "${location2}api/json" | jq '.executable.number')
if [[ $buildnumber != "null" ]]; then
echo "Build Started. Build number is : "$buildnumber"
break
else
echo "Still in Queue"
sleep 1
fi
done
Jenkins - How to access BUILD_NUMBER environment variable
use ${BUILD_NUMBER} to get the current build.
already extensive answers are available in stack overflow. kindly surf it before raising the question.

jenkins plugin for triggering build whenever any file changed in a given directory

I am looking for functionality where we have a directory with some files in it.
Whenever any one makes a change in any of the files in the directory, jenkins shoukd trigger a build.
Is there any plugin or mathod for this functionality. Please advise.
Thanks in advance.
I have not tried it myself, but The FSTrigger plugin seems to do what you want:
FSTrigger provides polling mechanisms to monitor a file system and
trigger a build if a file or a set of files have changed.
If you can monitor the directory with a script, you can trigger the build with a HTTP GET, for example with wget or curl:
wget -O- $JENKINS_URL/job/JOBNAME/build
Although slightly related.. it seems like this issue was about monitoring static files on system.. however there are many version control systems for just this purpose.
I answered this in another post if you're using git to track changes on the files themselves:
#!/bin/bash
set -e
job_name="whatever"
JOB_URL="http://myserver:8080/job/${job_name}/"
FILTER_PATH="path/to/folder/to/monitor"
python_func="import json, sys
obj = json.loads(sys.stdin.read())
ch_list = obj['changeSet']['items']
_list = [ j['affectedPaths'] for j in ch_list ]
for outer in _list:
for inner in outer:
print inner
"
_affected_files=`curl --silent ${JOB_URL}${BUILD_NUMBER}'/api/json' | python -c "$python_func"`
if [ -z "`echo \"$_affected_files\" | grep \"${FILTER_PATH}\"`" ]; then
echo "[INFO] no changes detected in ${FILTER_PATH}"
exit 0
else
echo "[INFO] changed files detected: "
for a_file in `echo "$_affected_files" | grep "${FILTER_PATH}"`; do
echo " $a_file"
done;
fi;
You can add the check directly to the top of the job's exec shell, and it will exit 0 if no changes detected.. Hence, you can always poll the top level of the repo for check-in's to trigger a build. And only complete a build if the files in question change.

Resources