2

I have two identical replace statements in the azure pipeline yaml file

 - script: echo ${{ replace('refs/heads/origin', 'refs/heads', 'origin') }}
 - script: echo $[ replace('refs/heads/origin', 'refs/heads', 'origin') ]

except for one is a runtime expression while the other one is a compile time expression.

While the compile time expression works fine run time expression is giving me following error

line 1: replace('refs/heads/origin', 'refs/heads', 'origin') : syntax error in expression (error token is "('refs/heads/origin', 'refs/heads', 'origin') ")

How do I make runtime replace expression works fine?

1 Answer 1

3

You should use a variable for this:

variables:
  runtimeTest: $[ replace('refs/heads/origin', 'refs/heads', 'origin') ]

Then you can reference it in the script part with no errors:

steps:
- script: echo $(runtimeTest)
Sign up to request clarification or add additional context in comments.

6 Comments

Cool, it works with hardcoded values, thank you. Can you tell how can I do with PredefiinedVariables $[replace(variables['System.PullRequest.TargetBranch'], 'refs/heads', 'origin')] returns refs/heads/master
It should work with the syntax you use. Note that according to the docs for System.PullRequest.TargetBranch : "This variable is initialized only if the build ran because of a Git PR affected by a branch policy. This variable is only available in a YAML pipeline if the PR is affected by a branch policy." learn.microsoft.com/en-us/azure/devops/pipelines/build/…
So are runtime expressions supported only in variables? I need to call the format() function with a runtime variable and a compile time variable (from the each loop); is there no way to do that?
@Palec it's not quite clear what you're trying to achieve. It would be great if you shape it into a separate question.
IIRC, I gave up and used a script for that, @Keab42.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.