简体   繁体   English

如何使用 azure devops api 或其他方法从拉取请求中获取目标分支?

[英]how to get target branch from a pull request using azure devops api or other methods?

I'm trying to make a single build pipeline for 3 env (dev, qa, prod) but with ability to choose which one to build from.我正在尝试为 3 env(dev、qa、prod)创建一个构建管道,但能够选择从哪个构建管道。

The idea is to keep the pipeline on prod branch or another repo, and not having it in every env.这个想法是将管道保留在 prod 分支或另一个 repo 上,而不是在每个环境中都有它。 The issue now is that on a PR it will start the pipeline only on master(prod) branch as it shall contain the yml file.现在的问题是,在 PR 上它只会在 master(prod) 分支上启动管道,因为它应该包含 yml 文件。

Is there a way to get the PR target branch in order to add additional conditions for PR triggers?有没有办法获得 PR 目标分支以便为 PR 触发器添加额外的条件?

how to get target branch from a pull request using azure devops api or other methods?如何使用 azure devops api 或其他方法从拉取请求中获取目标分支?

Agree with Yan Sklyarenko.同意 Yan Sklyarenko 的观点。 Azure devops provides us with some predefined variables , like: Azure devops 为我们提供了一些预定义的变量,例如:

System.PullRequest.IsFork
System.PullRequest.PullRequestId
System.PullRequest.PullRequestNumber
System.PullRequest.SourceBranch
System.PullRequest.SourceRepositoryURI
System.PullRequest.TargetBranch

To get the target branch from a pull request, we could use the predefined variable System.PullRequest.TargetBranch .要从拉取请求中获取目标分支,我们可以使用预定义的变量System.PullRequest.TargetBranch

So, we could use this predefined variable as condition:所以,我们可以使用这个预定义的变量作为条件:

condition: and(succeeded(), eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/master'))

You can still have one build pipeline.您仍然可以拥有一个构建管道。 PR triggers are for github/bitbucket repositories. PR 触发器用于 github/bitbucket 存储库。 You can create branch policy which triggers your code etc.您可以创建触发您的代码等的分支策略。

The conditions you can have on each step/task: conditions in Azure DevOps for example:您可以在每个步骤/任务上拥有的条件: Azure DevOps中的条件,例如:

- stage: B
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/master'))

with that condition this stage will run, only when source branch is named "master"只有当源分支被命名为“master”时,这个阶段才会运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM