So I came across a problem recently where I have a Jenkins job that build multiple Nuget package and pushes them to Octopus Deploy. I spent some time looking into versioning and decided I wanted my packages to be versioned using the Microsoft versioning mechanism used by MSBuild with wildcard characters in AssemblyInfo.cs.
This is done like this:
The versioning here is Major.Minor.Build.Revision and with the wildcard in place the Build and Revision are generated based on the these rules:
Build: Number of days since December 31st 1999
Revision: Number of second since midnight on the current day divided by two
So using Octopack this would work great, and my Nuget package would get generated with the correct version number, but my build job is more complex than that and some of the packages are created manually from files on disk, but I want all packages to share the same version number.
This got me thinking as to how I could create a dynamic parameter, so with some searching I found the Dynamic Parameter Plug-in. This allows you to create parameters where the default value is set by a Groovy script. Being primarily .NET focused I’d not come across groovy before but with a few Google searches I’d written the following script to generate a version number using the same logic:
def now = new Date() def midnight = new Date().clearTime() def from = new GregorianCalendar(1999, Calendar.DECEMBER, 31).time def build = groovy.time.TimeCategory.minus(now, from).days def release = (now.getTime() - midnight.getTime()).intdiv(1000).intdiv(2) '1.0.' + build + '.' + release
I have a Dynamic variable in Jenkins with this script:
And when I go to build by job I can see the following.