Azure Pipeline 使用 YAML 触发流水线

Ale*_*ski 25 azure azure-devops azure-pipelines

尝试在使用 YAML 完成另一个管道时触发 Azure 管道。有文档表明您可以添加管道资源:

resources:   # types: pipelines | builds | repositories | containers | packages
  pipelines:
  - pipeline: string  # identifier for the pipeline resource
    connection: string  # service connection for pipelines from other Azure DevOps organizations
    project: string # project for the source; optional for current project
    source: string  # source defintion of the pipeline
    version: string  # the pipeline run number to pick the artifact, defaults to Latest pipeline successful across all stages
    branch: string  # branch to pick the artiafct, optional; defaults to master branch
    tags: string # picks the artifacts on from the pipeline with given tag, optional; defaults to no tags
Run Code Online (Sandbox Code Playgroud)

但是,我一直无法弄清楚“来源”是什么意思。例如,我有一个名为的管道myproject.myprogram

resources:
  pipelines:
  - pipeline: myproject.myprogram
    source: XXXXXXXX
Run Code Online (Sandbox Code Playgroud)

此外,目前还不清楚您将如何基于此构建触发器。

我知道这可以从 web-GUI 完成,但应该可以从 YAML 中完成。

小智 20

对于从另一个 azure 官方文档触发一个管道,建议以下解决方案。即使用管道触发器

resources:
  pipelines:
  - pipeline: RELEASE_PIPELINE // any arbitrary name
    source: PIPELINE_NAME.    // name of the pipeline shown on azure UI portal
    trigger:
    branches:
      include:
        - dummy_branch        // name of branch on which pipeline need to trigger
Run Code Online (Sandbox Code Playgroud)

但实际上发生的是,它触发了两个管道。举个例子,假设我们有两个管道 A 和 B,我们想在 A 完成时触发 B。因此,在这种情况下,B 运行 2 次,一次是在您执行提交时(与 A 并行),第二次是在 A 完成后。

为了避免这两次管道运行问题,请遵循以下解决方案

trigger: none // add this trigger value to none 
resources:
  pipelines:
  - pipeline: RELEASE_PIPELINE // any arbitrary name
    source: PIPELINE_NAME.    // name of the pipeline shown on azure UI portal
    trigger:
    branches:
      include:
        - dummy_branch        // name of branch on which pipeline need to trigger
Run Code Online (Sandbox Code Playgroud)

通过添加trigger:none第二个管道不会在开始提交时触发,只有在第一次完成其工作时才会触发。

希望它会有所帮助。


Sij*_*hew 11

Microsoft文档说 YAML 是首选方法。因此,与其使用 build-trigger 选项,不如让我们了解一下有点令人困惑的 YAML 触发器。以下标签将适用于原始问题,现在使用更简单的文档:

resources:
  pipelines:
  - pipeline: aUniqueNameHereForLocalReferenceCanBeAnything
    project: projectNameNOTtheGUID
    source: nameOfTheOtherPipelineNotTheDefinitionId
    trigger:
      branches:
        include:
        - master
        - AnyOtherBranch
Run Code Online (Sandbox Code Playgroud)

Microsoft 的文档令人困惑,ID 很多。有时他们想要项目 GUID 有时是项目名称。有时他们需要管道名称,有时需要管道定义 ID。但是它们对变量(项目和管道)使用相同的名称。最重要的是,他们编写的文档不容易猜测使用哪种方法的最佳方法是反复试验。

我想为了避免其他地方的混淆,我举了管道中另一个地方的例子,你引用具有不同值的相同变量。在 DownloadArtifact 任务中,您需要使用项目 GUID 和管道定义 Id,如下所示:

- task: DownloadPipelineArtifact@2
      inputs:
        source: specific (a literal constant value not the pipeline name)
        project: projectGUIDNOTtheProjectName
        pipeline: numericDefinitionIdOfPipelineNotPipelineNameOrUniqueRef
        runVersion: 'latest'
Run Code Online (Sandbox Code Playgroud)

看看他们如何以不同的方式使用相同的变量,但都指的是管道,在我的情况下是完全相同的管道。这可能会造成混乱并避免陷入下一个问题,我在此提供澄清。

  • 你的第一个 yaml 片段对我很有帮助。我一直认为文档意味着在 DevOps 中放置代码路径而不是管道名称。Microsoft 文档在解释与代码库和 DevOps 相关的预期值方面做得非常糟糕。 (3认同)

Sha*_*zyk 8

这些资源不适用于 Build Completion 触发器。根据文档构建完成触发被支持在YAML语法。

创建 YAML 管道后,您可以转到经典编辑器(单击settingsvariables)并在那里创建触发器。

编辑:

现在您需要单击“触发器”:

在此处输入图片说明

进而:

在此处输入图片说明

第二次编辑:

微软也在 YAML 中添加了此功能 :) 请参见此处

# this is being defined in app-ci pipeline
resources:
  pipelines:
  - pipeline: security-lib
    source: security-lib-ci
    trigger: 
      branches:
      - releases/*
      - master
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我们有两个管道——app-ci 和 security-lib-ci。我们希望 app-ci 管道在每次在 master 或 release 分支中构建新版本的安全库时自动运行。


rsy*_*rsy 6

如果您不从触发管道发布工件,它将不会触发触发管道。

此外,如果defaultBranch for manual and scheduled builds触发管道中的 与您的工作分支不同,则触发管道不会在触发管道执行结束时启动。

我已经为管道触发器创建了一个最小可行产品,并且更好地解释了我刚刚在这个答案中提到的两个问题。