【问题标题】:How to run one pipeline conditionally from another pipeline in Azure Devops?如何从 Azure Devops 中的另一个管道有条件地运行一个管道?
【发布时间】:2020-04-25 05:05:06
【问题描述】:

好的,所以我们正在研究微服务架构,并尝试在 azure devOps 上部署代码。我们实际上只想在一个项目发生变化时触发一个管道。我们正在使用 monorepo 架构。

这就是我当前为项目特定的 build.yaml 添加条件的方式

name: $(TeamProject)_$(Build.DefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)
steps:
  # for PowerShell Core
  - pwsh: ./build.ps1
  # Restore sms-messaging micro service projects
  - task: DotNetCoreCLI@1
    displayName: Run dotnet restore
    inputs:
      command: "restore"
      projects: sms-messaging/src/**/*.csproj
    condition:  and(succeeded(), eq(variables['SmsMessaging'], 'True'))

condition 包含之前执行的 powershell 脚本中的变量 SmsMessaging。

$files=$(git diff HEAD HEAD~ --name-only)
$temp=$files -split ' '
$count=$temp.Length
echo "Total changed $count files"
For ($i=0; $i -lt $temp.Length; $i++)
{
  $name=$temp[$i]
  echo "this is $name file"
  if ($name -like "sms-messaging/*")
  {
    echo "changes in sms-messaging"
    Write-Host "##vso[task.setvariable variable=SmsMessaging]True"
  }
}

所以实际的问题是,在推送到 repo 时。所有管道触发。由于添加的条件,任务确实会被跳过。但我们实际上并不想触发所有管道。

为此,我创建了一个包含 powerShell 脚本和变量的主管道。并将有条件地触发其他管道。

但我无法向此 yaml 添加“条件”。

resources:
  pipelines:
  - pipeline: SmsMessaging
    project: SmsMessaging
    source: SmsMessaging
    trigger:
      branches:
        include:
        - master
      (i want to add condition here maybe if thats possible ? and trigger different pipelines from here. Or if there is another approach)

这是正确的方法吗?任何帮助表示赞赏。谢谢

编辑:我还尝试通过 Powershell 登录并触发管道。

Invoke-AzDataFactoryV2Pipeline -ResourceGroupName "RGName" -DataFactoryName "FactoryName" -PipelineName "PipelineName"

但它不允许我运行管道说需要组织 ID。

【问题讨论】:

    标签: azure azure-devops microservices


    【解决方案1】:

    您可以将您的方法与路径过滤器结合使用。您可以使用 monorepo 架构中的路径触发器来触发构建,例如:

    trigger:
      branches:
        include:
        - master
      paths:
        include:
        - path/to/SmsMessaging/*
    

    如果这没有帮助,请查看extension

    【讨论】:

    • 这只会触发修改文件的构建?我会试试的。谢谢!
    • 是的,只有指定路径下的文件发生变化才会触发构建。
    • 谢谢!这行得通。我首先为所有管道分离了分支,现在我已经根据路径添加了触发器。
    • 太棒了。很高兴知道。 :)
    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 2020-10-13
    • 2022-07-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多