Scopes of Environment Variables#
As you might have already found out, there are multiple places where environment variables can be defined. In this section, we will explore the behavior of environment variables in different contexts.
Overview#
Scope |
Definition Location |
Availability |
Variable Priority |
Variable Access |
---|---|---|---|---|
Global Environment |
Top-level |
Entire configuration file |
Lowest priority, can be overwritten by other levels |
|
Autopilot Environment |
|
Only in the autopilot’s |
Overrides global, can be overwritten by check level |
|
Check Environment |
|
Only in the autopilot’s |
Overrides global and autopilot levels |
|
Hint
See the Autopilots section for an explanation
of the differences between $NAME
, ${NAME}
, and ${{ env.NAME }}
.
Global Variables#
Environment variables that are defined in the top-level env
section of the
qg-config.yaml
file are available globally in the entire configuration
file.
Those variables will be available as environment variables in each
autopilot script and can be used in the same way as any other
environment variables in a shell script via $<name>
, ${<name>}
or ${{ env.<name> }}
.
However, keep in mind that variables set at this global level are the least
prioritized and can be overwritten by variables from other levels.
Scope |
Description |
---|---|
Definition Location |
Top-level |
Availability |
Global, accessible throughout the entire configuration file |
Variable Priority |
Lowest priority, can be overwritten by variables from other levels |
Variable Access |
Accessible in autopilot scripts as |
[...]
env: # global environment variables
MY_VARIABLE: "my_value"
[...]
autopilots:
[...]
my-autopilot:
run: |
echo $MY_VARIABLE # prints "my_value"
[...]
Autopilot Variables#
Environment variables that are defined in the env
section of an
autopilot overwrite environment variables from the global scope.
Those variables will be available as environment variables during execution of
the related autopilot and can be used in the same way as any other
environment variables in a shell script via $<name>
, ${<name>}
or ${{ env.<name> }}
.
Still, keep in mind that environment variables from the autopilot level override
variables from the global level, but can be overwritten by environment variables
from the check level.
Scope |
Description |
---|---|
Definition Location |
|
Availability |
Only in the autopilot’s |
Variable Priority |
Overrides global environment variables, but can be overwritten by check level variables |
Variable Access |
Accessible in autopilot scripts as |
env: # global environment variables
GLOBAL_VARIABLE: "my_value"
OVERRIDDEN_VARIABLE: "my_other_value"
[...]
autopilots:
[...]
my-autopilot:
run: |
echo $GLOBAL_VARIABLE # prints "my_value"
echo ${OVERRIDDEN_VARIABLE} # print "autopilot_overrides_global"
env: # autopilot environment variables
OVERRIDDEN_VARIABLE: "autopilot_overrides_global"
Check Variables#
Environment variables that are defined in the env
section of a check
automation
overwrite environment variables from the global and
autopilot scope.
Those variables will be available as environment variables during execution of
the autopilot in reference to the related check
and can be used in the
same way as any other environment variables in a shell script via $<name>
,
${<name>}
, or ${{ env.<name> }}
.
Still, keep in mind that environment variables from the check level override
variables from the global and autopilot level.
Scope |
Description |
---|---|
Definition Location |
|
Availability |
Limited to the execution of the autopilot associated with the check |
Variable Priority |
Overrides global and autopilot environment variables |
Variable Access |
Accessible in autopilot scripts as |
Tip
By setting env
variables on check level, you can parametrize the autopilot used in the check. This is useful if you want to use the same autopilot multiple times with different parameters.
autopilots:
my-autopilot: # is called for both checks, but with different FILE value
run: |
echo Executing check for file ${FILE}...
chapters:
[...]
checks:
'1':
title: Check file A
automation:
autopilot: my-autopilot
env:
FILE: A
'2':
title: Check file B
automation:
autopilot: my-autopilot
env:
FILE: B
Examples#
[...]
autopilots:
[...]
my-autopilot:
run: |
echo $MY_VARIABLE # prints "check_variable"
chapters:
[...]
requirements:
[...]
checks:
'1':
title: Check 1
automation:
autopilot: my-autopilot
env: # check environment variables
MY_VARIABLE: "check_variable"
[...]
autopilots:
[...]
my-autopilot:
run: |
echo $MY_VARIABLE # prints "check_overrides_autopilot"
env: # autopilot environment variables
MY_VARIABLE: "autopilot_variable"
chapters:
[...]
requirements:
[...]
checks:
'1':
title: Check 1
automation:
autopilot: my-autopilot
env: # check environment variables
MY_VARIABLE: "check_overrides_autopilot"
env: # global environment variables
MY_VARIABLE: "my_global_value"
[...]
autopilots:
[...]
my-autopilot:
run: |
echo $MY_VARIABLE # prints "check_overrides_global"
chapters:
[...]
requirements:
[...]
checks:
'1':
title: Check 1
automation:
autopilot: my-autopilot
env: # check environment variables
MY_VARIABLE: "check_overrides_global"