Search code examples
jenkinsjenkins-plugins

Adding version number to Jenkins build artifact


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


Solution

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

    Install the Environment Injector plugin. You now have two options:
    General tab
    [ X ] Prepare an environment for the run

    Build Environment tab
    [ X ] Inject environment variables to the build process

    Set the "Properties Content" (that's an environment variable).

    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 Global and the Node level. You can also have individual build steps to inject / change variables between job steps (we use this to set specific JAVA_HOME for SonarQube step).

    You will also see an [Environment variables] button on the LH side of each build log to inspect what you ran with (see below).


    If you add Build With Parameters plugin then you can be prompted to supply values for variables when triggering the job which can be consumed in the same fashion without re-configuring the job (it won't remember them, but you will see a [Parameters] button on the LH side of each build log to inspect what you ran with.

    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