25

I want to set VersionSuffix from Build.Sourcebranch but this fails since SourceBranch contains refs/heads/<branchname>.

- task: DotNetCoreCLI@2
  condition: succeeded()
  inputs: 
    command: 'pack'
    versioningScheme: ByPrereleaseNumber
    majorVersion: '0' 
    minorVersion: '1' 
    patchVersion: '1' 
    packTimezone: 'utc' 
    buildProperties: VersionSuffix=$(Build.SourceBranch)-$(Build.BuildNumber)

I just want to add .Replace('/','_') and a few similar statements to $(Build.SourceBranch), but I can't find anything in the expression syntax on how to do that.

It didn't work to send in another string (i.e. VersionSuffixRaw) and create the VersionSuffix with String.Replace inside the .csproj; it just got ignored for some reason.

Note: There is Build.SourceBranchName which has the last part of a branchname, so if SourceBranch is refs/heads/feature/foo, SourceBranchName will be foo. However a branch namd feature/JIRA-123_foo_unittest will not work since _ is not valid in a version string.

Macke
  • 24,812
  • 7
  • 82
  • 118

3 Answers3

42

For others finding this through searches, there is now a replace expression: MS Doc

variables:      
  branch: $[replace(variables['Build.SourceBranch'], '/', '-')]
goulashsoup
  • 2,639
  • 2
  • 34
  • 60
Yacuzo
  • 542
  • 7
  • 6
  • 3
    I couldn't get this working, other than for conditions. Had to use template expression syntax like this: `${{ replace(variables['build.sourcebranchname'], '_', '-') }}` or i would get errors in the pipeline (although the code actually worked, and did the replacement, it still gave errors) In my case i was using my own variable aswell, not one of the "built-in" ones. – Jim Wolff Apr 19 '23 at 06:19
  • 2
    When to use expression or template syntax is quite finicky. Depends on where you declare the var you want to replace on and where you do the replacement. I have this monstrosity in a job variable: $[replace('${{ lower(variables['build.sourcebranchname'])}}', '_', '-')]. – Yacuzo Aug 04 '23 at 07:48
8

As @4c74356b41 pointed out. You can try adding powershell scripts to replace the Build.SourceBranch in your yaml build definition. And output the new sourcebranch to a new variable. Then you can use the new variable in the next steps.

Below is just a simple example. Click here for more information

1, replace "/" to "_" for SourceBranch and set the replaced value to variable newSourceBranch

- powershell: |
    $newbranch = "$(Build.SourceBranch)" -replace "/", "_"  
    echo "##vso[task.setvariable variable=newSourceBranch;isOutput=true]$newbranch"
  name: branchsetter 

2, Use the newSourceBranch in the following steps.

- powershell: |
    write-host "$(branchsetter.newSourceBranch)"
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • It helped me to replace $(Build.Repository.Name) from ```workspace/reponame``` to ```reponame``` – wia Mar 26 '21 at 12:50
3

I don't think you can do something like that natively yet, what I've been doing is the following:

  - bash: |
      date=$(date --rfc-3339=ns | sed "s/ /T/; s/\(\....\).*-/\1-/g")
      echo "##vso[task.setvariable variable=CONTAINER_BUILD_TIME]$date"

basically using a script step to set a specific value for a specific variable, and later on you can use it like you normally would: $(date)

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
4c74356b41
  • 69,186
  • 6
  • 100
  • 141