Taul
Taul

Reputation: 2273

How to conditionally run a build agent job with a pipeline variable?

In Azure DevOps pipelines there's an option to conditionally run a task based on a pipeline variable. This is handled under the Run this task > Custom conditions field and it uses the syntax:

eq(variables['VarName'], 'Desired Value')

An agent job has a similar field for conditional execution under Run this job > Custom condition using variable expressions.

However, when I use the same syntax as a conditional task the result always evaluates to 'false'.

So how can I conditionally run an agent job?

Screenshots:

enter image description here enter image description here enter image description here enter image description here enter image description here

Upvotes: 0

Views: 5043

Answers (3)

Shadee
Shadee

Reputation: 21

Try this one: https://stefanstranger.github.io/2019/06/26/PassingVariablesfromStagetoStage/

This one gives you to pass variables from one stage/job to another stage/job in the same release pipeline. I tried and it's working fine.

Also to run this you need to give some permissions for release pipeline. To allow for updating the Release Definition during the Release you need to configure the Release Permission Manage releases for the Project Collection Build Service.

Upvotes: 0

Filip Skakun
Filip Skakun

Reputation: 31724

Something like this worked for me:

- job: Job1
  steps:
  - powershell: |
      if (some condition)
      {
          Write-Host ("##vso[task.setvariable variable=RunJob2;isOutput=true]True")
      }

    name: ScriptStep
- job: Job2
  dependsOn: Create_Build_Matrix
  condition: and(succeeded(), eq(dependencies.Job1.outputs['ScriptStep.RunJob2'], 'True'))

Upvotes: 4

Taul
Taul

Reputation: 2273

I discovered the answer. Unfortunately, it is not possible to conditionally run an agent job with a variable that is modified during build execution.

From the Azure DevOps Pipeline documentation under Pipeline Variables:

To define or modify a variable from a script, use the task.setvariable logging command. Note that the updated variable value is scoped to the job being executed, and does not flow across jobs or stages.

Upvotes: 1

Related Questions