Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I substring a variable in Azure Pipelines?

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.

like image 645
nikitz Avatar asked Jun 12 '20 12:06

nikitz


People also ask

How do you pass variables in Azure pipeline?

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.

How do you use a variable group in a pipeline?

To use a variable group, open your pipeline. Select Variables > Variable groups, and then choose Link variable group. In a build pipeline, you see a list of available groups. In a release pipeline, for example, you also see a drop-down list of stages in the pipeline.

How do I pass variables between jobs Azure DevOps?

Logging command called task. setvariable lets us pass variables across Tasks. Task. setvariable sets the value to a variable which by default can be used in any Task within a Job or across the Jobs of a given Stage.

Should I use Azure pipelines or GitHub actions?

Key differencesGitHub Actions uses YAML files to define workflows and does not support a graphical editor. Azure Pipelines allows you to omit some structure in job definitions. For example, if you only have a single job, you don't need to define the job and only need to define its steps.


3 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.

like image 188
Leo Liu-MSFT Avatar answered Nov 07 '22 17:11

Leo Liu-MSFT


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)"'
like image 40
Lucas Avatar answered Nov 07 '22 16:11

Lucas


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

like image 39
Kağan Mersin Avatar answered Nov 07 '22 17:11

Kağan Mersin