Home > OS >  Adding version number to Jenkins build artifact
Adding version number to Jenkins build artifact

Time:12-06

I have been ordered to migrate a dotnet build from Bamboo to Jenkins. I used a Freestyle job to run a powershell script, using the PowerShell plugin and successfully built it. However I need to add version number to the built artifact. The Bamboo job uses: ~\.dotnet\tools\dotnet-lambda.exe package -pl $fullDir -f "netcoreapp3.1" -o Payment.${bamboo.majorVersion}.${bamboo.minorVersion}.${bamboo.revisionVersion}.${bamboo.buildNumber}.zip

I went into Jenkins Configuration and in Global Properties, created Environment variables named - buildNumber, majorVersion, minorVersion and revisionVersion, giving it values and in the Build part of the Freestyle job, I used: ~\.dotnet\tools\dotnet-lambda.exe package -pl $fullDir -f "netcoreapp3.1" -o Payment.${env.majorVersion}.${env.minorVersion}.${env.revisionVersion}.${env.buildNumber}.zip

However the name of the built artifact is: Payment.....zip

  1. How can I pass the variable values?
  2. Is there a way to auto increment the revisionNumber and buildNumber, instead of hard coding it?

I'm very new to both Bamboo and Jenkins. Any help would be extremely helpful.

Regards Ramesh

CodePudding user response:

Personally, I'd not configure this things globally as they seem job specific. Nevertheless,

Install the Job Config -Build Env

In your shell step( no need to preface with ${env....} ):

Execute Shell step:

#!sh -
echo ${FOO}.${BUILD_NUMBER}
echo ${LABEL}

Output:

[EnvInject] - Loading node environment variables.
[EnvInject] - Preparing an environment for the build.
[EnvInject] - Keeping Jenkins system variables.
[EnvInject] - Keeping Jenkins build variables.
[EnvInject] - Injecting contributions.
Building in workspace C:\Users\jenkins\.jenkins\workspace\Foo
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content 
FOO=bar

[EnvInject] - Variables injected successfully.
[Foo] $ sh - C:\Users\jenkins\AppData\Local\Temp\jenkins281351632631450693.sh
bar.8
Finished: SUCCESS

You'll also see at the bottom of the Execute Shell step a link to ${JENKINS_URL}/env-vars.html which lists variables available to shell scripts, which includes BUILD_NUMBER; use that in lieu of buildNumber.

The plugin also supports configuration same at both the Job buttons


The Version Number plugin can provides greater flexibility, say you want to AutoIncrement and the "BUILD_NUMBER" option is too limiting, it offers a variable BUILDS_ALL_TIME, which can use the above defined variables or hard-coded constants to aggregate a version label and optionally control which it is incremented (eg: only increment on successful builds). eg:

[ X ] Prepare an environment for the run
Properties Content
FOO=bar

[ X ] Create a formatted version number
Environment Variable Name [ BUILD-${FOO}.${BUILDS_ALL_TIME} ]
Skip Builds worse than [ SUCCESS ]

Execute Shell step:

#!sh -
echo ${FOO}.${BUILD_NUMBER}
echo ${LABEL}

Output:

[Foo] $ sh - C:\Users\jenkins\AppData\Local\Temp\jenkins4160554383847615506.sh
bar.10
BUILD-bar.2
  • Related