CNB Replace Tokens Plugin (Go implementation). Replaces token placeholders in files with values from environment variables, inline JSON, or files.
Ported from qetza/replacetokens (Node.js), rewritten as a CNB plugin.
All parameters are passed via PLUGIN_-prefixed environment variables.
| Parameter | Env Var | Description |
|---|---|---|
sources | PLUGIN_SOURCES | Source file glob pattern(s), separated by ; or newline. Supports => output path syntax. |
At least one of the following is also required:
| Parameter | Env Var | Description |
|---|---|---|
use_env | PLUGIN_USE_ENV | When true, all current environment variables are used as token variables. |
variables | PLUGIN_VARIABLES | Variable source(s): inline JSON / @<glob> file / $ENV_VAR. |
| Parameter | Env Var | Default | Description |
|---|---|---|---|
root | PLUGIN_ROOT | CNB_WORKSPACE or cwd | Root directory for resolving relative glob paths. |
encoding | PLUGIN_ENCODING | auto | File encoding: auto / utf-8 / utf-16le / utf-16be. |
token_pattern | PLUGIN_TOKEN_PATTERN | default | Token pattern, see below. |
token_prefix | PLUGIN_TOKEN_PREFIX | — | Custom token prefix (required when token_pattern=custom). |
token_suffix | PLUGIN_TOKEN_SUFFIX | — | Custom token suffix (required when token_pattern=custom). |
missing_var_action | PLUGIN_MISSING_VAR_ACTION | none | Action when variable is not found: none / keep / replace. |
missing_var_default | PLUGIN_MISSING_VAR_DEFAULT | "" | Default value used when missing_var_action=replace. |
missing_var_log | PLUGIN_MISSING_VAR_LOG | warn | Log level for missing variables: off / info / warn / error. |
escape | PLUGIN_ESCAPE | auto | Value escape type: auto / off / json / xml / custom. |
escape_char | PLUGIN_ESCAPE_CHAR | — | Escape character for custom escape. |
chars_to_escape | PLUGIN_CHARS_TO_ESCAPE | — | Characters to escape when using custom escape. |
recursive | PLUGIN_RECURSIVE | false | Enable recursive token replacement (values can contain other tokens). |
add_bom | PLUGIN_ADD_BOM | false | Add BOM when writing files. |
transforms | PLUGIN_TRANSFORMS | false | Enable transform functions in tokens. |
transforms_prefix | PLUGIN_TRANSFORMS_PREFIX | ( | Transform prefix. |
transforms_suffix | PLUGIN_TRANSFORMS_SUFFIX | ) | Transform suffix. |
case_insensitive_paths | PLUGIN_CASE_INSENSITIVE_PATHS | false | Case-insensitive glob matching. |
include_dot_paths | PLUGIN_INCLUDE_DOT_PATHS | false | Include dot-prefixed files and directories. |
separator | PLUGIN_SEPARATOR | . | Separator used when flattening nested variable names. |
log_level | PLUGIN_LOG_LEVEL | info | Log level: debug / info / warn / error / off. |
| Pattern | Prefix | Suffix | Example |
|---|---|---|---|
default | #{ | }# | #{MY_VAR}# |
azurepipelines | $( | ) | $(MY_VAR) |
doublebraces | {{ | }} | {{MY_VAR}} |
doubleunderscores | __ | __ | __MY_VAR__ |
githubactions | ${{ | }} | ${{MY_VAR}} |
octopus | #{ | } | #{MY_VAR} |
custom | user-defined | user-defined | — |
PLUGIN_VARIABLES accepts multiple sources separated by newlines or ;:
# Inline JSON
{"key": "value", "nested": {"a": "1"}}
# Load from file(s) — supports JSON / YAML, supports glob
@configs/*.json
# Load from an environment variable (its value must be a JSON string)
$MY_JSON_ENV
Nested objects are flattened and keys are uppercased automatically:
{"db": {"host": "localhost", "port": 5432}}
Results in variables: DB.HOST=localhost, DB.PORT=5432 (using default separator .).
When transforms=true, you can include a transform function inside a token:
#{myvar(lower)}# → lowercase
#{myvar(upper)}# → uppercase
#{myvar(base64)}# → Base64 encode
#{myvar(indent,4)}# → indent with 4 spaces
#{myvar(raw)}# → raw value (no escaping applied)
After execution, the plugin sets the following pipeline output variables:
| Variable | Description |
|---|---|
files | Number of files processed |
tokens | Total tokens found |
replaced | Tokens successfully replaced |
defaults | Times the default value was used |
transforms | Transform functions executed |
steps:
- name: Replace tokens in config files
image: cnb.cool/r_r/replacetokens
settings:
sources: "config/*.json;app.yaml"
variables: |
{"app_name": "myapp", "version": "1.0.0"}
@vars/production.json
token_pattern: default
missing_var_action: warn
escape: auto
log_level: info
steps:
- name: Replace using environment variables
image: cnb.cool/r_r/replacetokens
settings:
sources: "deployment.yaml => deployment.out.yaml"
use_env: true
token_pattern: doublebraces
missing_var_action: keep
# Multiple globs (semicolon-separated)
**/*.json;**/*.yaml
# Output to a different file
template.json => output.json
# Wildcard output (* maps to the matched part of the input filename)
templates/*.json => out/*.json
# Relative output path (relative to the input file's directory)
config.tmpl => config.json
go build -o replacetokens .
Cross-compile for Linux container:
GOOS=linux GOARCH=amd64 go build -o replacetokens .