Skip to content

Commit 63679c4

Browse files
jc-clarksubatoi
andauthored
EDI - Actions contexts (#56465)
Co-authored-by: Ben Ahmady <32935794+subatoi@users.noreply.github.com>
1 parent a0f20c7 commit 63679c4

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Contexts
3+
shortTitle: Contexts
4+
intro: 'Learn about contexts in {% data variables.product.prodname_actions %}.'
5+
versions:
6+
fpt: '*'
7+
ghes: '*'
8+
ghec: '*'
9+
type: overview
10+
topics:
11+
- Actions
12+
- Workflows
13+
---
14+
15+
## About contexts
16+
17+
{% data reusables.actions.actions-contexts-about-description %} Each context is an object that contains properties, which can be strings or other objects.
18+
19+
{% data reusables.actions.context-contents %} For example, the `matrix` context is only populated for jobs in a [matrix](/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix).
20+
21+
You can access contexts using the expression syntax. For more information, see [AUTOTITLE](/actions/learn-github-actions/expressions).
22+
23+
{% raw %}
24+
`${{ <context> }}`
25+
{% endraw %}
26+
27+
{% data reusables.actions.context-injection-warning %}
28+
29+
## Determining when to use contexts
30+
31+
{% data variables.product.prodname_actions %} includes a collection of variables called _contexts_ and a similar collection of variables called _default variables_. These variables are intended for use at different points in the workflow:
32+
33+
* **Default environment variables:** These environment variables exist only on the runner that is executing your job. For more information, see [AUTOTITLE](/actions/learn-github-actions/variables#default-environment-variables).
34+
* **Contexts:** You can use most contexts at any point in your workflow, including when _default variables_ would be unavailable. For example, you can use contexts with expressions to perform initial processing before the job is routed to a runner for execution; this allows you to use a context with the conditional `if` keyword to determine whether a step should run. Once the job is running, you can also retrieve context variables from the runner that is executing the job, such as `runner.os`. For details of where you can use various contexts within a workflow, see [AUTOTITLE](/actions/reference/accessing-contextual-information-about-workflow-runs#context-availability).
35+
36+
The following example demonstrates how these different types of variables can be used together in a job:
37+
38+
{% raw %}
39+
40+
```yaml copy
41+
name: CI
42+
on: push
43+
jobs:
44+
prod-check:
45+
if: ${{ github.ref == 'refs/heads/main' }}
46+
runs-on: ubuntu-latest
47+
steps:
48+
- run: echo "Deploying to production server on branch $GITHUB_REF"
49+
```
50+
51+
{% endraw %}
52+
53+
In this example, the `if` statement checks the [`github.ref`](/actions/learn-github-actions/contexts#github-context) context to determine the current branch name; if the name is `refs/heads/main`, then the subsequent steps are executed. The `if` check is processed by {% data variables.product.prodname_actions %}, and the job is only sent to the runner if the result is `true`. Once the job is sent to the runner, the step is executed and refers to the [`$GITHUB_REF`](/actions/learn-github-actions/variables#default-environment-variables) variable from the runner.

content/actions/concepts/workflows-and-actions/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ children:
1010
- /variables
1111
- /avoiding-duplication
1212
- /about-custom-actions
13+
- /contexts
1314
- /about-monitoring-workflows
1415
- /notifications-for-workflow-runs
1516
- /about-troubleshooting-workflows
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Accessing contextual information about workflow runs
3-
shortTitle: Contexts
4-
intro: You can access context information in workflows and actions.
2+
title: Contexts reference
3+
shortTitle: Contexts reference
4+
intro: 'Find information about contexts available in {% data variables.product.prodname_actions %} workflows, including available properties, access methods, and usage examples.'
55
redirect_from:
66
- /articles/contexts-and-expression-syntax-for-github-actions
77
- /github/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions
@@ -11,27 +11,14 @@ redirect_from:
1111
- /actions/learn-github-actions/contexts
1212
- /actions/writing-workflows/choosing-what-your-workflow-does/contexts
1313
- /actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs
14+
- /actions/reference/accessing-contextual-information-about-workflow-runs
1415
versions:
1516
fpt: '*'
1617
ghes: '*'
1718
ghec: '*'
1819
---
1920

20-
{% data reusables.actions.enterprise-github-hosted-runners %}
21-
22-
## About contexts
23-
24-
{% data reusables.actions.actions-contexts-about-description %} Each context is an object that contains properties, which can be strings or other objects.
25-
26-
{% data reusables.actions.context-contents %} For example, the `matrix` context is only populated for jobs in a [matrix](/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix).
27-
28-
You can access contexts using the expression syntax. For more information, see [AUTOTITLE](/actions/learn-github-actions/expressions).
29-
30-
{% raw %}
31-
`${{ <context> }}`
32-
{% endraw %}
33-
34-
{% data reusables.actions.context-injection-warning %}
21+
## Available contexts
3522

3623
| Context name | Type | Description |
3724
|---------------|------|-------------|
@@ -174,7 +161,7 @@ jobs:
174161

175162
## `github` context
176163

177-
The `github` context contains information about the workflow run and the event that triggered the run. You can also read most of the `github` context data in environment variables. For more information about environment variables, see [AUTOTITLE](/actions/learn-github-actions/variables).
164+
The `github` context contains information about the workflow run and the event that triggered the run. You can read most of the `github` context data in environment variables. For more information about environment variables, see [AUTOTITLE](/actions/learn-github-actions/variables).
178165

179166
{% data reusables.actions.github-context-warning %}
180167
{% data reusables.actions.context-injection-warning %}
@@ -877,3 +864,7 @@ jobs:
877864
```
878865

879866
{% endraw %}
867+
868+
## Further reading
869+
870+
* [AUTOTITLE](/actions/concepts/workflows-and-actions/contexts)

content/actions/reference/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ children:
1212
- /workflow-commands-for-github-actions
1313
- /variables-reference
1414
- /evaluate-expressions-in-workflows-and-actions
15-
- /accessing-contextual-information-about-workflow-runs
15+
- /contexts-reference
1616
- /metadata-syntax-for-github-actions
1717
- /actions-limits
1818
- /dockerfile-support-for-github-actions

0 commit comments

Comments
 (0)