Defining environment variables for a single workflow
To set a custom environment variable for a single workflow, you can define it using the env
key in the workflow file. The scope of a custom variable set by this method is limited to the element in which it is defined. You can define variables that are scoped for:
- The entire workflow, by using
env
at the top level of the workflow file. - The contents of a job within a workflow, by using
jobs.<job_id>.env
. - A specific step within a job, by using
jobs.<job_id>.steps[*].env
.
name: Greeting on variable day on: workflow_dispatch env: DAY_OF_WEEK: Monday jobs: greeting_job: runs-on: ubuntu-latest env: Greeting: Hello steps: - name: "Say Hello Mona it's Monday" run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!" env: First_Name: Mona
name: Greeting on variable day
on:
workflow_dispatch
env:
DAY_OF_WEEK: Monday
jobs:
greeting_job:
runs-on: ubuntu-latest
env:
Greeting: Hello
steps:
- name: "Say Hello Mona it's Monday"
run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
env:
First_Name: Mona
You can access env
variable values using runner environment variables or using contexts. The example above shows three custom variables being used as runner environment variables in an echo
command: $DAY_OF_WEEK
, $Greeting
, and $First_Name
. The values for these variables are set, and scoped, at the workflow, job, and step level respectively. The interpolation of these variables happens on the runner.
The commands in the run
steps of a workflow, or a referenced action, are processed by the shell you are using on the runner. The instructions in the other parts of a workflow are processed by GitHub Actions and are not sent to the runner. You can use either runner environment variables or contexts in run
steps, but in the parts of a workflow that are not sent to the runner you must use contexts to access variable values. For more information, see Using contexts to access variable values.
Because runner environment variable interpolation is done after a workflow job is sent to a runner machine, you must use the appropriate syntax for the shell that's used on the runner. In this example, the workflow specifies ubuntu-latest
. By default, Linux runners use the bash shell, so you must use the syntax $NAME
. By default, Windows runners use PowerShell, so you would use the syntax $env:NAME
. For more information about shells, see Workflow syntax for GitHub Actions.
Defining configuration variables for multiple workflows
You can create configuration variables for use across multiple workflows, and can define them at either the organization, repository, or environment level.
For example, you can use configuration variables to set default values for parameters passed to build tools at an organization level, but then allow repository owners to override these parameters on a case-by-case basis.
When you define configuration variables, they are automatically available in the vars
context. For more information, see Using the vars
context to access configuration variable values.
Creating configuration variables for a repository
Um Geheimnisse oder Variablen auf GitHub für ein Repository eines persönlichen Kontos zu erstellen, müssen Sie Repositorybesitzer*in sein. Um Geheimnisse oder Variablen auf GitHub für ein Organisationsrepository zu erstellen, müssen Sie über admin
-Zugriff verfügen. Um Geheimnisse oder Variablen für ein Repository eines persönlichen Kontos oder einer Organisation über die REST-API zu erstellen, müssen Sie über Projektmitarbeiterzugriff verfügen.
-
Navigieren Sie auf GitHub zur Hauptseite des Repositorys.
-
Wähle unter dem Namen deines Repositorys die Option Einstellungen aus. Wenn die Registerkarte „Einstellungen“ nicht angezeigt wird, wähle im Dropdownmenü die Option Einstellungen aus.
-
Wählen Sie im Abschnitt „Sicherheit“ in der Randleiste Geheimnisse und Variablen und klicken Sie dann auf Aktionen.
-
Klicke auf die Registerkarte Variablen.
-
Click New repository variable.
-
Gib im Feld Name einen Namen für deine Variable ein.
-
Gib im Feld Wert den Wert für deine Variable ein.
-
Click Add variable.
Creating configuration variables for an environment
Um Geheimnisse oder Variablen für eine Umgebung in einem Repository eines persönlichen Kontos zu erstellen, müssen Sie Besitzer des Repositorys sein. Für das Erstellen von Geheimnissen oder Variablen für eine Umgebung in einem Organisationsrepository benötigen Sie admin
-Zugriff. Weitere Informationen zu Umgebungen findest du unter Managing environments for deployment.
-
Navigieren Sie auf GitHub zur Hauptseite des Repositorys.
-
Wähle unter dem Namen deines Repositorys die Option Einstellungen aus. Wenn die Registerkarte „Einstellungen“ nicht angezeigt wird, wähle im Dropdownmenü die Option Einstellungen aus.
-
Klicke auf der linken Randleiste auf Umgebungen.
-
Click on the environment that you want to add a variable to.
-
Under Environment variables, click Add variable.
-
Gib im Feld Name einen Namen für deine Variable ein.
-
Gib im Feld Wert den Wert für deine Variable ein.
-
Click Add variable.
Creating configuration variables for an organization
Beim Erstellen eines Geheimnisses oder einer Variable in einer Organisation kannst du mit einer Richtlinie den Zugriff jeweils nach Repository einschränken. Du kannst beispielsweise allen Repositorys Zugriff gewähren oder nur private Repositorys oder eine angegebene Liste von Repositorys zulassen.
Organisationsbesitzerinnen und Benutzerinnen mit den Berechtigungen „Organisationsaktionenvariablen verwalten“ oder „Organisationsaktioneneheimnisse verwalten“ können Geheimnisse oder Variablen auf Organisationsebene erstellen.
Weitere Informationen findest du unter Informationen zu benutzerdefinierten Organisationsrollen.
-
Navigieren Sie auf GitHub zur Hauptseite der Organisation.
-
Klicke unter deinem Organisationsnamen auf die Option Einstellungen. Wenn die Registerkarte „Einstellungen“ nicht angezeigt wird, wähle im Dropdownmenü die Option Einstellungen aus.
-
Wählen Sie im Abschnitt „Sicherheit“ in der Randleiste Geheimnisse und Variablen und klicken Sie dann auf Aktionen.
-
Klicke auf die Registerkarte Variablen.
Hinweis
Auf der Seite „Actions secrets and variables“ werden je nach deinen Berechtigungen möglicherweise keine unterschiedlichen Registerkarten für Geheimnisse und Variablen angezeigt. Organisationsbesitzerinnen und Benutzerinnen mit den Berechtigungen „Organisationsaktionenvariablen verwalten“ und „Organisationsaktionengeheimnisse verwalten“ sehen die Registerkarten Variablen und Geheimnisse. Weitere Informationen finden Sie unter Informationen zu benutzerdefinierten Organisationsrollen.
-
Click New organization variable.
-
Gib im Feld Name einen Namen für deine Variable ein.
-
Gib im Feld Wert den Wert für deine Variable ein.
-
From the Repository access dropdown list, choose an access policy.
-
Click Add variable.
Using contexts to access variable values
Kontexte sind eine Möglichkeit, auf Informationen zu Workflowausführungen, Variablen, Runnerumgebungen, Aufträge und Schritten zuzugreifen. For more information, see Contexts reference. There are many other contexts that you can use for a variety of purposes in your workflows. For details of where you can use specific contexts within a workflow, see Contexts reference.
You can access environment variable values using the env
context and configuration variable values using the vars
context.
Using the env
context to access environment variable values
In addition to runner environment variables, GitHub Actions allows you to set and read env
key values using contexts. Environment variables and contexts are intended for use at different points in the workflow.
The run
steps in a workflow, or in a referenced action, are processed by a runner. As a result, you can use runner environment variables here, using the appropriate syntax for the shell you are using on the runner - for example, $NAME
for the bash shell on a Linux runner, or $env:NAME
for PowerShell on a Windows runner. In most cases you can also use contexts, with the syntax ${{ CONTEXT.PROPERTY }}
, to access the same value. The difference is that the context will be interpolated and replaced by a string before the job is sent to a runner.
However, you cannot use runner environment variables in parts of a workflow that are processed by GitHub Actions and are not sent to the runner. Instead, you must use contexts. For example, an if
conditional, which determines whether a job or step is sent to the runner, is always processed by GitHub Actions. You must therefore use a context in an if
conditional statement to access the value of an variable.
name: Conditional env variable on: workflow_dispatch env: DAY_OF_WEEK: Monday jobs: greeting_job: runs-on: ubuntu-latest env: Greeting: Hello steps: - name: "Say Hello Mona it's Monday" if: ${{ env.DAY_OF_WEEK == 'Monday' }} run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!" env: First_Name: Mona
name: Conditional env variable
on: workflow_dispatch
env:
DAY_OF_WEEK: Monday
jobs:
greeting_job:
runs-on: ubuntu-latest
env:
Greeting: Hello
steps:
- name: "Say Hello Mona it's Monday"
if: ${{ env.DAY_OF_WEEK == 'Monday' }}
run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
env:
First_Name: Mona
In this modification of the earlier example, we've introduced an if
conditional. The workflow step is now only run if DAY_OF_WEEK
is set to "Monday". We access this value from the if
conditional statement by using the env
context. The env
context is not required for the variables referenced within the run
command. They are referenced as runner environment variables and are interpolated after the job is received by the runner. We could, however, have chosen to interpolate those variables before sending the job to the runner, by using contexts. The resulting output would be the same.
run: echo "${{ env.Greeting }} ${{ env.First_Name }}. Today is ${{ env.DAY_OF_WEEK }}!"
Hinweis
Contexts are usually denoted using the dollar sign and curly braces, as ${{ context.property }}
. In an if
conditional, the ${{
and }}
are optional, but if you use them they must enclose the entire comparison statement, as shown above.
Warnung
Bei der Erstellung von Workflows und Aktionen sollten Sie immer bedenken, ob Ihr Code nicht vertrauenswürdige Eingaben von möglichen Eindringlingen ausführen könnte. Bestimmte Kontexte sollten als nicht vertrauenswürdige Eingaben behandelt werden, da ein Angreifer seine eigenen schädlichen Inhalte einfügen könnte. Weitere Informationen finden Sie unter Secure use reference.
Using the vars
context to access configuration variable values
Configuration variables can be accessed across the workflow using vars
context. For more information, see Contexts reference.
Wenn keine Konfigurationsvariable festgelegt wurde, ist der Rückgabewert eines Kontexts, der auf die Variable verweist, eine leere Zeichenfolge.
Das folgende Beispiel zeigt die Verwendung von Konfigurationsvariablen mit dem Kontext vars
in einem Workflow. Jede der folgenden Konfigurationsvariablen wurde auf Repository-, Organisations- oder Umgebungsebene definiert.
on: workflow_dispatch: env: # Setting an environment variable with the value of a configuration variable env_var: ${{ vars.ENV_CONTEXT_VAR }} jobs: display-variables: name: ${{ vars.JOB_NAME }} # You can use configuration variables with the `vars` context for dynamic jobs if: ${{ vars.USE_VARIABLES == 'true' }} runs-on: ${{ vars.RUNNER }} environment: ${{ vars.ENVIRONMENT_STAGE }} steps: - name: Use variables run: | echo "repository variable : $REPOSITORY_VAR" echo "organization variable : $ORGANIZATION_VAR" echo "overridden variable : $OVERRIDE_VAR" echo "variable from shell environment : $env_var" env: REPOSITORY_VAR: ${{ vars.REPOSITORY_VAR }} ORGANIZATION_VAR: ${{ vars.ORGANIZATION_VAR }} OVERRIDE_VAR: ${{ vars.OVERRIDE_VAR }} - name: ${{ vars.HELLO_WORLD_STEP }} if: ${{ vars.HELLO_WORLD_ENABLED == 'true' }} uses: actions/hello-world-javascript-action@main with: who-to-greet: ${{ vars.GREET_NAME }}
on:
workflow_dispatch:
env:
# Setting an environment variable with the value of a configuration variable
env_var: ${{ vars.ENV_CONTEXT_VAR }}
jobs:
display-variables:
name: ${{ vars.JOB_NAME }}
# You can use configuration variables with the `vars` context for dynamic jobs
if: ${{ vars.USE_VARIABLES == 'true' }}
runs-on: ${{ vars.RUNNER }}
environment: ${{ vars.ENVIRONMENT_STAGE }}
steps:
- name: Use variables
run: |
echo "repository variable : $REPOSITORY_VAR"
echo "organization variable : $ORGANIZATION_VAR"
echo "overridden variable : $OVERRIDE_VAR"
echo "variable from shell environment : $env_var"
env:
REPOSITORY_VAR: ${{ vars.REPOSITORY_VAR }}
ORGANIZATION_VAR: ${{ vars.ORGANIZATION_VAR }}
OVERRIDE_VAR: ${{ vars.OVERRIDE_VAR }}
- name: ${{ vars.HELLO_WORLD_STEP }}
if: ${{ vars.HELLO_WORLD_ENABLED == 'true' }}
uses: actions/hello-world-javascript-action@main
with:
who-to-greet: ${{ vars.GREET_NAME }}
Detecting the operating system
You can write a single workflow file that can be used for different operating systems by using the RUNNER_OS
default environment variable and the corresponding context property ${{ runner.os }}
. For example, the following workflow could be run successfully if you changed the operating system from macos-latest
to windows-latest
without having to alter the syntax of the environment variables, which differs depending on the shell being used by the runner.
on: workflow_dispatch jobs: if-Windows-else: runs-on: macos-latest steps: - name: condition 1 if: runner.os == 'Windows' run: echo "The operating system on the runner is $env:RUNNER_OS." - name: condition 2 if: runner.os != 'Windows' run: echo "The operating system on the runner is not Windows, it's $RUNNER_OS."
on: workflow_dispatch
jobs:
if-Windows-else:
runs-on: macos-latest
steps:
- name: condition 1
if: runner.os == 'Windows'
run: echo "The operating system on the runner is $env:RUNNER_OS."
- name: condition 2
if: runner.os != 'Windows'
run: echo "The operating system on the runner is not Windows, it's $RUNNER_OS."
In this example, the two if
statements check the os
property of the runner
context to determine the operating system of the runner. if
conditionals are processed by GitHub Actions, and only steps where the check resolves as true
are sent to the runner. Here one of the checks will always be true
and the other false
, so only one of these steps is sent to the runner. Once the job is sent to the runner, the step is executed and the environment variable in the echo
command is interpolated using the appropriate syntax ($env:NAME
for PowerShell on Windows, and $NAME
for bash and sh on Linux and macOS). In this example, the statement runs-on: macos-latest
means that the second step will be run.
Passing values between steps and jobs in a workflow
If you generate a value in one step of a job, you can use the value in subsequent steps of the same job by assigning the value to an existing or new environment variable and then writing this to the GITHUB_ENV
environment file. The environment file can be used directly by an action, or from a shell command in the workflow file by using the run
keyword. For more information, see Workflow commands for GitHub Actions.
If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output. You can then reference this job output from a step in another job. For more information, see Workflow syntax for GitHub Actions.
Next steps
For reference information, see Variables reference.