Bill
Bill

Reputation: 2993

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

Upvotes: 0

Views: 1682

Answers (1)

Glue Ops
Glue Ops

Reputation: 698

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

Upvotes: 2

Related Questions