Parameter List in Jenkins to display list of build numbers from another build - jenkins

I have two Jenkins builds, one for compiling and one for deploying.
The developer wants to be able to choose a build from the compiler build when running the deploy build, not always run the most recent build.
What I am after is a method of populating a choice parameter for the deploy build with a list of successful\unstable builds from the compile build.
I will then use the the option listed in the parameter to deploy that artifact.

Using the Dynamic Parameters plugin
In your promote job:
[x] This build is parameterized
Add Parameter
Dynamic Choice Parameter
Set Name to whatever
Paste below into Choices Script
import jenkins.model.Jenkins
import hudson.model.AbstractProject
import hudson.model.Result
import hudson.util.RunList
AbstractProject<?, ?> otherJob = Jenkins.getInstance().getItemByFullName("otherJobName", AbstractProject.class)
RunList<?> builds = otherJob.getBuilds().overThresholdOnly(Result.SUCCESS)
def list = builds.limit(5).collect { it.number }
Screenshot from wiki page:

As Dynamic Parameters plugin is no more accessible. You can use Active Choice parameter plugin in Jenkins.
Now you can list all successful Jenkins builds as parameterized option in Jenkins Job/pipeline
Follow below steps to access the list of successful jobs [as dropdown list]
In job configuration [General section] select this project is parameterized
Select add parameter as "Active choice parameter"
Give name for parameter
Select groovy script and paste below code in groovy script text box
return jenkins.model.Jenkins.instance.getJob('<Jenkins-job>').builds.findAll{ it.result == hudson.model.Result.SUCCESS }.collect{ "$it.number" }
It worked awesome without without powershell and BASH
No need to process Jenkins API and filter JSON output

One option is to use the Promoted Builds plugin to mark a specific build to be deployed. This moves the choice from the deployment build into the compilation build. Select the Promote builds when... option in the compilation build and set up how you want promotion to work. The developer could choose (or automate) the build to promote. In the deployment build, the Copy Artifact plugin can grab the appropriate build (based on a permalink to the latest promoted build).

As far as I know, it is not possible to populate the choice parameter. However, you don't need to always use the newest build. I assume that you use the copy artifact plugin. This plugin provides the "Build selector for Copy Artifact" parameter. You still need to enter the build number manually, but when deploying you have all the standard choices, like "Latest successful build", but also "Specific Build". You need to enter the number and don't have a drop down, but I got my deployers trained well enough to enter the build number.

Related

Jenkins: custom command for any finished build from history with access to this build`s ID, ideally from build history menu

User needs the ability to perform particular action in Jenkins UI on any of the job builds that are already finished with access to the build`s ID.
For each build in history there is menu with items "Changes", "Console output", "Delete build", "Rebuild" etc. Can I add a custom command into that menu using an existing plugin? The command will be "pass build ID to a script on Jenkins machine and launch the script" or at least "put build ID to a file" or "pass build ID to another job and launch it".
Is there any plugin that can do that?
The workarounds I am currently aware of are the following:
a separate Jenkins job that gets required build ID (needs launching another job, correct copying of the build ID)
rename build using menu item "Edit Build Information", then a script/cron job on Jenkins machine will do what it needs to (requires correct renaming)
putting a link to script into html report
artifact an html with the link to script
Looked at the plugins like "batch-task", "sidebar-link" etc., but all work on job level and not on build level.
P.S. It is a free-style Jenkins job, pipeline / Groovy has not been used.
Perhaps a Groovy script might help? How can it look like and how to make launching such script user-friendly (ideally with one button)?

How can I use the value from "Run Parameter" in "Copy artifacts from another project" for our Jenkins job?

I have a Jenkins job I want to use to run automation on a build created in another job. I want the user to specify that build job's build number using the drop-down box created by the Run Parameter plug-in and then have the automation job copy the artifacts from the user-specified build job.
These are the settings for Run Parameter:
Name: BUILD_SELECTOR
Project: Foo
Filter: All Builds
The user then selects a build like which is saved to BUILD_SELECTOR like "https://blah.com/job/Foo/4/"
Then later in my job, I have Copy artifacts from another project and I want to copy the artifact from the job selected above:
Project Name: Foo
Which build: ?
This is where I get stumped. I have tried the URL above as a permanent link and also tried stripping out the "4" above for Specific build, but nothing seems to work. How can I use that value to do what I need to do?
As stated in https://issues.jenkins-ci.org/browse/JENKINS-21519
You can use ${BUILD_SELECTOR_NUMBER} to get just the build number which you can use with specific build.

Parameterize the approver detail in Promoted Build Plugin in Jenkins

I am using Promote Build plugin in Jenkins .
I need to take the approver information from the user in Jenkins and provide him the approval rights .
Here is what I am trying to do :
Is it feasible ?
Don't think you can use variables there. However, you could skip that condition, and instead have an Execute Shell build step, and there check for variables $PROMOTED_USER_NAME. Parse the name, and make your decision based on that.
Parent parameters don't automatically get passed to Promoted builds. However, you can export them to file, archive the file (important to archive as opposed to keep it in workspace), bring the file over in the promotion step, and either load it to environment variables with EnvInject plugin, or simply use the file as is in a script
On Parent Job
Configure parameter approverid
Have an Execute Shell build step with the following:
echo approverid=$approverid > myfile
At the end, make sure to Archive myfile
On Promotion Configuration
Skip the approval criteria
Add Copy Artifacts from another project step
For Project Name, use $PROMOTED_JOB_NAME
For Which build, use Specific Build, then provide $PROMOTED_NUMBER
For Artifacts to copy, use myfile
Add Inject Environment Variables build step
For Properties File Path, enter myfile
Add Execute Shell build step
In that shell, compare values of $approverid and $PROMOTED_USER_NAME
If they match, continue, else abort/exit promotion.
Or course, the history of execution (and abort) will be noted however.

Triggering the same jenkins job after the build is finished in Jenkins

Is there a way to trigger the same job when the build is finished. I have one job that needed to be run until I aborted it manually. Is there a way to accomplish this?
The easiest way to do this is to add a post build step that builds the same project. Set "Post-build Actions" - "Build other projects" - "Projects to build" to the name of your project and it will loop forever.
This is a pretty crazy request. Are you sure that's what you want to do? If you just want to keep up to date you could just have the job build when the SCM system changes- even down to using the filesystem as an SCM.
If you really want to do it though, it is possible. You can't just tell it to trigger itself, but you can use the REST api.
Add a shell build step with the line
curl -X POST http://localhost:8080/job/Tester/build
and a new job will get scheduled each time you run build.
There is "Build after other projects are built" under "Build Triggers" in job configuration page in which you can specify which project to build after which project. So if you want to continuously run a particular job, you can add Project name i.e your job name in "Projects to watch" field under "Build after other projects are built" and can run it with options :- Trigger only if build is stable
Trigger even if the build is unstable
Trigger even if the build fails
you can try
add build step to create few files - file per condition set
add build step Trigger/call builds on other projects with add parameter factories with For every matching file, invoke one build
the called project might have input file as a parameter - it would be passed from the parent project from #2.

Changing Jenkins build number

Is there a way to change the build number that is sent via email after a job completes? The problem is that are product builds are NOT being done by Jenkins, so we want to be able to get the build number(ie. from a text file) and update the build number in Jenkins to match it. I have tried to set the build number:
set BUILD_NUMBER=45
But the email is still showing the build number that Jenkins originally set.
If you have access to the script console (Manage Jenkins -> Script Console), then you can do this following:
Jenkins.instance.getItemByFullName("YourJobName").updateNextBuildNumber(45)
can be done with the plugin:
https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin
more info:
http://www.alexlea.me/2010/10/howto-set-hudson-next-build-number.html
if you don't like the plugin:
If you want to change build number via nextBuildNumber file you should
"Reload Configuration from Disk" from "Manage Jenkins" page.
Under the job workspace folder, like:
C:\Program Files (x86)\Jenkins\jobs\job_name
there is a file named nextBuildNumber.
Setting the build number in the file and reloading the configuration from disk (Manage Jenkins menu) will force the next build you start to have the value from the file as BUILD_NUMBER.
If you have branch name including Forward Slash (using git flow for example), you will need to replace the Forward Slash with its Unicode character %2F within the branch name.
Here is an example for the pipeline My-Pipeline-Name and the branch release/my-release-branch-name
Jenkins.instance.getItemByFullName("My-Pipeline-Name/release%2Fmy-release-branch-name").updateNextBuildNumber(BUILD_NUMBER)
I was able to find out about this by running the following command which will list the different jobs (branches) for your pipeline
Jenkins.instance.getItem("My-Pipeline-Name").getAllJobs()
Hope it helps.
Perhaps a combination of these plugins may come in handy:
Parametrized build plugin - define some variable which holds your build number
Version number plugin - use the variable to change the build number
Build name setter plugin - use the variable to change the build number
You can change build number by updating file ${JENKINS_HOME}/jobs/job_name/nextBuildNumber on Jenkins server.
You can also install plugin Next Build Number plugin to change build number using CLI or UI
For multibranch pipeline projects, do this in the script console:
def project = Jenkins.instance.getItemByFullName("YourMultibranchPipelineProjectName")
project.getAllJobs().each{ item ->
if(item.name == 'jobName'){ // master, develop, feature/......
item.updateNextBuildNumber(#Number);
item.saveNextBuildNumber();
println('new build: ' + item.getNextBuildNumber())
}
}
Follow the steps: Jenkins Console > Manage Jenkins > Script Console, then write the script as:
Jenkins.instance.getItemByFullName("Your_job_name").updateNextBuildNumber(45)
By using environmental variables:
$BUILD_NUMBER =4

Resources