Add automatic counter field to Jenkins - jenkins

I have field containing sprint number.
I want to add to Jenkins additional field that will to grow one for each run of the job.
Is it possible?
Thanks.

BUILD_NUMBER is the right parameter which automatically increases with each build. If you want to reset the build number, you can do so by running the following script in the jenkins master groovy script console
jenkins = hudson.model.Hudson.instance
job = jenkins.getJob("JOBNAME")
println job.getNextBuildNumber()
job.updateNextBuildNumber(0)
println job.getNextBuildNumber()
This will set the build number to 0. You can then on rely on BUILD_NUMBER. If your requirement is only for a parameter that increases by 1 for each build, use BUILD_NUMBER

See the Jenkins documentation. The BUILD_NUMBER environment variable does this and is provided by Jenkins to your job.

Related

Seed Job environment variable ${WORKSPACE} is assigned to child jobs

Create a Seed Job using DSL that would create 5 Child Jobs. As part of build step I've to use ${WORKSPACE} environment variable, which should read the value from the slave machine where the child Job is running. But however when the child jobs are created, the workspace value is replaced with SeedJobs workspace location from master server.
How would I restrict seed job not to inject its ${WORKSPACE} value.
Could some one help me on this please.
Thanks
Mano
To refer to the BUILD_NUMBER of the generated job in your job-dsl code, you should code it as follows :
"\${BUILD_NUMBER}"
The important part is the \ before the $ sign which prevents the evaluation of the BUILD_NUMBER variable as it pertains to the Goovy runtime that is (likely) your Jenkins job-dsl seed job.
The config.xml of the generated job will contain ${BUILD_NUMBER} instead of a digit that represents you build number of your seed-job. And then your generated job will evaluate that when it runs.

How can I create parameterized Jenkins job?

I want to use same job in different machine. But I don't want to change the configuration of the job each time. Can I pass the machine name label as parameter and run the job in different machine ? (Not simultaneously).
I want to pass parameters while running a job to the script which I have written in th configuration (batch script). Can we do that ?
Can I get a return value from a job and use it in next job?
Example criteria:
User needs to use different environments for executions.
1. Select jenkins project
2. Job -> Configure -> Select this project is parameterized check box.
3. Select Add parameter drop down - > String parameter
4. Define a string parameter (In my example I use environment variable as : BaseURL)
5.Now under build section initialize the mvn command user needs to execute.Here in this case I want to execute my jenkins build on a environment where I specify. So I should be able to execute my build in different environment each time with out changing the code.
My mvn command: I pass my environment url as a parameter (Using $ sign). This is based on user requirement.
clean test -Dcustomproperty="$BaseURL"
6. Apply and save your Jenkins project.
7. Now your project is parameterized. Then your project should have Build with parameters option.
8.Click Build with parameters link and enter your parameter (In this example my environment variable) and click build. Your jenkins job should run with the parameter.
NOTE: This build won't succeed unless the passing parameter is not utilized in the automation script. So script has to be modified to retrieve the passing variable.
String BaseURL= System.getProperty("customproperty");
Yes, you can pass a node label parameter with NodeLabel Parameter Plugin.
Yes, you can define parameters, as described, in Parameterized Builds and then use it in your script as an environment variable:
The parameter is available as environment parameters. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
This is not exactly a return value, but you can pass any parameter (with its value) to the downstream job with Parameterized Trigger Plugin.
Use parameterized Build plugin and NodeLabel Parameter Plugin

How to synchronize subproject build number

Jenkins JobA triggers JobB as a subproject.
Is there any way that I can force JobB to have the same build number as JobA?
I am currently passing an environment variable into JobB so that JobB can use the correct number during its build. But it's still confusing having JobA and JobB have different build numbers.
I am also using the "Next Build Number Plugin", but JobA and JobB's build numbers keep drifting apart over time as JobA fails before it calls JobB, forcing me to come back and manually fix it.
You should consider Build Name Setter Plugin, as it allows setting a BUILD_NUMBER through a variable. Configure it to use something like $parentBuildNumber
Now, in JobB configure a text/string parameter, parentBuildNumber.
When you trigger JobB from JobA, you should be using Parameterized Trigger Plugin.
There configure a predefined parameter set as parentBuildNumber=$BUILD_NUMBER
In the parent job get the next build number, substract by 1 if needed and pass it to child job:
cat $JENKINS_HOME/jobs/$JOB_NAME/nextBuildNumber
And output it to
$JENKINS_HOME/jobs/$CHILD_JOB_NAME/nextBuildNumber
You may or may not have to install the NextBuildNumber plugin:
Also, there is a CLI command set-next-build-number. Try executing jenkins CLI help.

Jenkins Promoted Build Parameter Value

I have a jenkins job that runs some tests and promotes the build if all tests pass.
I then have a second job that has a 'Promoted build parameter' which is used for deployments.
THe idea is that the deply job should let you pick one of the prompted builds for deploying. The issue I'm having is that I can pick a promoted build, but I have not idea how I access information about the build.
I've named the build parameter
SELECTED_BUILD
And according to the docs this should then be available via the environment.
The problem is that it doesn't seem to be bound to anything.
If I run a build step to exectute this sheel script:
echo $SELECTED_BUILD
echo ${SELECTED_BUILD}
The values are not interpolated / set.
Any idea how I can access the values of this param?
Many Thanks,
Vackar
Inject the information as variable user jenkins plugin (eject evn variable).
while triggering parameterized build on other job, pass above variable as parameter to next job.

Downstream Jenkins project gets wrong upstream run parameter

I'm having a problem with a Jenkins build pipeline. All jobs after the first one are parameterized with the "Run Parameter" of the first job. By default, this should reference the most recent stable build of the first job. Each subsequent job uses the "Run Parameter" of the first job to access saved artifacts from the first job. Each subsequent job triggers the next job of the pipeline as a parameterized build and passes the aforementioned "Run Parameter". The first job of the pipeline triggers the second job as a simple (i.e., not parameterized) build.
Here's a screenshot of the relevant configuration of a downstream job:
My problem is that the job number in the "Run Parameter" isn't the job number of the first job of the pipeline. Instead, it's the job number of the first job of the previous pipeline. Thus, if the first job is on build #11, then all subsequent job of that pipeline will access the archive of build #10 of the first job.
How can I get the subsequent jobs of the pipeline to access the archive directory of the first job of the pipeline?
I discovered the answer. Apparently, the reason the downstream job was using the artifacts from the upstream job of the previous pipeline was because I had set the "Run Parameter" filter in the configuration of the downstream job to "Stable Builds Only". Setting this filter to "All Builds" results in correct behavior.
It's as if Jenkins doesn't consider an upstream job to be stable when it's starting another build in its post-build section.
Quote: "By default, this should reference the most recent stable build of the first job."
Do you mean the last successful build of the Top job. Since in that case there might be a case where the last successful build of the top job was #7 and current build is #11. So you want the downstream jobs to look for #7 and not #10.
If that is the case then I will suggest putting a groovy build step. Install the groovy plugin for that. But before that test the script.
Open: YourJenkinsServerURL/script
Run this script.
def tmp = hudson.model.Hudson.instance
def myJobName="YourTopJobName";
tmp.getItems(hudson.model.Job).each {job ->
if(job.displayName == myJobName)
{
println(job.getLastSuccessfulBuild())
}
}
In groovy you can access and set an environment variable (injected via envInject plugin maybe) to this last successful build number and then pass on this variable to downstream job.
If that is not the case then I will suggest use Nant Script.
Use "int::parse()" to convert the string format build number to integer. Decrement the value and then pass on the value to the downstream job.

Resources