Can I substring a variable in Azure Pipelines?

Viewed 13453

I'm looking for a way to define a variable in my azure-pipelines.yml file where I can substring the 'Build.SourceVersion' -> Use the first 7 characters only.

Seems like there is no build-in function that can do such string operations in the documentation. Is there something I'm missing?

My other approach would be to use a bash task and overwrite the variable there but finding something build-in that can do this would be way better solution.

5 Answers

My other approach would be to use a bash task and overwrite the variable there but finding something build-in that can do this would be way better solution.

I agree with Lucas. There is no such built-in task to get the first 7 characters of $(Build.SourceVersion) in Azure DevOps.

We could use the command line/powershell task to split long sha into short sha:

echo $(Build.SourceVersion)

set TestVar=$(Build.SourceVersion)

set MyCustomVar=%TestVar:~0,7%

echo %MyCustomVar%

echo ##vso[task.setvariable variable=ShortSourceVersion]%MyCustomVar%

In this case, we could get the short versions of Build.SourceVersion and set it as environment variable.

Then we could set this command line task as a task group:

enter image description here

So, we could use this task to set the ShortSourceVersion directly.

Hope this helps.

You are right, there is no native way to do it. You will have to write a script to transform the variable.

Here is an example:

trigger:
- master

resources:
- repo: self

stages:
- stage: Build
  displayName: Build image
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - task: CmdLine@2
      inputs:
        script: ' x=`echo "$(Build.SourceVersion)" | head -c 7`; echo "##vso[task.setvariable variable=MyVar]$x"'
    - task: CmdLine@2
      inputs:
        script: 'echo "$(MyVar)"'

Here is the shortest version that I use for this job;

- bash: |
    longcommithash=$(Build.SourceVersion)
    echo "##vso[task.setvariable variable=shorthash;]$(echo ${longcommithash::9})"

It gives you the output as shown below;

enter image description here

On Azure devops it's possible to be done using batch technique indeed - like other answers propose in here - however I wanted to remove string from the end of string - and with batch this gets more complex ( See following link ).

Then I've concluded to use Azure's built-in expressions, for example like this:

- name: sourceBranchName
  value: ${{ replace(variables['Build.SourceBranch'], 'refs/heads/') }}

replace will have similar effect to substring - so it will remove unnecessary part from original string.

If however you want to concatenate some string and then remove it from original name - it's possible to be done using for instance format function:

- name: originBranchName
  value: ${{ replace(variables.sourceBranchName, format('-to-{0}', variables.dynamicvar ) ) }}

If dynamicvar is a - then function will remove -to-a from branch name.

Sure you can! If you absolutely must. Here is a runtime computation taking the first 7 characters of the Build.SourceVersion variable.

variables:
  example: ${{ format('{0}{1}{2}{3}{4}{5}{6}', variables['Build.SourceVersion'][0], variables['Build.SourceVersion'][1], variables['Build.SourceVersion'][2], variables['Build.SourceVersion'][3], variables['Build.SourceVersion'][4], variables['Build.SourceVersion'][5], variables['Build.SourceVersion'][6]) }}

NB: I can't get it to work with $[...] syntax as the variable is apparently empty at the initial.

Related