Config Variables
DevSpace allows you to make your configuration dynamic by using variables in devspace.yaml
.
While there is no need to explicitly define a config variable, it allows you to customize the behavior of DevSpace when working with the variable. Variables are defined within the vars
section of devspace.yaml
.
- Question
- Options
- Password
- Environment Variable
- Implicit Variable
images:
database:
image: "mysql:${MYSQL_VERSION}"
vars:
- name: MYSQL_VERSION
question: Which mysql version do you want to use?
default: "5.7"
images:
database:
image: "mysql:${MYSQL_VERSION}"
vars:
- name: MYSQL_VERSION
question: Which mysql version do you want to use?
options:
- "5.5"
- "5.6"
- "5.7"
images:
database:
image: "mysql:${MYSQL_VERSION}"
vars:
- name: MYSQL_VERSION
question: Which mysql version do you want to use?
password: true
images:
database:
image: "mysql:${MYSQL_VERSION}"
vars:
- name: MYSQL_VERSION
source: env
default: "5.5"
images:
database:
image: "mysql:${MYSQL_VERSION}"
vars: {}
DevSpace allows you to use config variables without explicitly defining them as variables. You can simply reference them via ${MY_ENV_VAR}
anywhere in your devspace.yaml
. Implicit variables use source: all
.
The source
option of a config variable expects either:
If source
is either all
or input
and the variable is not defined, the user will be asked to provide a value either using a generic question or the one provided via the question
option. The user-provided value will be cached in .devspace/generated.yaml
and the user will only be asked again after the cache has been cleared first using:
devspace reset vars
Predefined Variables
DevSpace provides some variables that are filled automatically and can be used within the config. These can be helpful for image tagging and other use cases:
- DEVSPACE_RANDOM: A random 6 character long string
- DEVSPACE_TIMESTAMP A unix timestamp when the config was loaded
- DEVSPACE_GIT_COMMIT: A short hash of the local repo's current git commit
- DEVSPACE_SPACE: The name of the Space that is currently used
- DEVSPACE_SPACE_NAMESPACE: The Kubernetes namespace of the Space in the cluster
- DEVSPACE_USERNAME: The username currently logged into DevSpace Cloud
Example: Using ${DEVSPACE_GIT_COMMIT}
images:
default:
image: myrepo/devspace
tag: ${DEVSPACE_GIT_COMMIT}-${DEVSPACE_TIMESTAMP}
Explanation:
This config will tag the image in the form of myrepo/devspace:d9b4bcd-1559766514
. Many other combinations are possible with this method.
Useful Commands
devspace list vars
To get a list of all variables defined in the devspace.yaml
, you can run this command:
devspace list vars
devspace reset vars
Once DevSpace asks you to provide a value for a variable, this value will be stored in the variables cache, so you will not asked about this variable again. To reset the variables cache, run:
devspace reset vars
DevSpace will fill the variables cache again, once you run the next build or deployment command.
devspace print
The following command prints the config after all variables have been replaced:
devspace print
You can optionally add the -p / --profiles
flag to this command.
export VAR_NAME=value
The value for a config variable can also be set by defining an environment variable named [VAR_NAME]
. Setting the value of a config variable with name ${IMAGE_NAME}
would be possible by setting an environment value IMAGE_NAME
.
- Windows Powershell
- Mac Terminal
- Linux Bash
$env:IMAGE_NAME = "some-value"
export IMAGE_NAME="some-value"
export IMAGE_NAME="some-value"