* 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>
2.0 KiB
{% 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:
- Default environment variables: These variables exist only on the runner that is executing your job. For more information, see "Default environment variables."
- 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
ifkeyword 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 asrunner.os. For more information, see "Contexts."
The following example demonstrates how these different types of environment variables can be used together in a job:
{% raw %}
name: CI
on: push
jobs:
prod-check:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to production server on branch $GITHUB_REF"
{% endraw %}
In this example, the if statement checks the github.ref 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 environment variable from the runner.