Eddie
Eddie

Reputation: 621

How to configure condition based pipeline | Azure Pipeline

I have come across a scenario where I want to build source code dependant upon the source directory.

I have 2 languages in the same git repository (dotnet & Python).

I wanted to build the source code using single Azure Pipelines

If the commit is done for both (dotnet & Python) all the task should get executed & if the commit is done for a specific directory only the respective language should get executed.

Please let me know how can I achieve this using -condition or if there are any other alternatives

Below is my 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')

Any help will be appreciated

Upvotes: 0

Views: 465

Answers (1)

psfinaki
psfinaki

Reputation: 2132

I don't think it's possible to do directly what you want. All these task conditions are evaluated at the beginning of the pipeline execution. So if you set pipeline variables in any particular task, even the first one, it's already too late.

If you really want to do this, you probably have to go scripting all the way. So you set the variables in the first script using syntax from here:

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

And then in other scripts you evaluate them like:

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

Something among these lines. That'll probably be quite subtle so maybe it would make sense to reconsider the flow on some higher level but without extra context it's hard to say.

Upvotes: 1

Related Questions