独立的暂存和生产管道

Separate staging and production pipelines

我希望为微服务以及暂存和生产提供单独的管道。它看起来像这样:

我刚开始设置 azure-pipelines.yaml 并将其作为 admin 的触发器:

trigger:
  branches:
    include:
    - staging
    - production
  paths:
    include:
    - admin/*

resources:
  - repo: self

我 运行 遇到的问题是,当我提交 staging 时,它会触发两个管道。

所以我的问题是:有没有一种方法可以让这个微服务的 stagingproduction 都有一个 yaml,或者我需要有两个单独的 yaml 个文件?


编辑:

鉴于这两件事,我认为除非我将 adminStaging.yamladminProduction.yaml:

分开,否则我想做的事情不太可能

You cannot use variables in triggers, as variables are evaluated at runtime (after the trigger has fired).

If you use templates to author YAML files, then you can only specify triggers in the main YAML file for the pipeline. You cannot specify triggers in the template files.

https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml

我同意@Daniel Mann 的观点,您应该转向采用更现代的分支策略。我通常使用 Gitflow 或基于 T运行k 的发布分支:

它们各有优缺点,但都能满足您的用例。你有一个非产品分支(develop 或 rc/*)和一个生产分支(master)。 UAT 部署将从非产品分支触发,产品发布将从 master.

触发

但是,要解决您关于是否可以同时部署到 UAT 和 Production 的问题,同时将触发器指向两个分支,是的。您可以设置您的 YAML 管道,但它有点不优雅。

实际上,您要做的是有选择地使用 条件if 语句 运行 你的阶段基于分支 我在下面的例子中包括了这两个阶段:

name: Whosebug-Example-Multiple-Triggers
trigger:
    - staging
    - production
stages:
    - ${{ if contains(variables['Build.SourceBranch'], 'refs/heads/staging') }}:
      - stage: StageA
        displayName: "Stage A"
        jobs:
          - job: output_message_job_a
            displayName: "Output Message Job"
            pool:
                vmImage: "ubuntu-latest"
            steps:
                - powershell: echo "Hello World!"
                  displayName: "Output Message"
            condition: contains(variables['Build.SourceBranch'], 'refs/heads/staging')

    - ${{ if contains(variables['Build.SourceBranch'], 'refs/heads/production') }}:
      - stage: StageB
        displayName: "Stage B"
        jobs:
          - job: output_message_job_b
            displayName: "Output Message Job"
            pool:
                vmImage: "ubuntu-latest"
            steps:
                - powershell: echo "Hello World!"
                  displayName: "Output Message"
            condition: contains(variables['Build.SourceBranch'], 'refs/heads/production')
          

您需要注意条件与 if 语句的行为不同:

  • 条件:将显示管道中跳过的阶段,但不会 运行 它们。
  • If 语句:将跳过对完全不符合 if 语句条件的 YAML 的解释,并且不会在您的管道执行中显示。这意味着,如果您尝试 运行 除了 stagingproduction[,您的管道将失败 =43=]分支机构。