How to increase jenkins build number automatically

2019-03-27 20:36发布

问题:

How to increase jenkins build number automatically or by using shell script?? For now i am doing the same using configure option, and increasing manually. i want to do it automatically.

回答1:

What you are asking for (i.e. keeping the build number same between multiple jobs) is simply impossible in Jenkins. This is done by design (as noted by Jenkins creator himself): "[JENKINS] assumes that the build number is unique and monotonic.". You can change it to a higher value, but changing it to same number will outright break Jenkins and is therefore not possible.

Build run number is just that: a run number.
It is supposed to be sequential.
It is not a version number in any way.

There are several ways to get versioning information into the build history however.

Description Setter Plugin
This is what I use. It doesn't change the build number, but rather the description, which is done automatically. Furthermore, since this is a description, it can also be modified manually at any time.

This is what it looks like:

Note that builds #50 and #51 actually have the same version 1.3.0.394376.
In that version string, the 1.3.0 is the current release number, as determined by the project properties, while the 394376 is actually an SVN revision number, obtained from $SVN_REVISION_1 build variable. So every time there is a new commit in SVN, I get a new revision number. But if there are no commits and the job is rebuilt for whatever reason (like in #50 and #51), my version number remains same, as there was no code change.

The way I have it configured is:

  • Download Description Setter Plugin
  • Somewhere in your Build Steps, output your version *
  • I simply have:
    echo Version is: ${internalVersionNumberFromWherever}.${SVN_REVISION_1}
  • Next setup Post-build Action with Set build description
  • Setup Regular expression to extract the version number from the console: Version is: (.*)
  • Leave everything else blank, and the whatever RegEx picks up in (.*) will be used as description.
  • Alternatively, you can further set Description text as Build - \1. Where \1 is a reference to first group (.*) from the RegEx.
  • At any time you can manually change the description of the run by selecting the build run from the build history, and clicking "edit description" link on the right (provided that user has "Update" permissions configured).

* Note that if your build fails before the step that outputs the version text, the description setter plugin will not be able to pick it up. If you always want to see the version, then setup the display of version number as the very first step.

Build Name Setter plugin
This plugin will actually change the build number

However remember the limitation that each build number has to be unique.
Best way to ensure that is to add ${BUILD_NUMBER} to whatever arbitrary number that you want (like in the image above). This way you can have builds numbers like 50.123 and then 50.124 where 50 is your version while 123 and 124 are sequential build run numbers, and Jenkins makes sure of keeping the ${BUILD_NUMBER} unique.

Finally, there is the Version Number plugin which I haven't personally tried, but it may help you.