How to set build version info via pipeline? - jenkins

In my build job on Jenkins via pipeline, I need show build description in build history list. how to set it in pipeline?

According to this support article, you can update the display name of a given build by running currentBuild.displayName = "7.6.2" (or whatever your version is) within a script block.
So with the use of the version number plugin, you can do something like this:
BUILD_VERSION_GENERATED = VersionNumber(
versionNumberString: '7.6.2.${BUILDS_ALL_TIME, X}',
projectStartDate: '1970-01-01',
skipFailedBuilds: true)
currentBuild.displayName = BUILD_VERSION_GENERATED
Wrap that in a script {} block in the steps section of a stage if you're using declarative pipeline and you're good to go.

Execute the script Console ($JENKINS_URL/script):
def job = Jenkins.instance.getItemByFullName("folderName/my-pipeline-job")
job.nextBuildNumber = 10
job.save()
Replace folderName/my-pipeline-job with your folder/job name with the desired next build number.

Related

How to set build name in Jenkins Job DSL?

According to the documentation in https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.wrapper.MavenWrapperContext.buildName
Following code should update build name in Build History in Jenkins jobs:
// define the build name based on the build number and an environment variable
job('example') {
wrappers {
buildName('#${BUILD_NUMBER} on ${ENV,var="BRANCH"}')
}
}
Unfortunately, it is not doing it.
Is there any way to change build name from Jenkins Job DSL script?
I know I can change it from Jenkins Pipeline Script but it is not needed for me in this particular job. All I use in the job is steps.
steps {
shell("docker cp ...")
shell("git clone ...")
...
}
I would like to emphasise I am looking for a native Jenkins Job DSL solution and not a Jenkins Pipeline Script one or any other hacky way like manipulation of environment variables.
I have managed to solve my issue today.
The script did not work because it requires build-name-setter plugin installed in Jenkins. After I have installed it works perfectly.
Unfortunately, by default jobdsl processor does not inform about missing plugins. The parameter enabling that is described here https://issues.jenkins-ci.org/browse/JENKINS-37417
Here's a minimal pipeline changing the build's display name and description. IMHO this is pretty straight forward.
pipeline {
agent any
environment {
VERSION = "1.2.3-SNAPSHOT"
}
stages {
stage("set build name") {
steps {
script {
currentBuild.displayName = "v${env.VERSION}"
currentBuild.description = "#${BUILD_NUMBER} (v${env.VERSION})"
}
}
}
}
}
It results in the following representation in Jenkins' UI:
setBuildName("your_build_name") in a groovyPostBuild step may do the trick as well.
Needs Groovy Postbuild Plugin.

How can i add a post build step to an existing job using job dsl?

How can i add a post build step to an existing job using job dsl ?
Note: I need to append to the existing job. It should not delete the existing steps.
You can not append something to an existing job. You need to code the complete job definition in Job DSL.
But you can use the Jenkins API to add a post build step:
FreeStyleProject job = Jenkins.instance.getItem('job-a')
job.publishersList << new hudson.tasks.BuildTrigger('job-b', false)
You can try the code in Jenkins Script Console.
Note that a post build step will be added each time you run the script. If you code the complete job definition in Job DSL, the Job DSL engine will modify the job only if the script changed or your job configuration does not match the definition.

Jenkins: how to trigger pipeline on git tag

We want to use Jenkins to generate releases/deployments on specific project milestones. Is it possible to trigger a Jenkins Pipeline (defined in a Jenkinsfile or Groovy script) when a tag is pushed to a Git repository?
We host a private Gitlab server, so Github solutions are not applicable to our case.
This is currently something that is sorely lacking in the pipeline / multibranch workflow. See a ticket around this here: https://issues.jenkins-ci.org/browse/JENKINS-34395
If you're not opposed to using release branches instead of tags, you might find that to be easier. For example, if you decided that all branches that start with release- are to be treated as "release branches", you can go...
if( env.BRANCH_NAME.startsWith("release-") ) {
// groovy code on release goes here
}
And if you need to use the name that comes after release-, such as release-10.1 turning into 10.1, just create a variable like so...
if( env.BRANCH_NAME.startsWith("release-") ) {
def releaseName = env.BRANCH_NAME.drop(8)
}
Both of these will probably require some method whitelisting in order to be functional.
I had the same desire and rolled my own, maybe not pretty but it worked...
In your pipeline job, mark that "This project is parameterized" and add a parameter for your tag. Then in the pipeline script checkout the tag if it is present.
Create a freestyle job that runs a script to:
Checkout
Run git describe --tags --abbrev=0 to get the latest tag.
Check that tag against a running list of builds (like in a file).
If the build hasn't occurred, trigger the pipeline job via a url passing your tag as a parameter (in your pipeline job under "Build Triggers" set "Trigger builds remotely (e.g. from scripts) and it will show the correct url.
Add the tag to your running list of builds so it doesn't get triggered again.
Have this job run frequently.
if you use multibranch pipeline, there is a discover tag. Use that plus Spencer solution

How can I get the joblist in jenkins after I execute the jobdsl

How can I get the job list in jenkins after I execute the jobdsl ?
Jenkin JobDSL is nice to manage the jenkins jobs. When you execute the jobDSL, jenkins can help to generate the jobs are expected. Even more, if the job is created, you can choose to skip or overwrite.
Now I want to trigger the build directly after it is new generated.
See sample console output from jenkins build.
Processing DSL script demoJob.groovy
Added items:
GeneratedJob{name='simpliest-job-ever'}
Existing items:
GeneratedJob{name=’existing-job'}
How can I get the jobname simpliest-job-ever in jenkins? And in this case, I don't want to build existing-job
Scan the console log could be choice, but it is not elegant enough.
You can trigger a build from the DSL script using the queue method (docs).
job('simpliest-job-ever') {
// ...
}
queue('simpliest-job-ever')

Add automatic counter field to 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.

Resources