You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/pipelines/process/conditions.md
+61-65Lines changed: 61 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Pipeline conditions
3
3
description: Learn about conditions that Azure Pipelines stages, jobs, or steps can run under, and ways to specify those conditions.
4
4
ms.topic: conceptual
5
5
ms.assetid: C79149CC-6E0D-4A39-B8D1-EB36C8D3AB89
6
-
ms.date: 07/29/2025
6
+
ms.date: 07/31/2025
7
7
monikerRange: '<= azure-devops'
8
8
#customer intent: As an Azure Pipelines user, I want to understand the conditions that pipeline stages, jobs, and steps can run under, so I can configure builds to run under various conditions.
9
9
---
@@ -23,7 +23,7 @@ By default, a pipeline job or stage runs if it doesn't depend on any other job o
23
23
24
24
By default, a step runs if nothing in its job failed yet and the step immediately preceding it completed. For more context on stages, jobs, and steps, see [Key concepts for Azure Pipelines](../get-started/key-pipelines-concepts.md).
25
25
26
-
You can override or customize these default behaviors by setting a stage, job, or step to run even if or only if a previous dependency fails or has another outcome, or by defining[custom conditions](#custom-conditions). In a YAML pipeline definition, you use the `condition` property to specify conditions under which a stage, job, or step can run.
26
+
You can override or customize these default behaviors by setting a stage, job, or step to run even if or only if a previous dependency fails or has another outcome. You can also define[custom conditions](#custom-conditions). In a YAML pipeline definition, you use the `condition` property to specify conditions under which a stage, job, or step can run.
condition: failed() # this job runs only if job1 fails
51
51
```
@@ -57,11 +57,11 @@ If the built-in conditions don't meet your needs, you can specify custom conditi
57
57
The agent evaluates the expression beginning with the innermost function and proceeding outward. The final result is a boolean value that determines whether or not to run the stage, job, or step. For a full guide to the syntax, see [Expressions](expressions.md).
58
58
59
59
> [!IMPORTANT]
60
-
> Conditions are evaluated to determine whether to start a stage, job, or step. Therefore, nothing that's computed at runtime inside a stage, job, or step is available to use within that same stage, job, or step. For example, if you set a variable using a runtime expression with `$[ ]` syntax in a job, you can't use that variable in conditions within that job.
60
+
> Conditions are evaluated to determine whether to start a stage, job, or step. Therefore, nothing computed during the runtime of a stage, job, or step is available to use within that same stage, job, or step. For example, if you set a variable in a job using a runtime expression with `$[ ]` syntax, you can't use that variable in conditions within that job.
61
61
62
62
## Variables in conditions
63
63
64
-
You can set variables and use them in conditions. The following pipeline sets an `isMain` variable and uses it in a condition that runs Stage B only when the build source branch is `main`.
64
+
You can set pipeline variables and use them in conditions. The following pipeline sets an `isMain` variable and uses it in a condition that runs Stage B only when the build source branch is `main`.
65
65
66
66
```yaml
67
67
variables:
@@ -96,9 +96,9 @@ jobs:
96
96
condition: eq(variables.testEmpty, '')
97
97
```
98
98
99
-
### Job output variables used in subsequent job conditions
99
+
### Job output variables used in other job conditions
100
100
101
-
You can make variables available for future jobs in the same stage to specify in conditions. Variables available to future jobs must be marked as [multi-job output variables](variables.md#set-a-multi-job-output-variable) by using `isOutput=true`, as in the following code:
101
+
You can create a variable in a job that other jobs in the same stage can specify in conditions. Variables available to dependent jobs must be marked as [multi-job output variables](variables.md#set-a-multi-job-output-variable) by using `isOutput=true`, as in the following code:
102
102
103
103
```yaml
104
104
jobs:
@@ -117,7 +117,7 @@ jobs:
117
117
118
118
### Step variables used in subsequent step conditions
119
119
120
-
You can create a variable in a step that future steps in the same job can specify in a condition. Variables created from steps are available to future steps by default and don't need to be marked as multi-job output variables.
120
+
You can create a variable in a step that future steps in the same job can specify in conditions. Variables created from steps are available to future steps in the job by default and don't need to be marked as multi-job output variables.
121
121
122
122
Variables created in a step in a job have the following limitations:
123
123
@@ -143,60 +143,6 @@ steps:
143
143
condition: and(succeeded(), eq(variables['doThing'], 'Yes')) # or and(succeeded(), eq(variables.doThing, 'Yes'))
144
144
```
145
145
146
-
## Parameters in conditions
147
-
148
-
Parameter expansion occurs before conditions are evaluated. Therefore, when you declare a parameter in a pipeline, you can embed the parameter inside any conditions in that pipeline. The script step in the following example runs because the preceding step succeeded and `parameters.doThing` is `true`.
The `condition` in the preceding pipeline combines two functions: `succeeded()`and `${{ eq(parameters.doThing, true) }}`. The `succeeded()` function checks if the previous step succeeded. This function also returns `true` if there was no previous step.
162
-
163
-
The `${{ eq(parameters.doThing, true) }}` function checks whether the `doThing` parameter is equal to `true`. Since the pipeline sets the default value for `doThing` as `true`, the condition returns `true` unless the pipeline sets a different value.
164
-
165
-
### Template parameters in conditions
166
-
167
-
When you pass a parameter to a template, you can set the parameter's value in the template or [use templateContext to pass the parameter to the template](template-parameters.md?view=azure-devops&preserve-view=true#use-templatecontext-to-pass-properties-to-templates).
168
-
169
-
The following *parameters.yml* template file declares the `doThing` parameter with a default value of `true`.
170
-
171
-
```yaml
172
-
# parameters.yml
173
-
parameters:
174
-
- name: doThing
175
-
default: true
176
-
type: boolean
177
-
178
-
jobs:
179
-
- job: B
180
-
steps:
181
-
- script: echo I did a thing
182
-
condition: ${{ eq(parameters.doThing, true) }}
183
-
```
184
-
185
-
The following *azure-pipelines.yml* pipeline definition references the job in the *parameters.yml* template file. The output of the pipeline is `I did a thing` because the parameter `doThing` is true.
186
-
187
-
```yaml
188
-
# azure-pipelines.yml
189
-
parameters:
190
-
- name: doThing
191
-
default: true
192
-
type: boolean
193
-
194
-
extends:
195
-
template: parameters.yml
196
-
```
197
-
198
-
For more template parameter examples, see the [Template usage reference](templates.md).
199
-
200
146
## Condition settings for various outcomes
201
147
202
148
The following table shows `condition` settings to produce various desired outcomes.
@@ -208,7 +154,7 @@ The following table shows `condition` settings to produce various desired outcom
208
154
| Run if the source branch isn't `main`, and the parent or preceding stage, job, or step succeeded. | `and(succeeded(), ne(variables['Build.SourceBranch'], 'refs/heads/main'))` |
209
155
| Run for `user` branches if the parent or preceding stage, job, or step succeeded. | `and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/users/'))` |
210
156
| Run for continuous integration (CI) builds, if the parent or preceding stage, job, or step succeeded. | `and(succeeded(), in(variables['Build.Reason'], 'IndividualCI', 'BatchedCI'))` |
211
-
| Run if the build was triggered by a pull request and the parent or preceding stage, job, or step failed. | `and(failed(), eq(variables['Build.Reason'], 'PullRequest'))` |
157
+
| Run if a pull request triggered the build and the parent or preceding stage, job, or step failed. | `and(failed(), eq(variables['Build.Reason'], 'PullRequest'))` |
212
158
| Run for a scheduled build, even if the parent or preceding stage, job, or step failed or was canceled. | `eq(variables['Build.Reason'], 'Schedule')` |
213
159
| Run if the `System.debug` variable is set to `true`, even if the parent or preceding stage, job, or step failed or was canceled. | `eq(variables['System.debug'], true)` |
214
160
@@ -221,7 +167,7 @@ Canceling a build doesn't mean that all its stages, jobs, and steps stop running
221
167
222
168
A stage, job, or step runs whenever its conditions evaluate to `true`. If a condition doesn't account for the state of the task's parent, the task might run even if its parent is canceled. To control whether jobs, stages, or steps run when a build is canceled, include a [job status check function](expressions.md?view=azure-devops&preserve-view=true#job-status-functions) in your conditions.
223
169
224
-
If you cancel a build while it's in the queue stage but not yet running, the entire job is canceled, including all other stages.
170
+
If you cancel a build while it's in the queue stage but not yet running, the entire run is canceled, including all other stages.
225
171
226
172
>[!NOTE]
227
173
>If any of your conditions make it possible for tasks to run even after the build is canceled, specify a value for [cancel timeout](phases.md#timeouts) that provides enough time for the tasks to complete after the run is canceled.
You can use parameters in conditions. Parameter expansion happens before the pipeline runs and replaces values surrounded by `${{ }}` with the literal parameter values. Because parameter expansion occurs before condition evaluation, you can declare a parameter in a pipeline and embed the parameter inside any condition in that pipeline.
301
+
302
+
The `condition` in the following example combines two functions: `succeeded()`and `${{ eq(parameters.doThing, true) }}`. The `succeeded()` function checks if the previous step succeeded. This function returns `true` if there is no previous step.
303
+
304
+
The `${{ eq(parameters.doThing, true) }}` function checks whether the `doThing` parameter is equal to `true`. The script step in the following example runs because there was no previous step and `parameters.doThing` is `true` by default.
When you pass a parameter to a pipeline template, you can set the parameter's value in the template file or [use templateContext to pass the parameter to the template](template-parameters.md?view=azure-devops&preserve-view=true#use-templatecontext-to-pass-properties-to-templates).
320
+
321
+
The following *parameters.yml* template file declares the `doThing` parameter with a default value of `true` and uses the parameter in a job condition.
322
+
323
+
```yaml
324
+
# parameters.yml
325
+
parameters:
326
+
- name: doThing
327
+
default: true
328
+
type: boolean
329
+
330
+
jobs:
331
+
- job: B
332
+
steps:
333
+
- script: echo I did a thing
334
+
condition: ${{ eq(parameters.doThing, true) }}
335
+
```
336
+
337
+
The following *azure-pipelines.yml* pipeline definition references the job in the *parameters.yml* template file. The output of the pipeline is `I did a thing` because the parameter `doThing` is true by default.
338
+
339
+
```yaml
340
+
# azure-pipelines.yml
341
+
342
+
extends:
343
+
template: parameters.yml
344
+
```
345
+
346
+
For more template parameter examples, see the [Template usage reference](templates.md).
347
+
352
348
## FAQ
353
349
354
350
<!-- BEGINSECTION class="md-qanda" -->
@@ -379,4 +375,4 @@ You can experience this issue if a condition configured in a stage doesn't inclu
379
375
380
376
- [Specify jobs in your pipeline](phases.md)
381
377
- [Add stages, dependencies, and conditions](stages.md)
0 commit comments