Remove Jenkins builds by name - jenkins

I have Jenkins job with parameters. Every build is named by number and name that is read from parameter (#1-build1, #2-example1, #3-build2, #4-example2). Is it possible to configure Jenkins to delete jobs by name and not by how old it is. In my example, if I issue new build named #5-example3, I want #2-example1 to be removed, and not #1-build1. Is there a plugin for removing builds by some filter?

Here is a Groovy Script that can do the job. But please replace the condition as per your logic.
Jenkins.instance.getItemByFullName('temp').builds.findAll { it.number.toString().contains '<your build name pattern to delete>' }.each { it.delete() }
Please add this as a build step in your job. This will simply do as you want. If need any help, please comment here. Hope this helps.

Related

How to get Jenkins build history in a Groovy script? Which information are available for every build?

I have a groovy script that runs as a shared library and get jenkins build details.
projects = Jenkins.instance.getJob('ABB').getItems()
for( build in projects.getAllJobs())
{
//process build data
build.getDuration();
build.getTime();
etc.
}
Can some one tell me how I can see all the get methods (all metadata related to build) that I can access by using the build variable? Is there any Javadoc for this? I am not able to find? As of now it access Duration and Time but I want to know what all info can I get.
Yes, Jenkins provides Javadoc: https://javadoc.jenkins.io/
build is an instance of the hudson.model.Run interface.
Of course the instance may provide more details because every type of job (FreeStyle, Pipeline etc.) may return an object with more details. For example new pipelines use org.jenkinsci.plugins.workflow.job.WorkflowRun.
The easiest option is to check the class and next search for its Javadoc :)
for (def build in projects.allJobs) {
echo "${build.class}"
}

Jenkins : Change the name of JenkinsFile

I'm using Pipeline Plugin under Jenkins
My job is basically using a file called "jenkinsFile" to get the divers steps to run.
-> My purpose is how to let the job use a different file name :
examples:
myJenkinsFile
build_JenkinsFile
deploy_JenkinsFile
buildSteps
...
Since it seems that "JenkinsFile" is a conventional format ,
is there any ways to change it if it's not verry clean ??
Suggestions ??
On the project section of the configuration page you just have to click Add > Pipeline Jenkins and then you can choose the custom name that jenkins will look for the pipeline.
If you want also a better level of customization you can also use Remote File Plugin, which allows you to put your pipeline in a repository and make it work with multiple repositories/branch (and of course you can still customize the name of the file)

Trigger Multibranch Job from another

I have a job in Jenkins and I need to trigger another one when it ends (if it ends right).
The second job is a multibranch, so I want to know if there's any way to, when triggering this job, pass the branch I want to. For example, if I start the first job in the branch develop, I need it to trigger the second one for the develop branch also.
Is there any way to achieve this?
Just think about the multibranch job being a folder containing the real jobs named after the available branches:
Using Pipeline Job
When using the pipeline build step you'll have to use something like:
build(job: 'JOB_NAME/BRANCH_NAME'). Of course you may use a variable to specify the branch name.
Using Freestyle Job
When triggering from a Freestyle job you most probably have to
Use the parameterized trigger plugin as the plain old downstream build plugin still has issues triggering pipeline jobs (at least the version we're using)
As job name use the same pattern as described above: JOB_NAME/BRANCH_NAME
Should be possible to use a job parameter to specify the branch name here. However I didn't give it a try, though.
Yes, you can call downstream job by adding post build step: Trigger/Call build on other projects(you may need to install "Parameterized Trigger Plugin"):
where in Parameters section you define vars for the downstream job associated with vars from current job.
Also multibranch_PARAM1 and *PARAM2 must be configured in the downstreamjob:
Sometimes you want to call one or more subordinate multibranch jobs and have them build all of their branches, not just one. A script can retrieve the branch names and build them.
Because the script calls the Jenkins API, it should be in a shared library to avoid sandbox restrictions. The script should clear non-serializable references before calling the build step.
Shared library script jenkins-lib/vars/mbhelper.groovy:
def callMultibranchJob(String name) {
def item = jenkins.model.Jenkins.get().getItemByFullName(name)
def jobNames = item.allJobs.collect {it.fullName}
item = null // CPS -- remove reference to non-serializable object
for (jobName in jobNames) {
build job: jobName
}
}
Pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
library 'jenkins-lib'
mbhelper.callMultibranchJob 'multibranch-job-one'
mbhelper.callMultibranchJob 'multibranch-job-two'
}
}
}
}
}

How do I get the name of the pipeline from inside the jenkinsfile

env.JOB_NAME Is the pipeline name suffixed with the branch name.
So env.JOB_NAME will be <jenkins_pipeline_name>_<my_branch>
How can I just get the pipeline name and store it in a var in the environment{} block at the top of my jenkinsfile to use through the file?
I don't want to resort to scripted pipeline just the declarative.
#red888 pointed out the following answer that worked like magic for me. I am pointing it out in an actual answer because I almost missed it:
env.JOB_BASE_NAME
Credit to #red888 in the comment above. Send upvotes his/her way.
Since you are using the multibranch job, the env variable is returning the actual job name that it created out of the branch .i.e. _ .. So, without some mechanism to strip/sed out the branch name, i don't think there is an env variable for it in jenkins out-of-the-box.
You can check http://YOUR-JENKINS-SERVER.com/pipeline-syntax/globals#env and it'll get you
JOB_NAME
Name of the project of this build, such as "foo" or "foo/bar".
JOB_BASE_NAME
Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".
For a jenkins organization project with multiple branches and PRs, the JOB_NAME is actually YOUR_ORG_NAME/YOUR_REPO_NAME/YOUR_BRANCH_OR_PR_NAME, e.g. my-org/my-repo/PR-23, and the JOB_BASE_NAME is just PR-23. Just as Israel said, you can use env.JOB_NAME.tokenize('/') as String[] to get what you want.
If you want more fancy scripts in Jenkins, Groovy can be a good reference.
In jenkins pipeline
def allJob = env.JOB_NAME.tokenize('/') as String[];
def projectName = allJob[0];
env.JOB_NAME is the project name... There is no DOUBT
However, my issue with copyArtifact plugin. It said :
Unable to find project for artifact copy: YOUR_PROJECT_WITH_ARTIFACTS
This may be due to incorrect project name or permission settings;
At the end it is not "incorrect project name", and env.JOB_NAME must work.
However, the issue is permission settings which can be fixed generally by :
pipeline {
agent any
options {
copyArtifactPermission('*');
}
//..
}
Reference

1 jenkins job trigger multiple jenkins jobs based on parameters

Is there any Jenkins plugin that helps with the following:
if a directory <XXX*, is present in SVN folder <GoRoCo>
then the <GoRoCo>_<XXX> Jenkins job is called
?
Example:
In job "TEST" , I specify parameters like directory name (A, B , C) and folder name (G1R2) then job "TEST" should trigger the jobs "G1R2_A" , "G1R2_B" and "G1R2_C"
Use Parameterized Trigger Plugin. When specifying jobs to call in the plugin you can use tokens, as in JOB_${PARAM1}_${PARAM2}.
Take a look at that plugin, i think it does exactly what you are looking for:
https://wiki.jenkins-ci.org/display/JENKINS/Files+Found+Trigger
Use Build Flow plugin
With the help this plugin you can run as many jobs with or without parameter.
Use some scripts to create property file with the required parameters for each of the modified project and place them in the workspace directory.
Later you can use parameterised plugin to trigger downstream project like this.
Note: you might also have to delete those properties after triggering the down stream projects.

Resources