Yaml
| source | condition | target |
|---|---|---|
✔ | ✔ | ✔ |
Description
The yaml resource reads and writes a key in a YAML file, addressed by a yamlpath expression.
- source
Returns the value of one key from one file.
- condition
Checks that the key holds
value, or the source output whenvalueis unset. Withkeyonly, checks only that the key exists.- target
Writes the value at the key. Keys already holding it are reported as up to date and left untouched, so a target that changes nothing does not create a commit.
Parameters
| Name | Type | Description | Required |
|---|---|---|---|
| comment | string | comment defines a comment to add after the value. default: empty compatible:
remarks:
| |
| documentindex | integer | DocumentIndex defines the index of the document to interact with in a multi-document yaml file. compatible:
default: empty remark:
example:
| |
| engine | string | “engine” defines the engine to use to manipulate the yaml file. There is no one good Golang library to manipulate yaml files. And each one of them have has its pros and cons so we decided to allow this customization based on user’s needs. remark:
| |
| file | string | “file” defines the yaml file path to interact with. compatible:
remark:
| |
| files | array | “files” defines the list of yaml files path to interact with. compatible:
remark:
| |
| key | string | “key” defines the yaml keypath. compatible:
remark:
example using default engine:
remark: field path with key/value is not supported at the moment. some help would be useful on https://github.com/goccy/go-yaml/issues/290 | |
| keyonly | boolean | keyonly allows to check only if a key exist and do not return an error otherwise compatible: * condition default: false | |
| keys | array | “keys” defines multiple yaml keypaths to update with the same value. compatible:
remark:
example using default engine:
| |
| searchpattern | boolean | searchpattern defines if the MatchPattern should be applied on the file(s) path If set to true, it modifies the behavior of the The pattern syntax is: | |
| value | string | value is the value associated with a yaml key. compatible:
default: When used from a condition or a target, the default value is set to the associated source output. |
The spec has two mutually exclusive pairs, and breaking either aborts the run before the pipeline starts:
file/filesOne is mandatory. Setting both gives
the attributes 'spec.file' and 'spec.files' are mutually exclusive; duplicates insidefilesare rejected too.key/keysOne is mandatory. Setting both gives
'key' and 'keys' are mutually exclusive; duplicates insidekeysare rejected.
Several parameters work in only one stage, which the table above does not make obvious:
keysTarget only. Updates several keys with the same value in one go - useful for an image tag repeated across a chart. A source or condition must use
key.keyonlyCondition only. Passes when the key exists, whatever its value.
commentTarget only. Appended inline after the value, and only when Updatecli actually changes it.
searchpatternNot supported in a source. It turns
file/filesinto glob patterns rather than exact names. A source using it fails withvalidation error in sources of type 'yaml': the attribute 'spec.searchpattern' is not supported for source.
Engines
engine picks the YAML library, because none of them handles every case well:
| Value | Behaviour |
|---|---|
| Default when unset. Equivalent to |
| Same as |
| Required for filter expressions such as |
Key syntax
Arrays are indexed with key[x], counting from zero, and can be chained with dots, key.array[3].key:
COUNTRY_CODE:
- BE
- FR
- LUHere COUNTRY_CODE[1] is FR.
Escaping dots
A dot separates path elements, so a key that itself contains dots is escaped with a backslash. These two files are different:
image.tag: latest # one key named "image.tag" -> image\.tagimage: # a key "tag" nested under "image" -> image.tag
tag: latestThe escaped form must be written in single quotes, otherwise YAML consumes the backslash before Updatecli sees it:
targets:
helm-chart-label:
name: bump chart dependencies
kind: yaml
disablesourceinput: true
spec:
file: "/tmp/gateway.yaml"
key: 'spec\.values\.labels\.service\.istio\.io/canonical-revision'
value: "test"Multi-document files
documentindex selects one document in a ----separated file, counting from zero. Its default is not "the first document" but something different per stage:
source - the value is taken from the first document matching the query.
condition - every document is evaluated.
target - every document is updated.
That last one surprises people. Given this file:
kind: Deployment
image:
tag: 1.0.0
---
kind: Service
image:
tag: 2.0.0a target on $.image.tag with no documentindex rewrites both, even though the run report mentions a single key:
kind: Deployment
image:
tag: 9.9.9 # updated by updatecli
---
kind: Service
image:
tag: 9.9.9 # updated by updatecliSet documentindex: 0 to touch only the first one.
Remote files
For a source and a condition, file accepts https://, http:// and file://. A target cannot write back to a URL and fails with URL scheme is not supported for YAML target.
Example
# updatecli.yaml
name: Example of YAML resources
scms:
default:
kind: github
spec:
user: "my git user"
email: "my git email"
owner: "olblak"
repository: "chart"
token: "{{ requiredEnv .github.token }}"
username: "github username"
branch: "main"
sources:
lastRelease:
kind: helmchart
spec:
url: https://charts.jenkins.io
name: jenkins
conditions:
chartVersion:
name: "jenkinsci/jenkins Helm Chart used"
kind: yaml
scmid: default
spec:
file: "charts/jenkins/requirements.yaml"
key: "dependencies[0].name"
value: "jenkins"
targets:
chartVersion:
name: "jenkinsci/jenkins Helm Chart"
kind: yaml
scmid: default
spec:
file: "charts/jenkins/requirements.yaml"
key: "dependencies[0].version"
What it says:
- Source
Retrieve the version of the Jenkins helm chart from
https://charts.jenkins.io.- Condition
Check that
charts/jenkins/requirements.yamlhas adependenciesarray whose first element isjenkins. If not, the pipeline stops here.- Target
Update that first element to the source value in the GitHub repository
olblak/chart, then publish the change as a pull request againstmasterfrom a working branch.
Links
goccy/go-yaml#290 - why field paths matching on key/value are not supported