admin管理员组

文章数量:1400037

I have a problem with adding resources. Code is stored on BitBucket, authoritation through service connection.

resources:
  repositories:
    - repository: componentsPR
      type: bitbucket
      name: <my_project>
      endpoint: '<my_connection>'
      ref: ${{ variables['System.PullRequest.TargetBranch'] }}
    - repository: components
      type: bitbucket
      name: <my_project>
      endpoint: '<my_connection>'
      ref: ${{ variables['Build.SourceBranch'] }}

The problem is - If it's PullRequest, then I don't have a ${{ variables['Build.SourceBranch'] }} branch in my project and pipeline fails.

In resources: repositories, I need to specify *

ref: ${{ variables['System.PullRequest.TargetBranch'] }}* - if Build.Reason is PullRequest,

or

ref: ${{ variables['Build.SourceBranch'] }} - in other cases.

Does someone know how to set if-else for this situation? I will appreciate any idea

I tried checkout, but endpoint was missed; tried $[eq(variables['Build.Reason'], 'PullRequest') ? variables['System.PullRequest.TargetBranch'] : variables['Build.SourceBranch']] - not working.

I have a problem with adding resources. Code is stored on BitBucket, authoritation through service connection.

resources:
  repositories:
    - repository: componentsPR
      type: bitbucket
      name: <my_project>
      endpoint: '<my_connection>'
      ref: ${{ variables['System.PullRequest.TargetBranch'] }}
    - repository: components
      type: bitbucket
      name: <my_project>
      endpoint: '<my_connection>'
      ref: ${{ variables['Build.SourceBranch'] }}

The problem is - If it's PullRequest, then I don't have a ${{ variables['Build.SourceBranch'] }} branch in my project and pipeline fails.

In resources: repositories, I need to specify *

ref: ${{ variables['System.PullRequest.TargetBranch'] }}* - if Build.Reason is PullRequest,

or

ref: ${{ variables['Build.SourceBranch'] }} - in other cases.

Does someone know how to set if-else for this situation? I will appreciate any idea

I tried checkout, but endpoint was missed; tried $[eq(variables['Build.Reason'], 'PullRequest') ? variables['System.PullRequest.TargetBranch'] : variables['Build.SourceBranch']] - not working.

Share Improve this question asked Mar 24 at 20:35 AnnAnn 234 bronze badges 3
  • To achieve what you want, you need to set if-else in the steps or scripts, you can't do it in resource. To give you more accurate suggestions, please clarify: 1. Is your self-repo, the repo where the current YAML file is located, an Azure repo or a Bitbucket repo? 2. Is the current pipeline triggered by Azure build validation policy or the PR in your Bitbucket repo? – Ziyang Liu-MSFT Commented Mar 25 at 2:06
  • I was trying to do an if-else in steps using "checkout step," but it asks for an endpoint— no idea how to pass it. Answering your questions: 1. It's my repo in a separate Bitbucket repository; the pipeline is in another current repository. I store a UI library there and am trying to use it during the FE app deploy. 2. Azure policy, I suppose. The process is in ADO. The current pipeline is triggered on changes and PRs; it chooses a proper template conditionally. – Ann Commented Mar 25 at 10:13
  • 1 If the repo componentsPR and components are the same repo in bitbucket, you can use iif expression as suggested by bryanbcook. ref: ${{ iif( eq( variables['Build.Reason'], 'PullRequest'), variables['System.PullRequest.TargetBranch'], variables['Build.SourceBranch']) }}. If they are two different repos, please clarify if you want the pipeline to checkout repo "componentsPR" when triggered by a PR, and checkout repo "components" otherwise? – Ziyang Liu-MSFT Commented Mar 26 at 8:18
Add a comment  | 

1 Answer 1

Reset to default 1

Expressions for ref are supported, however if you were to write it like this:

resources:
  repositories:
  - repository: name
    type: bitbucket
    endpoint: <service-connection-name>
    ${{ if ne( variables['Build.BuildReason'], 'PullRequest') }}:
      ref: ${{ variables['Build.SourceBranch'] }}
    ${{ else }}:
      ref: ${{ variables['System.PullRequest.SourceBranch'] }}

...you'll get an error saying that template expressions aren't supported in this context.

Fortunately, sprint 248 introduced iif which allows you to specify the if/else statement as a single expression.

resources:
  repositories:
  - repository: name
    type: bitbucket
    endpoint: <service-connection-name>
    ref: ${{ iif( ne( variables['Build.BuildReason'], 'PullRequest'), variables['Build.SourceBranch'], variables['System.PullRequest.SourceBranch']) }}

本文标签: