Anouar BAKRI
Anouar BAKRI

Reputation: 335

Condition on pipeline Stage execution not working

I need to set a condition on executing a stage in my yml file(build pipeline).

The condition is that the repo name must contain "application" keyword, if not the stage need to be ignored;

my code is like this

- stage: deploy
  condition: contains(variables['Build.Repository.Name'],'application')
  dependsOn: build
  jobs:
   - template: deploy-snapshot-jobs.yml@pipelines-templates

It don't work like this, I think the interpreter don't replace variables['Build.Repository.Name'] with the repository name because when I use literal value of repo name it work fine

any help how can get this working guys ?

Upvotes: 0

Views: 1959

Answers (1)

Frank Wang-MSFT
Frank Wang-MSFT

Reputation: 1407

You can change the confition to effect your task not stage. For example, first I set the YAML file as below. enter image description here

The Deploy stage will not queue and you can see some differences in the log. enter image description here

The Deploy stage doesn't have the preparation stage before task. If the stage didn't checkout the git source, the Build.Repository.Name value will be null. That is why your deploy stage will not queue. Because the different stage meaning different environment and agent, so the agent-scoped variable value need to be obtained again in different stages. The Build.Repository.Name is an agent-scoped variable, you can refe to https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables.

When I edit the YAML file as below. enter image description here

The Deploy stage will run as expected. enter image description here

And if the repository name is not matched the condition, the error log is as below. enter image description here

Upvotes: 1

Related Questions