首页 > 解决方案 > 如何配置基于条件的管道 | Azure 管道

问题描述

我遇到了一个场景,我想根据源目录构建源代码。

我在同一个 git 存储库(dotnet 和 Python)中有 2 种语言。

我想使用单个 Azure Pipelines 构建源代码

如果对两者(dotnet 和 Python)都完成了提交,则所有任务都应执行,如果对特定目录完成提交,则仅应执行相应的语言。

请让我知道如何使用-condition或是否有其他替代方法来实现这一目标

下面是我的 azure-pipelines.yml

#Trigger and pool name configuration
variables:
  name: files 
steps:
- script: $(files) = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)"
  displayName: 'display Last Committed Files'
  ## Here I am getting changed files
  ##Dotnet/Server.cs
  ##Py/Hello.py

- task: PythonScript@0   ## It should only get called when there are changes in /Py
  inputs:
    scriptSource: 'inline'
    script: 'print(''Hello, FromPython!'')'
    condition: eq('${{ variables.files }}', '/Py')  
- task: DotNetCoreCLI@2  ## It should only get called when there are changes in /Dotnet
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    condition: eq('${{ variables.files }}', '/Dotnet')

任何帮助将不胜感激

标签: azureazure-devopsazure-pipelines-yamlazure-pipelines-tasks

解决方案


我认为不可能直接做你想做的事。所有这些任务条件都在管道执行开始时进行评估。因此,如果您在任何特定任务中设置管道变量,即使是第一个任务,也为时已晚。

如果您真的想这样做,您可能必须一直编写脚本。因此,您使用此处的语法在第一个脚本中设置变量:

(if there are C# files) echo '##vso[task.setvariable variable=DotNet]true'
(if there are Py files) echo '##vso[task.setvariable variable=Python]true'

然后在其他脚本中评估它们,如下所示:

if $(DotNet) = 'true' then dotnet build

这些线之间的东西。这可能会非常微妙,因此在更高级别重新考虑流程可能是有意义的,但如果没有额外的上下文,很难说。


推荐阅读