Docs
/
/

Variables

Commands for managing variables in the Knock CLI.

#

Variable commands enable you to manage variables in your Knock account from the CLI.

Variables are account-wide. A variable has a default value and can have an override for each environment. Variable commands do not take a --branch flag, and knock variable pull and knock variable push read and write the whole variable catalog. Pushing a variable saves it right away, and Knock does not create a commit for it.

Variable commands are separate from knock pull and knock push, which do not include variables.

Variable file structure

#

When variables are pulled from Knock, they are stored together in a single variables.json file. The file is a JSON object where each property name is a variable key. Knock CLI looks for the file in the directory set by your knock.json file or the --knock-dir flag, and falls back to the current working directory.

Each entry describes one variable:

  • type. public or secret. The type cannot change after the variable is created.
  • description. Optional context about what the variable is for.
  • value. The default value. Public variables only.
  • environment_values. Optional environment overrides, keyed by environment slug. Public variables only.
  • value_set. Whether a secret variable has a default value. Secret variables only.
  • environment_values_set. Whether a secret variable has an override in each environment. Secret variables only.

Knock never returns the value of a secret variable, so the CLI never writes one to your local file system. Pulled secret entries record only whether a value is set. To set the value of a secret variable, use knock variable set with --value-stdin or --value-file.

A variable key must start with a letter or underscore and contain only letters, numbers, and underscores. This is the format you use to reference the variable in a template as vars.<key>.

When you push, a null or omitted value is skipped and leaves the existing value unchanged. An empty string clears the value.

knock variable list [flags]

#

Display all variables in your account. By default, the command shows each variable's default value and its environment overrides. Secret values are never shown. The command reports whether a secret value is set or unset.

Use an --environment flag to show only the value that applies in that environment, which is the override when one is set and the default otherwise.

Flags

--environmentstring

Show only the value that applies in this environment: the override when one is set, otherwise the default.

--typestring

Filter variables by type. One of: public, secret.

--limitnumber

The total number to fetch per page.

--afterstring

Fetches all entries after this cursor.

--beforestring

Fetches all entries before this cursor.

--jsonstring

Format output as json.

knock variable get <key> [flags]

#

Display a single variable, including its default value and every environment override. Secret values are never shown.

Flags

--jsonstring

Format output as json.

knock variable new [flags]

#

Add a new variable entry to your local variables.json file.

The command writes to the variables.json file in the directory set by your knock.json file or the --knock-dir flag, and falls back to the current working directory. If the file does not exist, the command asks you to confirm before creating it. The command exits with an error if the key is already in the file.

The command does not accept --value for a secret variable. After you push the entry, set the secret value with knock variable set.

Flags

--keystringRequired

The variable key.

--typestringRequired

The variable type. One of: public, secret.

--valuestring

The default value. Public variables only.

--descriptionstring

The description of the variable.

--knock-dirdirectory

The Knock directory used to locate variables.json.

--pushboolean

Push the new entry to Knock after writing it locally. Defaults to false.

--forceboolean

Skips the confirmation prompt when creating the file. Defaults to false.

knock variable set <key> [flags]

#

Create or update a variable in Knock directly, without editing a local file. Pass --type when creating a variable. When the variable already exists, --type is optional, and it must match the existing type.

You can only pass one of --value, --value-file, --value-stdin, or --unset-value. A value is required to create a variable.

Secret values are never accepted as command-line arguments, because they would end up in your shell history. For a secret variable, pass the value with --value-stdin or --value-file. The same applies to environment overrides: use --environment-value-file rather than --environment-value.

Flags

--typestring

The variable type. One of: public, secret. Required when creating a variable.

--valuestring

The default value. Public variables only.

--value-filestring

Read the default value from a file. The command does not trim the file's bytes.

--value-stdinboolean

Read the default value from stdin until the end of input. The command does not trim the input. Defaults to false.

--unset-valueboolean

Clear the default value. Defaults to false.

--descriptionstring

The description of the variable.

--unset-descriptionboolean

Clear the description. Defaults to false.

--environment-valuestring

Set an environment override in the format <slug>=<value>. Can be passed more than once. Public variables only.

--environment-value-filestring

Set an environment override from a file in the format <slug>=<path>. Can be passed more than once.

--unset-environmentstring

Clear the override for an environment slug. Can be passed more than once.

--jsonstring

Format output as json.

knock variable pull <key> [flags]

#

Pull variables from Knock into your local variables.json file. The command always pulls the whole variable catalog, including every environment override, so it does not take an --environment flag.

Pass a key to update a single entry. If the variable does not exist in Knock, the command offers to remove that entry from your local file. Without a key, the command replaces the whole file, and any entry that no longer exists in Knock is removed.

By default, this command resolves the variables.json file through your knock.json file or the --knock-dir flag. When neither is set, it uses the current working directory.

Note: Knock CLI asks you to confirm before it writes to the local file system, unless you pass --force.

See the Variable file structure section for details on how the file is organized.

Flags

--knock-dirdirectory

The Knock directory used to locate variables.json.

--forceboolean

Removes the confirmation prompt. Defaults to false.

knock variable push <key> [flags]

#

Push variables from your local variables.json file to Knock. Knock updates an existing variable by the matching key, or creates a new variable if it does not exist yet. Pass a key to push a single entry, or omit it to push every entry in the file.

Note:

  • Pushing does not delete variables from Knock that are missing from your file.
  • A new variable needs a string value. For a secret variable, create it with knock variable set so that you can supply the value without writing it to the file.
  • You cannot change the type of an existing variable.
  • After a successful push, the command updates your local file to match what Knock stored. For a secret variable, the file records only whether a value is set.
  • Variables are saved right away, so this command does not have a --commit flag.

See the Variable file structure section for details on how the file is organized.

Flags

--knock-dirdirectory

The Knock directory used to locate variables.json.

--forceboolean

Removes the confirmation prompt. Defaults to false.

knock variable delete <key> [flags]

#

Delete a variable, or clear a single environment override.

Without an --environment flag, the command deletes the variable and every environment override.

With an --environment flag, the command clears only that environment's override and keeps the variable. The default value then applies in that environment.

Flags

--environmentstring

Clear only this environment's override. The variable is not deleted.

--forceboolean

Removes the confirmation prompt. Defaults to false.

--jsonstring

Format output as json.

New chat