简体   繁体   English

Azure Devops 中拉取请求的构建策略是否可以使用在所述 PR 中更新的 yaml 文件?

[英]Can the build policies on a pull request in Azure Devops use the yaml-files that is updated in said PR?

We have all our pipelines checked into code, but if we deliver a PR with changes in those pipelines, the PR Build Policies will run with the YAML-files in MASTER, not the ones that we want to check into master.我们将所有管道都签入了代码,但是如果我们交付了在这些管道中发生变化的 PR,则 PR 构建策略将与 MASTER 中的 YAML 文件一起运行,而不是我们想要签入 master 的文件。 It's basically a deadlock.这基本上是一个僵局。

Say you want to remove a validation that makes all your PRs fail, so you make a PR but you can't merge it cause the build policies fail :P假设您要删除使所有 PR 失败的验证,因此您创建了一个 PR,但无法合并它,导致构建策略失败:P

PS: I know I can remove the policies, complete the merge, and add the policies back in as a manual job, but that is not really a good solution. PS:我知道我可以删除策略,完成合并,然后手动添加策略,但这并不是一个好的解决方案。

Create a separate yaml pipeline with the pre merge build steps that you then set in the PR policies for.使用您随后在 PR 策略中设置的预合并构建步骤创建一个单独的 yaml 管道。 It will always run the code from the current branch that the PR is created from.它将始终从创建 PR 的当前分支运行代码。

We do it like this: (All in same repo)我们这样做:(都在同一个仓库中)

  1. build_steps.yml - Yaml template with build steps build_steps.yml - 带有构建步骤的 Yaml 模板
  2. azure-pipelines-yml - Main pipeline that has a reference to build_steps.yml to build the project azure-pipelines-yml - 引用 build_steps.yml 以构建项目的主管道
  3. pre_merge.yml - Secondary pipeline that is run only by PR request which has a reference to build_steps.yml so there are no differences in the build and two places to update if something changes. pre_merge.yml - 仅由 PR 请求运行的辅助管道,它引用了 build_steps.yml,因此构建中没有差异,如果发生变化,有两个地方需要更新。

Whole yaml definition:整个 yaml 定义:

#pre_merge.yml
trigger: none #Pipeline should never trigger on any branches. Only invoked by the policy.

variables:
  - name: system.buildConfiguration 
    value: 'Release'

  - name: system.buildPlatform
    value: 'win10-x86'

  - name: system.debug
    value: 'false'

pool:
  vmImage: 'windows-latest'

name: $(SourceBranchName)_$(date:yyyyMMdd)$(rev:.r)

steps:
- template: build_steps.yml

And then in the policies you setup it like this:然后在你这样设置的策略中: 在此处输入图片说明

All this applies also to classic pipelines.所有这些也适用于经典管道。 You need to create a seperate pre-merge build pipeline that could reference a TaskGroup with the steps used in the main build pipeline.您需要创建一个单独的预合并构建管道,该管道可以使用主构建管道中使用的步骤引用 TaskGroup。 In both cases you dont have to use a template or Taskgroup and and create the steps manually.在这两种情况下,您都不必使用模板或任务组并手动创建步骤。 But if the build would change in the future you have 2 places to update.但是,如果构建将来会发生变化,您有 2 个地方需要更新。

Just throwing this out there as an alternative.只是把它扔到那里作为替代。 However;然而; if desired can maintain one yaml pipeline that can do both the pre merge and the deployment.如果需要,可以维护一个可以进行预合并和部署的 yaml 管道。

Would want to create a variable to detect if the pipeline is running against the "master/main" branch similar to:想要创建一个变量来检测管道是否在“主/主”分支上运行,类似于:

  IsMainBranch: $[eq(variables['Build.SourceBranch'], 'refs/heads/master')]

From there the variable will need to be a condition on subsequent jobs/stages.从那里变量将需要成为后续作业/阶段的条件。 Personally I do not like this approach as it limits the portability of the pipelines.我个人不喜欢这种方法,因为它限制了管道的可移植性。 In addition it interjects another variable to account for;此外,它还插入了另一个变量来解释; however, felt it fair to provide an alternative option.但是,认为提供替代选择是公平的。

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

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