Skip to content

Commit 1bd90a4

Browse files
authored
Add documentation for conditional composite steps (runs.steps[*].if) (#12378)
1 parent dfc135a commit 1bd90a4

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

content/actions/creating-actions/metadata-syntax-for-github-actions.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ runs:
175175

176176
### `pre-if`
177177

178-
**Optional** Allows you to define conditions for the `pre:` action execution. The `pre:` action will only run if the conditions in `pre-if` are met. If not set, then `pre-if` defaults to `always()`.
178+
**Optional** Allows you to define conditions for the `pre:` action execution. The `pre:` action will only run if the conditions in `pre-if` are met. If not set, then `pre-if` defaults to `always()`. In `pre-if`, status check functions evaluate against the job's status, not the action's own status.
179+
179180
Note that the `step` context is unavailable, as no steps have run yet.
180181

181182
In this example, `cleanup.js` only runs on Linux-based runners:
@@ -202,7 +203,7 @@ The `post:` action always runs by default but you can override this using `post-
202203

203204
### `post-if`
204205

205-
**Optional** Allows you to define conditions for the `post:` action execution. The `post:` action will only run if the conditions in `post-if` are met. If not set, then `post-if` defaults to `always()`.
206+
**Optional** Allows you to define conditions for the `post:` action execution. The `post:` action will only run if the conditions in `post-if` are met. If not set, then `post-if` defaults to `always()`. In `post-if`, status check functions evaluate against the job's status, not the action's own status.
206207

207208
For example, this `cleanup.js` will only run on Linux-based runners:
208209

@@ -265,6 +266,35 @@ For more information, see "[`github context`](/actions/reference/context-and-exp
265266
**Required** The shell where you want to run the command. You can use any of the shells listed [here](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsshell). Required if `run` is set.
266267
{% endif %}
267268

269+
#### `runs.steps[*].if`
270+
271+
**Optional** You can use the `if` conditional to prevent a step from running unless a condition is met. You can use any supported context and expression to create a conditional.
272+
273+
{% data reusables.github-actions.expression-syntax-if %} For more information, see "[Expressions](/actions/learn-github-actions/expressions)."
274+
275+
**Example: Using contexts**
276+
277+
This step only runs when the event type is a `pull_request` and the event action is `unassigned`.
278+
279+
```yaml
280+
steps:
281+
- run: echo This event is a pull request that had an assignee removed.
282+
if: {% raw %}${{ github.event_name == 'pull_request' && github.event.action == 'unassigned' }}{% endraw %}
283+
```
284+
285+
**Example: Using status check functions**
286+
287+
The `my backup step` only runs when the previous step of a composite action fails. For more information, see "[Expressions](/actions/learn-github-actions/expressions#job-status-check-functions)."
288+
289+
```yaml
290+
steps:
291+
- name: My first step
292+
uses: octo-org/action-name@main
293+
- name: My backup step
294+
if: {% raw %}${{ failure() }}{% endraw %}
295+
uses: actions/heroku@1.0.0
296+
```
297+
268298
#### `runs.steps[*].name`
269299

270300
**Optional** The name of the composite step.

content/actions/learn-github-actions/expressions.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ Creates a hash for any `package-lock.json` and `Gemfile.lock` files in the repos
266266

267267
`hashFiles('**/package-lock.json', '**/Gemfile.lock')`
268268

269-
## Job status check functions
269+
## Status check functions
270270

271-
You can use the following status check functions as expressions in `if` conditionals. A default status check of `success()` is applied unless you include one of these functions. For more information about `if` conditionals, see "[Workflow syntax for GitHub Actions](/articles/workflow-syntax-for-github-actions/#jobsjob_idif)."
271+
You can use the following status check functions as expressions in `if` conditionals. A default status check of `success()` is applied unless you include one of these functions. For more information about `if` conditionals, see "[Workflow syntax for GitHub Actions](/articles/workflow-syntax-for-github-actions/#jobsjob_idif)" and "[Metadata syntax for GitHub Composite Actions](/actions/creating-actions/metadata-syntax-for-github-actions/#runsstepsif)".
272272

273273
### success
274274

@@ -316,6 +316,32 @@ steps:
316316
if: {% raw %}${{ failure() }}{% endraw %}
317317
```
318318

319+
### Evaluate Status Explicitly
320+
321+
Instead of using one of the methods above, you can evaluate the status of the job or composite action that is executing the step directly:
322+
323+
#### Example for workflow step
324+
325+
```yaml
326+
steps:
327+
...
328+
- name: The job has failed
329+
if: {% raw %}${{ job.status == 'failure' }}{% endraw %}
330+
```
331+
332+
This is the same as using `if: failure()` in a job step.
333+
334+
#### Example for composite action step
335+
336+
```yaml
337+
steps:
338+
...
339+
- name: The composite action has failed
340+
if: {% raw %}${{ github.action_status == 'failure' }}{% endraw %}
341+
```
342+
343+
This is the same as using `if: failure()` in a composite action step.
344+
319345
## Object filters
320346

321347
You can use the `*` syntax to apply a filter and select matching items in a collection.

0 commit comments

Comments
 (0)