从另一个管道触发Azure Devops管道

时间:2020-03-11 19:50:10

标签: azure azure-devops azure-pipelines

我在从Azure DevOps的另一个管道触发管道时遇到问题。我有一个配置项管道,并且每当配置项在主分支上传递时,我都想触发一个部署管道。这似乎在技术上是可行的,但是documentation尚不清楚。

我看到以下内容:

{'files': [{'d': 'asdasdas', 'data': {'d': 'asdasdas'}}, {'d': 'asdasdas', 'data': {'d': 'asdasdas'}}]}

但是尚不清楚a)这是在触发管道(在我的情况下为CI管道)还是在触发管道(在我的情况下为部署管道)中进行。

还不清楚# this is being defined in app-ci pipeline resources: pipelines: - pipeline: securitylib source: security-lib-ci trigger: branches: - releases/* - master pipeline是指什么,以及如何找出这些变量?它们都是管道的名称吗?我尝试了各种不同的排列,但似乎没有任何效果。

4 个答案:

答案 0 :(得分:12)

如果您不是从触发管道发布工件,那么它将不会触发被触发的管道。

此外,对使用这些类型的触发器也有很大的限制。必须将defaultBranch for manual and scheduled builds管道中的depends更改为工作分支。否则,它将不会在source管道执行结束时启动。因此,假设您正在feature分支上,并且defaultBranch设置为feature。您提交您的代码,一切将按预期运行:source管道开始运行,最后,depends管道将被触发。都好!但是,当您合并到master中时,如果不更改defaultBranch,则depends管道不会在source管道的末尾触发。在答案的末尾,我将说明如何更改defaultBranch


如何设置管道触发器

我设法将其启动并在一个简约项目上运行。 Here可以在Azure DevOps上获得代码和here项目。我将尝试引导您完成操作,并回答您在帖子中提出的问题。

我将触发管道称为depends管道,并将触发管道称为source管道。

source管道上,无需执行任何操作,除非发布工件。如果您没有从source管道发布工件,它将无法正常工作。在下面,您可以找到我用于虚拟source管道的代码。我希望为master分支触发它,最后,我想确保发布一个工件。

trigger:
  branches:
    include: # branch names which will trigger a build
    - master
pr: none

steps:
  # required to cause pipeline triggering downstream
  - task: CopyFiles@2
    inputs:
      contents: $(System.DefaultWorkingDirectory)/**/*.yml
      targetFolder: $(Build.ArtifactStagingDirectory)
  - task: PublishBuildArtifacts@1
    inputs:
      pathtoPublish: $(Build.ArtifactStagingDirectory)
      artifactName: dummy-$(Build.BuildId)

depends管道(如下所示的代码)上,我必须禁用CIPR触发器,否则当我提交此仓库时,该管道将由{ {1}}触发,然后在CI管道执行结束时触发。这是通过我的代码的前两行完成的。然后,我希望在名为source(在YAML中的source属性)的项目内,名为source(这是下面的YAML中的Pipelining属性)的管道将触发此更新project分支时的当前(depends)管道。

master

这有意义吗?重要的是,您在Azure DevOps上的项目名称必须与YAML trigger: none pr: none resources: pipelines: - pipeline: source project: Pipelining source: source trigger: branches: include: - master steps: - checkout: none - script: echo 'triggered depends' 管道代码中的property相匹配。对我来说,它是depends

enter image description here

以及Pipelining属性,同样在YAML source管道代码中。

enter image description here


更改depends分支

由于上面提到的问题,为了更改default,您应该编辑管道(在这种情况下为defaultBranch管道),然后在右上角的三个点上选择depends。然后选择Triggers标签,您将进入下图所示的屏幕,您可以在其中设置工作分支。

enter image description here

答案 1 :(得分:0)

应在触发的管道(部署管道)中定义yaml管道之上的触发器。

- pipeline: string此处的字符串是您赋予此管道资源的标识符。它可以是任何字符串。

source: string此处的字符串是触发管道的定义名称(您的CI管道的名称)。

以下yaml来自文档pipeline resource

resources:
  pipelines:
  - pipeline: string  # identifier for the pipeline resource
    project:  string # project for the build pipeline; optional input for current project
    source: string  # source pipeline definition name
    branch: string  # branch to pick the artifact, optional; defaults to all branches
    version: string # pipeline run number to pick artifact, optional; defaults to last successfully completed run
    trigger:     # optional; triggers are not enabled by default.
      branches:
        include: [string] # branches to consider the trigger events, optional; defaults to all branches.
        exclude: [string] # branches to discard the trigger events, optional; defaults to none.

选项:您还可以从Ui页面设置管道触发器。转到触发的Yaml管道(部署管道)的编辑页面,单击 3点,然后选择 Triggers

enter image description here

转到“触发器”->构建完成,然后单击“添加”->选择您的触发管道(CI管道)

enter image description here

更新:

我看到azure-deploy.yml中的管道资源定义如下。

resources:
  pipelines:
  - pipeline: 'Deploy to Development'
    source: 'DFE-Digital.dfe-teachers-payment-service'
  trigger:
    branches:
      include:
      - "master"
      - "release-stuff"

请尝试将 trigger 元素的缩进与 source 元素相同。请检查以下示例:

    resources:
      pipelines:
      - pipeline: 'Deploy to Development'
        source: 'DFE-Digital.dfe-teachers-payment-service'
        trigger:
          branches:
            include:
            - "master"
            - "release-stuff"

答案 2 :(得分:0)

我可以假设您不在master分支上工作,对吗?我以前有同样的问题。但是在我阅读了MS's doc的触发器的默认分支部分之后。我明白为什么。触发器默认情况下仅检查master分支的yaml文件。这意味着管道只会由master分支的yaml文件中的触发器定义来触发。

因此,无论您在其他分支(不是master分支)的yaml文件的trigger部分中添加的分支,tirgger都不处于活动状态。您需要更改管道以在当前分支而非主分支中查找yaml文件。只需按照文档的说明,更改默认触发器分支即可。您将使其正常工作。

将工作合并到master中之后,可能需要将dedault触发器分支改回master。

答案 3 :(得分:0)

我发现了以下内容:

  • 在源代码管道中,我不需要创建工件

  • 在依赖管道中,如果我想在对源分支进行任何提交后构建,我可以让它与此一起工作:

    trigger: none
    pr: none
    resources:
          pipelines:
          - pipeline: 'depends'
            source: 'common-gulp-trigger'
            trigger: true