azure - 独立的暂存和生产管道

标签 azure azure-devops azure-pipelines azure-devops-pipelines

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

enter image description here

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

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

resources:
  - repo: self

正如您可能立即看到的那样,我遇到的问题是,当我提交暂存时,它会触发两个管道。

所以我的问题是:有没有一种方法可以为这个微服务的staging生产使用一个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://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml

最佳答案

我同意@Daniel Mann,你应该采取更现代的分支策略。我通常使用 Gitflow 或基于 Trunk 的发布分支:

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

但是,为了解决您的问题,是否可以同时部署到 UAT 和生产,同时将触发器指向两个分支,是的。您可以设置 YAML 管道来实现这一点,但这有点不优雅。

实际上,您想要做的是使用条件if语句根据分支有选择地运行阶段 我已将两者都包含在下面的示例中:

name: Stackoverflow-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 的解释,并且不会在管道执行中显示。这意味着,如果您尝试在登台生产分支以外的任何分支上运行它,您的管道将会失败

关于azure - 独立的暂存和生产管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66321794/

相关文章:

node.js - 为什么此代码会发送重复的电子邮件?

azure-devops - Azure Function 部署成功,但不工作

azure-devops - ##[错误]未找到与指定模式匹配的项目文件

.net - 尝试dotnet还原多项目解决方案时无法解决错误

azure - 将sqlserver数据库复制到azure

azure - 如何在任务组中使用 Key Vault secret 而不创建任务组参数

python - 使用 python 获取图像并将其上传到 blob

java - Azure 构建管道,docker compose - 设置环境变量

tfs - 将本地 TFS 与 Slack 集成

手动触发时找不到 Azure Synapse Pipeline