Transformer
Description
The "transformers" parameter can be used by any stage definition as a "source", "condition", or "target". A "transformers" object contains a list of transformers. Each kind of transformer rules applies a string manipulation like adding or removing characters.
source
Source returns information modified by a list of transformers.
condition
A condition receives information from the source then modifies that value using transformers before using it in the resource.
target
A target receives information from the source then modifies that value with the transformers rule before using the resource.
Parameters
| Name | Default | Description |
|---|---|---|
addprefix | - | Add a prefix to the value |
addsuffix | - | Add a suffix to the value |
trimprefix | - | Remove a prefix from the value, if present |
trimsuffix | - | Remove a suffix from the value, if present |
replacers | - | A list of Replacer, applied as a single pass, more here |
replacer | - | Replace a string with another one, more here |
find | - | Return the first substring matching a regular expression, or an empty string when there is no match |
findsubmatch | - | Search for a substring using a regular expression and return a capture group, more here |
semverinc | - | Bump a semantic version, accepts a comma separated list of "major", "minor", "patch" |
quote | false | Add quotes around the value |
unquote | false | Remove quotes around the value |
jsonmatch | - |
Important | A single list entry may hold several of those keys, and they are then applied in the fixed order of the table above, not in the order they are written in the manifest. To control the order, use one list entry per operation: |
Note | Transformer keys are lowercase. The camelCase spellings addPrefix, addSuffix, trimPrefix, trimSuffix, semverInc, findSubMatch, and captureIndex are deprecated: they still work but log a warning, and are ignored when the lowercase form is also present. |
Transformers never receive an empty value: an empty input aborts the transformation with a validation error.
Replacer
A replacer rule modifies the information by replacing every occurrence of the "from" value by the "to" value.
from and to are plain strings, not regular expressions.
| Name | Required | Default | Description |
|---|---|---|---|
from | ✔ | - | "from" value defines the string that will be replaced |
to | ✔ | - | "to" value defines the string that we want to have |
replacers accepts a list of those rules and applies them in a single pass, so a replacement
never rewrites the output of an earlier one:
transformers:
- replacers:
- from: "alpha"
to: "beta"
# "beta" produced by the previous rule is left untouched
- from: "beta"
to: "gamma"FindSubMatch
A findsubmatch rule captures the desired information by applying pattern as a regular expression
and returning one of its capture groups.
| Name | Required | Default | Description |
|---|---|---|---|
pattern | ✔ | - |
|
captureindex | - |
| Index of the group to return. |
capturepattern | - | - | A template used to build the output from several groups at once, referencing them with |
When the regular expression does not match at all, or when captureindex points past the last group,
the transformer returns an empty string rather than failing.
transformers:
# "jenkins-2.375.1" -> "2.375.1"
- findsubmatch:
pattern: 'jenkins-(\d+\.\d+\.\d+)'
captureindex: 1transformers:
# "1.2.3" -> "major=1 minor=2 patch=3"
- findsubmatch:
pattern: '(\d+)\.(\d+)\.(\d+)'
capturepattern: 'major=\1 minor=\2 patch=\3'JsonMatch
A jsonmatch rule parses the incoming value as JSON and extracts a value using a
dasel selector.
It is typically used behind a source returning a raw JSON payload, such as an HTTP response.
| Name | Required | Default | Description |
|---|---|---|---|
key | ✔ | - | The dasel selector to evaluate, for example |
nomatchresult | - | - | What to return when the selector matches nothing. Use the literal |
joinmultiplematches | - | - | When the selector matches several values, join them with this separator |
multiplematchselector | - | - | When the selector matches several values, pick one: |
Important | When the selector matches several values and neither joinmultiplematches nor multiplematchselector is set, the transformer fails. |
sources:
latestRelease:
kind: http
spec:
# The "http" source returns the raw response body
url: https://api.github.com/repos/updatecli/updatecli/releases/latest
transformers:
- jsonmatch:
key: .tag_name
- trimprefix: "v"Examples
Chaining transformers
Each list entry receives the output of the previous one.
Given a source returning jenkins-2.375.1:
sources:
default:
name: "Get latest Jenkins weekly version"
kind: jenkins
spec:
release: weekly
transformers:
# "jenkins-2.375.1" -> "2.375.1"
- trimprefix: "jenkins-"
# "2.375.1" -> "2.376.0"
- semverinc: "minor"
# "2.376.0" -> "jenkins/jenkins:2.376.0-jdk17"
- addprefix: "jenkins/jenkins:"
- addsuffix: "-jdk17"The output, jenkins/jenkins:2.376.0-jdk17, is a valid Docker image name usable by the later stages.
Preparing a value for a Docker tag
quote and unquote are convenient when the target file expects a quoted string:
sources:
version:
kind: shell
spec:
command: echo 1.2.3
transformers:
- addprefix: "v"
# -> "v1.2.3" including the double quotes
- quote: trueGo Further
To know more about how a source produces the value being transformed source
To know more about selecting a version before transforming it version filter