简体   繁体   English

Azure DevOps - 拉取请求工作流和部署

[英]Azure DevOps - Pull Request Workflow and Deployments

I'm a bit confused with how to set this workflow up using pull requests.我对如何使用拉取请求设置此工作流程有点困惑。

I have in place an existing multi-stage YAML build pipeline that in summary does the following:我有一个现有的多级 YAML 构建管道,总而言之,它执行以下操作:

  1. Runs a build from any branch从任何分支运行构建
  2. Runs a deployment job to a Development Environment/Resource, if the source branch is feature/* or release/* and the build succeeds如果源分支是 feature/* 或 release/* 并且构建成功,则将部署作业运行到开发环境/资源
  3. Runs a deployment job to a UAT Environment/Resource, if the source branch is release/*如果源分支是发布/*,则将部署作业运行到 UAT 环境/资源
  4. Runs a deployment job to Live/Production Environment/Resource, if the source branch is master如果源分支是主分支,则将部署作业运行到 Live/Production Environment/Resource

So off the back of CI this workflow seems to work fine, the correct stages are run depending on the branch etc.因此,在 CI 的背后,这个工作流程似乎运行良好,正确的阶段根据分支等运行。

I then decided that branch policies and pull requests might be a better option for code quality rather than let any of the main branches be direct committed to, so I started to rework the YAML to account - mainly by removing the trigger to然后我决定分支策略和拉取请求可能是代码质量的更好选择,而不是让任何主要分支直接提交,所以我开始重新设计 YAML 来考虑 - 主要是通过删除触发器

trigger: none

This now works correctly in line with a branch policy, the build only gets kicked off when a pull request on to develop or master is opened.这现在可以根据分支策略正常工作,只有在打开开发或主控的拉取请求时才会启动构建。

However this is then where I'm a bit confused with how this is supposed to work and how I think it works....然而,这就是我对它应该如何工作以及我认为它是如何工作的有点困惑的地方......

Firstly - is it not possible to trigger the multi-stage YAML off the back of pull requests (using Azure Repos)?首先 - 是否不可能在拉取请求的后面触发多阶段 YAML(使用 Azure Repos)? In my head all I want to do is introduce the pull request and branch policies but keep the multi-stage deployments to environments as is.在我的脑海中,我想做的就是引入拉取请求和分支策略,但保持多阶段部署到环境的原样。 However, the deployment job stages all get skipped now - but this might be to do with my conditions within the YAML, which are as follows:但是,部署作业阶段现在都被跳过了——但这可能与我在 YAML 中的条件有关,如下所示:

- stage: 'Dev'
  displayName: 'Development Deployment'
  dependsOn: 'Build'
  condition: |
    and
    (
      succeeded()
      eq(variables['Build.Reason'], 'PullRequest'),
      ne(variables['System.PullRequest.PullRequestId'], 'Null'),
      or
      (
        startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/'),
        startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')
      )
    )
  jobs:
  - deployment: Deploy
    pool: 
      name: 'Development Server Agent Pool'
    variables:
      Parameters.WebsitePhysicalPath: '%SystemDrive%\inetpub\wwwroot\App'
      Parameters.VirtualPathForApplication: ''
      Parameters.VirtualApplication: ''
    environment: 'Development.Resource-Name'
.....

Is there something I am missing?有什么我想念的吗? Or do I have to remove the multi-stage deployments from YAML and revert back to using Release Pipelines for pull requests (maybe with approval gates??)或者我是否必须从 YAML 中删除多阶段部署并恢复使用发布管道来处理拉取请求(可能有审批门??)

Thanks in advance!提前致谢!

It looks like the issue of the conditions within the YAML.看起来像是 YAML 内的条件问题。

If the pipeline is triggered by a PR.如果管道由 PR 触发。 the value variables['Build.SourceBranch'] will be refs/pull/<PR id>/merge .variables['Build.SourceBranch']将为refs/pull/<PR id>/merge The express in above condtion startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/') will be false, which caused the stage to be skipped.上述条件startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/')中的表达式为false,从而导致该阶段被跳过。 See build variables for more information.有关详细信息,请参阅构建变量

You can try using variables['System.PullRequest.SourceBranch'] , which will be evaluated to the value of the source Branch of the PR.您可以尝试使用variables['System.PullRequest.SourceBranch'] ,它将被评估为 PR 的源分支的值。 Check System variables for more information.检查系统变量以获取更多信息。 See below:见下文:

condition: |
    and
    (
      succeeded(),
      eq(variables['Build.Reason'], 'PullRequest'),
      ne(variables['System.PullRequest.PullRequestId'], ''),
      or
      (
        startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/feature/'),
        startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/release/')
      )
    )

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

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