I am looking for a way to modify the Jenkins build number from within the pipeline execution itself. Currently, Jenkins increments the build number automatically, but for our release tagging strategy, we need the build number to align with a specific versioning logic. Is there a plugin or a Groovy script that allows me to override the BUILD_NUMBER environment variable or set the next build number programmatically so it stays consistent with our internal tracking?
3 answers
To change the current build number during execution, you generally use the "Next Build Number" plugin, as Jenkins treats the current build ID as immutable once the process starts. However, you can use a Groovy script in the Jenkins script console or a pipeline stage to set the next build ID for future runs. For the current run, most people use the currentBuild.displayName property to change how the build appears in the history. This allows you to append version strings or custom identifiers without breaking the underlying filesystem structure that Jenkins relies on for build metadata.
Are you trying to change the actual integer ID in the filesystem, or are you just looking to make the UI display a specific version string like 1.0.4-beta?
I usually just update the display name in my Jenkinsfile. It is much safer than trying to force the internal counter to change mid-stream.
I agree with Amanda and Christopher. Overriding the internal build ID often causes issues with plugins that track history. Stick to changing the display name for visibility.
If you only need the UI to look different, use currentBuild.displayName = "Your-Version-Number". If you actually need to skip numbers or reset the counter, you must use Jenkins.instance.getItem('JobName').updateNextBuildNumber(Integer.parseInt(newNumber)). Note that this requires admin permissions and should be done carefully to avoid build collisions in your job history.