logo
0
0
WeChat Login

replacetokens

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.


Plugin Parameters

All parameters are passed via PLUGIN_-prefixed environment variables.

Required

ParameterEnv VarDescription
sourcesPLUGIN_SOURCESSource file glob pattern(s), separated by ; or newline. Supports => output path syntax.

At least one of the following is also required:

ParameterEnv VarDescription
use_envPLUGIN_USE_ENVWhen true, all current environment variables are used as token variables.
variablesPLUGIN_VARIABLESVariable source(s): inline JSON / @<glob> file / $ENV_VAR.

Optional

ParameterEnv VarDefaultDescription
rootPLUGIN_ROOTCNB_WORKSPACE or cwdRoot directory for resolving relative glob paths.
encodingPLUGIN_ENCODINGautoFile encoding: auto / utf-8 / utf-16le / utf-16be.
token_patternPLUGIN_TOKEN_PATTERNdefaultToken pattern, see below.
token_prefixPLUGIN_TOKEN_PREFIXCustom token prefix (required when token_pattern=custom).
token_suffixPLUGIN_TOKEN_SUFFIXCustom token suffix (required when token_pattern=custom).
missing_var_actionPLUGIN_MISSING_VAR_ACTIONnoneAction when variable is not found: none / keep / replace.
missing_var_defaultPLUGIN_MISSING_VAR_DEFAULT""Default value used when missing_var_action=replace.
missing_var_logPLUGIN_MISSING_VAR_LOGwarnLog level for missing variables: off / info / warn / error.
escapePLUGIN_ESCAPEautoValue escape type: auto / off / json / xml / custom.
escape_charPLUGIN_ESCAPE_CHAREscape character for custom escape.
chars_to_escapePLUGIN_CHARS_TO_ESCAPECharacters to escape when using custom escape.
recursivePLUGIN_RECURSIVEfalseEnable recursive token replacement (values can contain other tokens).
add_bomPLUGIN_ADD_BOMfalseAdd BOM when writing files.
transformsPLUGIN_TRANSFORMSfalseEnable transform functions in tokens.
transforms_prefixPLUGIN_TRANSFORMS_PREFIX(Transform prefix.
transforms_suffixPLUGIN_TRANSFORMS_SUFFIX)Transform suffix.
case_insensitive_pathsPLUGIN_CASE_INSENSITIVE_PATHSfalseCase-insensitive glob matching.
include_dot_pathsPLUGIN_INCLUDE_DOT_PATHSfalseInclude dot-prefixed files and directories.
separatorPLUGIN_SEPARATOR.Separator used when flattening nested variable names.
log_levelPLUGIN_LOG_LEVELinfoLog level: debug / info / warn / error / off.

Token Patterns

PatternPrefixSuffixExample
default#{}##{MY_VAR}#
azurepipelines$()$(MY_VAR)
doublebraces{{}}{{MY_VAR}}
doubleunderscores______MY_VAR__
githubactions${{}}${{MY_VAR}}
octopus#{}#{MY_VAR}
customuser-defineduser-defined

Variables Format

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 .).


Transforms

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)

Output Variables

After execution, the plugin sets the following pipeline output variables:

VariableDescription
filesNumber of files processed
tokensTotal tokens found
replacedTokens successfully replaced
defaultsTimes the default value was used
transformsTransform functions executed

Pipeline Examples

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

Sources Advanced Syntax

# 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

Build

go build -o replacetokens .

Cross-compile for Linux container:

GOOS=linux GOARCH=amd64 go build -o replacetokens .