首页 > 解决方案 > 如何防止触发基于提交标记的 Azure DevOps 构建管道?

问题描述

我正在将 Azure 管道与基于 Github 的项目一起使用。我已经建立了一个构建管道,它仅由标记的提交触发,以使其与每次提交时发生的自动每日构建分开。

我想从触发每日构建管道中排除标记的提交。在 yaml 脚本中这样做的正确方法是什么?

这是我所做的,但没有成功。

根据此页面上的 Azure 文档,据我了解,应该可以通过以下方式排除标签:

trigger:
  tags:
    exclude:
    - projectname_v*

但是,这不起作用,只会阻止构建管道在任何提交时运行,无论是否标记。

我也试过:

trigger:
  tags:
    include:
    - *
    exclude:
    - projectname_v*

但这显然不受支持,因为它会产生错误:

/azure-pipelines.yml: (Line: 12, Col: 7, Idx: 220) - (Line: 12, Col: 8, Idx: 221): While scanning an anchor or alias, did not find expected alphabetic or numeric character.

我还尝试了文档页面上提出的替代语法:

trigger:
  branches:
    exclude:
      refs/tags/{projectname_v*}

以及带/不带大括号和通配符的变体,但都因“意外值”或“输入字符串格式不正确”错误而失败。

编辑 2019-12-10

在阅读了下面 wallas-tg 的答案后,我在日常构建管道中尝试了以下内容:

trigger:
  branches:
    include:
    - '*'
    exclude:
    - 'refs/tags/*'

这有效,但不符合我的要求:

标签: azure-devopsazure-pipelines

解决方案


The syntax for build pipeline triggers is documented on this page.
Regarding what is exposed in the question, a couple of details are worth highlighting:

  1. There is a default implicit trigger that includes all branches and is overwritten by any user-defined trigger. Thus, it is not possible to specify a trigger that only excludes something: doing that would end up in nothing being included, and the trigger would never fire.
    This explains why the first code snippet shown in the question does not trigger anything.

When you specify a trigger, it replaces the default implicit trigger, and only pushes to branches that are explicitly configured to be included will trigger a pipeline. Includes are processed first, and then excludes are removed from that list. If you specify an exclude but don't specify any includes, nothing will trigger.

  1. The default implicit trigger looks like this (note the comment in last line, which explains the error produced by the second code snippet in the question):
trigger:  
  branches:  
    include:  
    - '*'  # must quote since "*" is a YAML reserved character; we want a string  

Summarizing, a correct way to do exclude tagged commits from triggering the pipeline should be the one shown in the edited part of the question:

trigger:
  branches:
    include:
    - '*'
    exclude:
    - 'refs/tags/*'

Or, which is equivalent:

trigger:
  branches:
    include:
    - '*'
  tags:
    exclude:
    - '*'

However, this does not obtain the desired effect. The following happens instead:

  • Pushing a commit without tags triggers the pipeline
  • Pushing only a tag does not trigger the pipeline
  • Pushing a tagged commit still triggers the pipeline

A final feedback received from Azure DevOps support clarifies that there is no way at the moment to obtain the desired behaviour:

Basically there is no way right now to prevent builds from being triggered if the tags are committed along with the branch changes and the CI on branch changes are enabled on the pipeline. There are couple of options you can use to prevent triggering the build on new tags:

  1. Check-in the tags separately than the branch changes.
  2. Add "[skip ci]" to your commit message to skip triggering the build on that particular commit.

None of the two options fit well to the original request. #1 is already working as intended, i.e. it does not trigger the build unless tags are explicitly included in triggers, which is only a part of the original request. #2 has the desired behaviour, but it requires a specific commit text and would force any user of the pipeline to be aware of its internals.

The workaround that I found in the meantime, as mentioned in a comment, was to use only one pipeline, that is always triggered by any commit, whether tagged or not, and detect the use of tags with dedicated scripts to activate specific pipeline steps when required.


推荐阅读