azure - azure-pipelines.yml 中的多个单独的触发器

标签 azure azure-devops azure-pipelines

我目前有一个在子目录中包含服务的单一存储库,我倾向于将其转变为带有元存储库的多存储库。

我决定尝试 Azure DevOps 的原因之一是有人告诉我你可以在子目录上设置触发器,例如:

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - client

经过测试,有效。

但是,我想知道是否可以有多个独立的触发器,或者这是否需要一个polyrepo或多个.yml?原因是,如果 client 服务中仅发生更改,它只会触发该组测试、构建和部署,而不会触发 api 服务来运行测试,构建和部署。

例如:

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - client

  stages:
    ...
    Run tests
    If tests pass, build and push to ACR
    Deploy to AKS
    ...

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - api

  stages:
    ...
    Run tests
    If tests pass, build and push to ACR
    Deploy to AKS
    ...

这样,其中的更改不会导致整个应用程序被重建,而只是更改了哪些内容。

但是,这是否需要多个 .yml 文件(甚至不确定是否可以识别 azure-pipelines.yml 之外的任何内容),这是否需要一个 polyrepo,或者这在我没有看到的单个 azure-pipelines.yml 中可行吗?

最佳答案

如果我正确理解您的请求,您可以在单个 azure-pipeline.yml 中实现此目的。请检查下面的示例 yml。

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - client/*
    - api/*

jobs:
- job: getchangepath
  pool:
    vmImage: 'windows-latest'
  steps: 
  - powershell: |
      $url="$(System.CollectionUri)/$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/commits/$(Build.SourceVersion)/changes?api-version=5.1"
      $result = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method GET
                       
      $changesFolder = $result.changes | Where-Object{$_.item.gitObjectType -match "tree"} | Select-Object -Property {$_.item.path}
     
      foreach($path in $changesFolder){
        if($path -match '/client'){
          echo "##vso[task.setvariable variable=Client;isOutput=true]$True"
          break
        }
      }

      foreach($path in $changesFolder){
        if($path -match '/api'){
          echo "##vso[task.setvariable variable=Api;isOutput=true]$True"
          break
        }
      }
    name: MyVariable

- job: client
  pool :
    vmImage: 'windows-latest'
  dependsOn: getchangepath
  condition: eq(dependencies.getchangepath.outputs['Myvariable.Client'], 'true')
  steps:
  - powershell: echo 'client job start'
  
- job: api
  pool :
    vmImage: 'windows-latest'
  dependsOn: getchangepath
  condition: eq(dependencies.getchangepath.outputs['Myvariable.Api'], 'true')
  steps:
  - powershell: echo 'api job start'

在上面的 yml 中我有三份工作。在第一份工作getchangepath中,我调用 git get changes PowerShell 任务中的 Rest API 以获取触发构建的更改路径。它还outputs the variables如果路径包含路径 /client/api

作业client和作业api依赖于作业getchangepath,并且将根据作业getchangepath中的输出变量的条件执行

假设我更改了文件夹客户端中的文件并将更改提交到 Azure Repo。然后在作业getchangepath完成后。 MyVariable.Client 将设置为 true。然后作业客户将评估其状况并开始。 Job Api 条件将为 false,并被跳过。

关于azure - azure-pipelines.yml 中的多个单独的触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59584717/

相关文章:

azure - Azure CLI 是否使用 Azure Rest API

尽管权限正确,Azure DevOps Repos 部分仍不可见

azure-devops - dotnet core 2.0 webjob部署-VSTS

.net - 检测该文件受 Microsoft Azure 信息保护保护

Angular 与 Azure Durable Functions => retryWhen 用于 http 响应

azure - 此静态 Web 应用程序具有最大数量的暂存环境

azure - 有没有可能的方法来更改现有 VSTS 扩展的发布者 ID?

azure - 如何在 Azure DevOps yaml 管道中设置环境状态

asp.net-core - 将符号发布到 VSTS

c# - nuget.config 出现问题 - 错误 NU1301 : Unable to load the service index for source https://pkgs. dev.azure.com/[PRIVATE FEED]/nuget/v3/index.json