chrsi
chrsi

Reputation: 1103

How to replace a token with a concatenated version variable in an Azure Devops Build Pipeline

My Azure DevOps Build Pipeline makes use of GitVersion for versioning the build artefacts. Having the task present sets the GitVersion-variable that gives access to GitVersion.MajorMinorPatch and so on. What I want to do though is concatenating this variable with the BuildID. Setting the artefact name with the following command works well:

name: $(GitVersion.MajorMinorPatch).$(BuildID)

In the next step I wanted to have the version to be written into various config files. I installed the ReplaceToken plugin that replaces a specific token (in my case #{Version}#) with the content of the variable "Version".

Creating a variable with the name Version and the value "$(GitVersion.MajorMinorPatch).$(BuildID)" simply replaces the variable with the string instead of the evaluated value. I already tried to set the variable in the job that runs after the GitVersion task, so it should already be present when the variable gets created.

eg.

- job: 'Frontend'
  dependsOn: 'Preparation'
  variables:
    Version: $('GitVersion.MajorMinorPatch')

  steps:
  - bash: echo $(Version)

The "Preparation" job is currently running the GitVersion task so it's present during the other jobs.

How can I get the concatenated GitVersion/BuildId-Version into the config files?

Upvotes: 2

Views: 1626

Answers (1)

chrsi
chrsi

Reputation: 1103

The issue was with the Scope of the GitVersion variable. I could access it only in the job where the task was executed.

The solution:

  1. Concatenate the Version (GitVersion.MajorMinorPatch + Build.BuildId) in a powershell script and export it in the job where the GitVersion is present.

    - powershell: echo "##vso[task. variable=Version;isOutput=true]$env:VERSION.$env:BUILD_ID"
      env:
        VERSION: $(GitVersion.MajorMinorPatch)
        BUILD_ID: $(Build.BuildId)
      name: SetVersion
      displayName: Set Version
    
  2. Map the Version to a Variable in the dependend job:

    - job: 'Frontend'
      dependsOn: 'Preparation'
      variables:
        Version: $[ dependencies.Preparation.outputs['SetVersion.Version']]
    

Now the Variable Version should be present in the Job

Upvotes: 3

Related Questions