Skip to content

Commit ffe3da2

Browse files
MNFlucascostiMartin Lopes
authored
Added if conditional environment variable example (#2485)
Co-authored-by: Lucas Costi <lucascosti@users.noreply.github.com> Co-authored-by: Martin Lopes <martin389@github.com>
1 parent 88af77b commit ffe3da2

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

content/actions/reference/environment-variables.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,25 @@ versions:
2121
To set custom environment variables, you need to specify the variables in the workflow file. You can define environment variables for a step, job, or entire workflow using the [`jobs.<job_id>.steps[*].env`](/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepsenv), [`jobs.<job_id>.env`](/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idenv), and [`env`](/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#env) keywords. For more information, see "[Workflow syntax for {% data variables.product.prodname_dotcom %}](/articles/workflow-syntax-for-github-actions/#jobsjob_idstepsenv)."
2222

2323
```yaml
24-
steps:
25-
- name: Hello world
26-
run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
24+
jobs:
25+
weekday_job:
26+
runs-on: ubuntu-latest
2727
env:
28-
FIRST_NAME: Mona
29-
middle_name: The
30-
Last_Name: Octocat
28+
DAY_OF_WEEK: Mon
29+
steps:
30+
- name: "Hello world when it's Monday"
31+
if: env.DAY_OF_WEEK == 'Mon'
32+
run: echo "Hello $FIRST_NAME $middle_name $Last_Name, today is Monday!"
33+
env:
34+
FIRST_NAME: Mona
35+
middle_name: The
36+
Last_Name: Octocat
3137
```
3238
39+
To use the value of an environment variable in a workflow file, you should use the [`env` context](/actions/reference/context-and-expression-syntax-for-github-actions#env-context). If you want to use the value of an environment variable inside a runner, you can use the runner operating system's normal method for reading environment variables.
40+
41+
If you use the workflow file's `run` key to read environment variables from within the runner operating system (as shown in the example above), the variable is substituted in the runner operating system after the job is sent to the runner. For other parts of a workflow file, you must use the `env` context to read environment variables; this is because workflow keys (such as `if`) require the variable to be substituted during workflow processing before it is sent to the runner.
42+
3343
You can also use the {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.22" %}`GITHUB_ENV` environment file{% else %} `set-env` workflow command{% endif %} to set an environment variable that the following steps in a workflow can use. The {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.22" %}environment file{% else %} `set-env` command{% endif %} can be used directly by an action or as a shell command in a workflow file using the `run` keyword. For more information, see "[Workflow commands for {% data variables.product.prodname_actions %}](/actions/reference/workflow-commands-for-github-actions/#setting-an-environment-variable)."
3444

3545
### Default environment variables

0 commit comments

Comments
 (0)