Changing Jenkins build number - jenkins

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

Related

Previous Build Number in Jenkins

I have a batch Job (not the pipeline Job) in Jenkins where I am using a plugin called "Naginator" which will check if the current build is failed/unstable - If the current build is failed/unstable it will run immediately the next build to confirm the error is genuine. If the error is not genuine then the run is successful and it will periodically set the next build to run. Along with this, I use some CMD commands to move my data into another folder with the Build number as my folder name.
Now here is the issue, if the previous build was unstable and Naginator runs the next build and that build is stable then what I need to do is delete the previous unstable build data from the folder manually. Is it possible to fetch the previous build number in Jenkins so that I can delete the file in an automated way - lets say in CMD Commands .BAT file.
Jenkins has it's own global variables. You can check whem in your pipeline job -> Pipeline Syntax -> Global Variables Reference.
Additionally check http://jenkins_url:port/env-vars.html/
For your purpose BUILD_NUMBER exist.
Just create new env var like this:
PREV_BUILD_NUMBER = $($BUILD_NUMBER -1)
Excuse me if this piece of code will not work, I'm not good about scripting) Just for example.
UPDATE:
also you can find in mentioned reference a list of variables:
previousBuild
previousBuildInProgress
previousBuiltBuild
previousCompletedBuild
previousFailedBuild
previousNotFailedBuild
previousSuccessfulBuild

Pass variables to jenkins from workspace

I am trying to set up builds on Jenkins. When the build is successful, I push a tag of the build using the Post build Action Git Publisher.
Until Now, these have been done manually, with the tag being given the version number, A.B.C.D (which comes from a text file). Now we are using Jenkins, D comes from the $BUILD_NUMBER Jenkins variable, but A.B.C are stored in a text file within the workspace. Is there a way I can pass A.B.C to the git publisher? or will have to do all of the git commands in a script?
Using Jenkins v1.624. Updating not currently possible (before people suggest this)
Not entirely sure if this will work the Post build Action Git Publisher (as I don't use it), and I can only test this on:
Jenkins ver. 2.32.3
EnvInject+Plugin 1.93.1
Groovy+plugin 1.30
Based on suggestion by 'Joerg S' in this post:
Creating a Jenkins environment variable using Groovy
Add a 'Execute Groovy Script' build step to read in the workspace file (tmpFile) containing A.B.C and convert it to a java based properties file - name:value) :
def custom_tag = new File('tmpfile').text.trim()
File propFile = new File('properties.text')
propFile.write "CUSTOM_TAG:"+custom_tag
Then add a 'Inject Environment Variables' build step to read in the new file, so Properties File Path is properties.text
You should then be able to use the ${CUSTOM_TAG} in your post build git publish as the TAG, as now it's an environment variable.
If this doesn't work check out the groovy code in the link above, it might offer something else.

Get build number of triggering project in Jenkins

I configured a Jenkins project B to run when project A completes succesfully.
How can I find the buildnumber of A in the project B pipeline?
If you just need the last successful build of A you can just read it from Jenkins:
http://JenkinsMaster:Port/job/MyJob/lastSuccessfulBuild/buildNumber
If you need the build the triggered B you can use the Parametized Trigger Plugin and use :
TRIGGERED_BUILD_NUMBER_MyJob="Last build number triggered"
You can do the following:
Use the Execute windows batch or Execute shell build step to store the build version in a file during the build of project A - e.g. from a windows batch:
echo "VARIABLEA=%BUILD_NUMBER%" > %WORKSPACE%\myartifact.properties:
Use the Archive the artifacts post build step to store the file against that build in project A
At the start of project B use the Copy artifacts from another project build step, point to project A and use the Artifacts to copy field to filter down to the file you created and choos Last successful build for the Which build field
Read the file in a shell script during the build of project A to pickup the build number
If you output the artifact in the format:
VARIABLEA=${BUILD_NUMBER}
VARIABLEB=${BUILD_NUMBER}
and you're using Linux on the Jenkins server, you could use the source command to make VARIABLEA and VARIABLEB available in that shell session, e.g.:
source "${WORKSPACE}/myartifact.properties"
echo ${VARIABLEA}
You could then do something with that variable in the shell script.
Alternately, you could simply use the Trigger parameterized build on other projects post build step (which I believe requires the Parameterized Trigger Plugin) on project A and setup project B to accept those parameters.

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.

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

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.

Resources