You can pass variables from command line using --var
and --var-file
options. Both options can be used multiple times. Variables are exposed via var variable.
Use --var
option to pass a single named variable from the command line.
Provide a single variable named myVariable with value hello:
Using Handlebars, you can refer to the variable in configuration files like so:
Or, if you prefer, EJS:
Use --var-file
option to load variables from a file. If the file extension is .json or .yml, the file is first parsed into an object which is then stored to a variable. If the file extension is something else, the contents are just read into a variable.
The variable name can be omitted for .yml and .json files as long as the file contents can be deserialized to an object. The deserialized object is then stored to the top-level of variables.
If the project directory contains a file named commit.txt, you can read its contents into a variable named commitHash like so:
Here's an example how you would deserialize file contents to a variable. Suppose you have a file /home/variables.yml with valid YAML contents:
You can deserialize its contents to a variable named myVariable like so:
And then with Handlebars, use the variable in the configuration like so:
Or like this when using EJS:
Say, you have a file properties.json with valid JSON contents:
You can deserialize its contents to top-level of variables:
And then with Handlebars, use the variables in the configuration like so:
Or, with EJS:
Variables from files are loaded first in the order they are defined, and then the named variables also in the definition order. Variables defined later will override previously loaded variables with the same name. Complex variables are merged recursively.
Say, you have a JSON file that defines some basic settings:
You also have another file that contains environment-specific settings:
You can load both settings files and also override and extend the loaded configuration using named variables:
The final merged variables object would look like this: