'replace function on Pipeline resource variables

Follow the azure online document and Pipeline resource variables I'd like to get the branch name from another pipeline, and use function replace() to get the real branch name.

variables:
- name: branchName
  value: $[replace(variables[resources.pipeline.mypipeline.sourceBranch], 'refs/heads/', '')]

but it always failed to pass the pipeline syntax check



Solution 1:[1]

I tested the following code and it's working.

Ensure you wrap the var in single quotes e.g: variables[`resources.pipeline.mypipeline.sourceBranch`]

And that you have defined a resources: block pointing to your pipeline.

pool: default

resources:
  pipelines:
  - pipeline: james
    source: james

variables:
- name: branchName
  value: $[replace(variables['resources.pipeline.james.sourceBranch'], 'ref', 'YEAHBUDDY')]

stages:
  - stage: approval_check
    jobs:
    - job: bash
      steps:
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            # Write your commands here
            set -x
            echo $(branchName)

Bash Output:

echo YEAHBUDDYs/heads/main

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James