Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace in variable strings inside azure-pipelines.yaml?

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.

like image 219
Macke Avatar asked Sep 06 '19 07:09

Macke


People also ask

How do you override variables in Azure Devops pipeline?

Just provide the same name/value pair and save. The checkbox “Let users override this value when running this pipeline” enables overwriting the variables value within the YAML pipeline. Like if based on some logic within pipeline, you want to change the value to server02.

How do I edit YAML in Azure pipeline?

Edit a YAML pipelineSign in to your organization ( https://dev.azure.com/{yourorganization} ). Select your project, choose Pipelines > Pipelines, and then select the pipeline you want to edit. Choose Edit. Make edits to your pipeline using Intellisense keyboard shortcuts and the task assistant for guidance.

How do you pass variables in Azure Pipelines YAML tasks?

Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.


1 Answers

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

Roughly like this to replace underscores with hypens:

variables:      
  suffix: $[replace(variables['build.sourcebranchname'], '_', '-')]
like image 73
Yacuzo Avatar answered Oct 12 '22 12:10

Yacuzo