Jenkins - How to list build choices from successful builds of another project - jenkins

Assume I have two projects build and deploy.
I expect the build parameters of deploy to be a dropdown so that I can select which one to deploy.
All the dropdown items are build names of successful builds in build.
Actually I have found this through Groovy scripts before, but I cannot find them now. :(

This groovy script ,when added in an Extensible Choice parameter, can list out the successful builds from a given job.
def builds = []
def job = jenkins.model.Jenkins.instance.getItem(JOB-NAME)
job.builds.each {
def build = it
if (it.getResult().toString().equals("SUCCESS")) {
it.badgeActions.each {
builds.add(build.displayName[1..-1])
}
}
}
builds.unique();

Related

How to trigger Automation Pipeline after an Build Pipeline is successfully and only triggered by a specific user

Suppose Jenkins Build A is Successfully Triggered and after that Automation Pipeline on that Build is to be executed.
The above Scenario is possible using Jenkins Build Triggers using : Build after other projects are built
But in addition when we want to trigger Automation only if the build is generated by a specific used
For e.g.
User A, User B, Users C
So the Automation Pipeline must get generated only if the Build Pipeline is triggered by User A and User B.
The Automation Pipeline must not be triggered if the Build is generated by User C.
If I understand your requirement correctly, you have two Jobs, Build
and Automation. If Build Job is triggered by UserA, UserB.... you want to trigger the Automation Job. In order to achieve this, in your Build job, before triggering the Automation Job you can check who triggered the Build Job and decide whether to trigger the Automation job or not. Check the example below.
pipeline {
agent any
stages {
stage ('BuildJob') {
steps {
script {
def userList = ["admin2", "UserA", "UserB", "UserC"]
def buildTrigger = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
// Here checking username and triggering the automation build
if(buildTrigger != null && !buildTrigger.userId.disjoint(userList)) {
echo "Build the Second JOb========"
build job: 'AutomationJob'
}
}
}
}
}
}

How to get the successful build numbers of master branch of another job using groovy

i am new to groovy script. i have 2 jobs deploy_job and build_job. i want successful build numbers of master branch of build_job
I need to write a groovy script in deploy_job that gets the successful build numbers of master branch of build_job.I was not able to find the methods that i can use to get the master branch name with build numbers. The below script displays the build numbers of all the branches(master,feature,bugfix etc)
import hudson.model.*
def builds = []
def job = jenkins.model.Jenkins.instance.getItem("build_job")
job.builds.each {
def build = it
if (it.getResult().toString().equals("SUCCESS") ) {
it.badgeActions.each {
builds.add(build.displayName[1..-1])
}
}
}
builds.unique();
want to retrieve the build numbers of master branch only. Any help would be much appreciated

Mark Jenkins Pipeline as Promoted

Last I knew, Jenkins Pipelines did not support promotions, so to work around this, I created a job called "job-name-promotion" which would gather artifacts from the job I wanted to promote, and then mark the corresponding build as "Keep Forever." Is there a way to mark the build that was kept forever as "promoted" somehow? Preferably using one of the Stars that typically denote promotions? Or even better, is there a way to add Promotion Process steps to pipelines now?
Since it appears that pipelines still do not support promotions (as of 11/21/2017), I wrote a custom groovy script to iterate over all the jobs on the Jenkins server, locate the one we wish to promote and add a gold star to the corresponding build number:
import hudson.model.*
import jenkins.model.*
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildAction
def log = manager.listener.logger
build = Thread.currentThread().executable
String jobName = build.project.getName()
// note: these two variables are defined as parameters of the current job
def number = manager.build.buildVariables.get("NUMBER") as int
def buildJobName = manager.build.buildVariables.get("BUILD_JOB_NAME")
Jenkins jenkins = Jenkins.getInstance()
List<Job> projects = jenkins.getAllItems(Job.class)
for (Job project : projects) {
if (project.getName().equals("platform-lanai-pipeline")) {
log.println("Found it!")
Run usb = project.getBuildByNumber(number)
usb.getActions().add(GroovyPostbuildAction.createBadge('star-gold.png', ''))
}
}

Jenkins, how to check regressions against another job

When you set up a Jenkins job various test result plugins will show regressions if the latest build is worse than the previous one.
We have many jobs for many projects on our Jenkins and we wanted to avoid having a 'job per branch' set up. So currently we are using a parameterized build to build eg different development branches using a single job.
But that means when I build a new branch any regressions are measured against the previous build, which may be for a different branch. What I really want is to measure regressions in a feature branch against the latest build of the master branch.
I thought we should probably set up a separate 'master' build alongside the parameterized 'branches' build. But I still can't see how I would compare results between jobs. Is there any plugin that can help?
UPDATE
I have started experimenting in the Script Console to see if I could write a post-build script... I have managed to get the latest build of master branch in my parameterized job... I can't work out how to get to the test results from the build object though.
The data I need is available in JSON at
http://<jenkins server>/job/<job name>/<build number>/testReport/api/json?pretty=true
...if I could just get at this data structure it would be great!
I tried using JsonSlurper to load the json via HTTP but I get 403, I guess because my script has no auth session.
I guess I could load the xml test results from disk and parse them in my script, it just seems a bit stupid when Jenkins has already done this.
I eventually managed to achieve everything I wanted, using a Groovy script in the Groovy Postbuild Plugin
I did a lot of exploring using the script console http://<jenkins>/script and also the Jenkins API class docs are handy.
Everyone's use is going to be a bit different as you have to dig down into the build plugins to get the info you need, but here's some bits of my code which may help.
First get the build you want:
def getProject(projectName) {
// in a postbuild action use `manager.hudson`
// in the script web console use `Jenkins.instance`
def project = manager.hudson.getItemByFullName(projectName)
if (!project) {
throw new RuntimeException("Project not found: $projectName")
}
project
}
// CloudBees folder plugin is supported, you can use natural paths:
project = getProject('MyFolder/TestJob')
build = project.getLastCompletedBuild()
The main test results (jUnit etc) seem to be available directly on the build as:
result = build.getTestResultAction()
// eg
failedTestNames = result.getFailedTests().collect{ test ->
test.getFullName()
}
To get the more specialised results from eg Violations plugin or Cobertura code coverage you have to look for a specific build action.
// have a look what's available:
build.getActions()
You'll see a list of stuff like:
[hudson.plugins.git.GitTagAction#2b4b8a1c,
hudson.scm.SCMRevisionState$None#40d6dce2,
hudson.tasks.junit.TestResultAction#39c99826,
jenkins.plugins.show_build_parameters.ShowParametersBuildAction#4291d1a5]
These are instances, the part in front of the # sign is the class name so I used that to make this method for getting a specific action:
def final VIOLATIONS_ACTION = hudson.plugins.violations.ViolationsBuildAction
def final COVERAGE_ACTION = hudson.plugins.cobertura.CoberturaBuildAction
def getAction(build, actionCls) {
def action = build.getActions().findResult { act ->
actionCls.isInstance(act) ? act : null
}
if (!action) {
throw new RuntimeException("Action not found in ${build.getFullDisplayName()}: ${actionCls.getSimpleName()}")
}
action
}
violations = getAction(build, VIOLATIONS_ACTION)
// you have to explore a bit more to find what you're interested in:
pylint_count = violations?.getReport()?.getViolations()?."pylint"
coverage = getAction(build, COVERAGE_ACTION)?.getResults()
// if you println it looks like a map but it's really an Enum of Ratio objects
// convert to something nicer to work with:
coverage_map = coverage.collectEntries { key, val -> [key.name(), val.getPercentageFloat()] }
With these building blocks I was able to put together a post-build script which compared the results for two 'unrelated' build jobs, then using the Groovy Postbuild plugin's helper methods to set the build status.
Hope this helps someone else.

How to reset build number in jenkins?

I am using Jenkins and Gradle to build my java project.
Every time I build my project, I get a new build number on the Jenkins screen.
The following is my Jenkins build info:
Success > Console Output #96 03-Jan-2014 15:35:08
Success > Console Output #95 03-Jan-2014 15:27:29
Failed > Console Output #94 03-Jan-2014 15:26:16
Failed > Console Output #93 03-Jan-2014 15:25:01
Failed > Console Output #92 03-Jan-2014 15:23:50
Success > Console Output #91 03-Jan-2014 12:42:32
Success > Console Output #90 03-Jan-2014 12:02:45
I want to reset the Jenkins build number like:
Success > Console Output #1 03-Jan-2014 12:02:45
How can I reset the build number in Jenkins?
Can be easier done from groovy script console .
Go to http://your-jenkins-server/script
In script window enter:
item = Jenkins.instance.getItemByFullName("your-job-name-here")
//THIS WILL REMOVE ALL BUILD HISTORY
item.builds.each() { build ->
build.delete()
}
item.updateNextBuildNumber(1)
From here
Given your Hudson job is named FooBar,
rename FooBar to FooBar-Copy
create a new job named FooBar, using 'Copy existing job' option, from FooBar-Copy
delete FooBar-Copy
First wipeout workspace and get rid of previous builds.
On the server navigate to
the job dir eg. 'var/lib/jenkins/jobs/myJob' delete the
workspace & build dirs as well as any polling files, lastSuccessful,
lastStable files etc. You should only have config.xml and
lastBuildNumber.
Shut down jenkins using something like service jenkins stop
Edit the file called nextBuildNumber, inserting 1 instead of the current build number
Start up jenkins again, service jenkins start
Log into jenkins and go to your job and hit build. Should start building job#1
If you want set the next build number, there is plugin "NextBuildNumber" for that. But this will not work in your case because the build number you need, which is 1, is lesser than your current build number.
Here need to wipe out all the previous builds first. You can do this by running this simple script
Go to -> Manage Jenkins -> Script console
// change this variable to match the name of the job whose builds you want to delete
def jobName = "Your Job Name"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
Now you can set next build number to 1 and run the build. It will start with 1. :)
Its that simple.
Update - Jenkins now has a Purge Job History plugin to get this done in easiest way. Checkout the page for more details - https://wiki.jenkins.io/display/JENKINS/Purge+Job+History+Plugin
To more generally reset your build number to N (where N is not necessarily 1):
Delete any existing builds where buildNumber >= N.
Edit Program Files (x86)/Jenkins/jobs/yourjob/nextBuildNumber. Set the number it contains to N.
From Jenkins, select Manage Jenkins -> Reload Configuration from Disk.
Expanding on the accepted answer, here's how to do it for all projects at once:
Jenkins.instance.allItems.each() {
item -> item.builds.each() {
build -> build.delete()
}
item.updateNextBuildNumber(1)
}
As an extention of #antweiss's excellent answer, we can actually go further ...
There's no need to delete the full Build History, if you don't want to, you can simply roll back time, to a prior point:
resetNumberTarget = 14
item = Jenkins.instance.getItemByFullName("Project Name [from project Dashboard]")
//println(item)
item.builds.each() { build ->
//println(build)
//println(build.number)
if(build.number >= resetNumberTarget)
{
//println("About to Delete '" + build + "'")
build.delete()
}
}
item.updateNextBuildNumber(resetNumberTarget)
If you want a dummy run, to check what it's going to do, without actually doing it, simply comment out the build.delete() and item.updateNextBuildNumber(resetNumberTarget) lines and uncomment the various print commands.
Documentation:
Details of these objects were hard to find, but I identified the following:
item is a FreeStyleProject (or possibly, just any type of Abstract Project?)
build appears to be a Run, thus exposing a number property and inheritting a delete() method from Job
Use Purge Job History plugin (Jenkins >= 2.176.1)
https://plugins.jenkins.io/purge-job-history/
You can use either nexBuildNumber plug-in or simply modify nexBuildNumber file to reset build number.
Following are the steps that you need to perform:
Go to .jenkins/Jobs/<YourJobName>/build/, take backup of this folder(if you need for future use) and delete build folder.
Note: Once you clean up old builds, you lose build histories and they
are no longer available on the Jenkins dashboard.
Reload the configuration(Jenkins -> Manage Jenkins).
Set next build version to 1 by either using the Next Build Number plug-in or modifying the nextBuildNumber file in yourjob directory.
So I tried the above solution and getting the following error.,
groovy.lang.MissingPropertyException: No such property: builds for
class:
org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject.
So I tried this,
item = Jenkins.get().getItem("Job Name")
jobs = item.getAllJobs()
jobs.each() { item ->
builds = item.getBuilds()
builds.each() { b ->
b.delete()
}
item.updateNextBuildNumber(1)
}
and it worked!!
Here is variation of #antweiss answer for multi-branch pipeline
items = Jenkins.instance.getItemByFullName("your-job-name-here").getItems()
//THIS WILL REMOVE ALL BUILD HISTORY
items.collectMany { it.builds }.each { build ->
build.delete()
}
items.each {
it.updateNextBuildNumber(1)
}
To reset build numbers of all jobs:
Jenkins.instance.getAllItems(AbstractProject.class).each {
item = Jenkins.instance.getItemByFullName(it.fullName)
//THIS WILL REMOVE ALL BUILD HISTORY
item.builds.each() { build ->
build.delete()
}
item.updateNextBuildNumber(1)
}
I found an easy way to do this.
Wipe out your work space.
Go to each builds which saved on Jenkins and delete it.
Set build number to 1 then build.
Jenkins use previous build to determine the next build number, if build number you input is lower than previous build number, Jenkins will automatically increase your build number to higher than previous build. So here we just

Resources