Skip to content

Commit b1a2184

Browse files
Martin LopesSarah Edwards
andauthored
Explain when to use default variables in a context and an environment (#16650)
* Added "Determining which type of environment variables to use" * Moved into reusable, added to various locations * Added some small edits * Some rewording * Revised example * Removed reusable reference from one location * Update context-and-expression-syntax-for-github-actions.md * Fixed merge conflict * Update environment-variables.md * Apply suggestions from code review Co-authored-by: Sarah Edwards <skedwards88@github.com> * Update using-context-or-environment-variables.md * Clarified locations of context variables * Clarified the distinction between contexts and default environment variables * Moved section up * Clarified that _most_ contexts can be used anywhere Co-authored-by: Sarah Edwards <skedwards88@github.com>
1 parent a90b001 commit b1a2184

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

content/actions/reference/context-and-expression-syntax-for-github-actions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ In order to use property dereference syntax, the property name must:
7575
- start with `a-Z` or `_`.
7676
- be followed by `a-Z` `0-9` `-` or `_`.
7777

78+
#### Determining when to use contexts
79+
80+
{% data reusables.github-actions.using-context-or-environment-variables %}
81+
7882
#### `github` context
7983

8084
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 "[Using environment variables](/actions/automating-your-workflow-with-github-actions/using-environment-variables)."

content/actions/reference/environment-variables.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ We strongly recommend that actions use environment variables to access the files
5757
| `GITHUB_API_URL` | Returns the API URL. For example: `{% data variables.product.api_url_code %}`.
5858
| `GITHUB_GRAPHQL_URL` | Returns the GraphQL API URL. For example: `{% data variables.product.graphql_url_code %}`.
5959

60+
#### Determining when to use default environment variables or contexts
61+
62+
{% data reusables.github-actions.using-context-or-environment-variables %}
63+
6064
### Naming conventions for environment variables
6165

6266
{% note %}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% data variables.product.prodname_actions %} includes a collection of variables called _contexts_ and a similar collection of variables called _default environment variables_. These variables are intended for use at different points in the workflow:
2+
3+
- **Default environment variables:** These variables exist only on the runner that is executing your job. For more information, see "[Default environment variables](/actions/reference/environment-variables#default-environment-variables)."
4+
- **Contexts:** You can use most contexts at any point in your workflow, including when _default environment 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 more information, see "[Contexts](/actions/reference/context-and-expression-syntax-for-github-actions#contexts)."
5+
6+
The following example demonstrates how these different types of environment variables can be used together in a job:
7+
8+
{% raw %}
9+
```yaml
10+
name: CI
11+
on: push
12+
jobs:
13+
prod-check:
14+
if: github.ref == 'refs/heads/main'
15+
runs-on: ubuntu-latest
16+
steps:
17+
- run: echo "Deploying to production server on branch $GITHUB_REF"
18+
```
19+
{% endraw %}
20+
21+
In this example, the `if` statement checks the [`github.ref`](/actions/reference/context-and-expression-syntax-for-github-actions#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/reference/environment-variables#default-environment-variables) environment variable from the runner.

0 commit comments

Comments
 (0)