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 |
|---|---|---|---|
| appendtoarray | boolean | “appendtoarray” appends the value as a new entry of the yaml sequence selected by “key”. compatible:
default: false remark:
example:
| |
| comment | string | “comment” defines a comment added after the value. compatible:
default: empty remark:
| |
| createmissingkey | boolean | “createmissingkey” creates the key when the yaml document does not hold it yet. compatible:
default: false remark:
example:
| |
| documentindex | integer | “documentindex” defines the index of the document to use in a multi document yaml file. compatible:
default: empty remark:
example:
| |
| engine | string | “engine” defines the library used to manipulate the yaml file. No single Go library handles yaml well in every case, and each has its own strengths, so Updatecli lets you pick the one that suits your file. default: go-yaml remark:
| |
| file | string | “file” defines the path of the yaml file to use. compatible:
remark:
| |
| files | array | “files” defines the list of yaml file paths to use. compatible:
remark:
| |
| key | string | “key” defines the yaml key path. compatible:
remark:
example:
| |
| keyonly | boolean | “keyonly” checks only that the key exists, whatever its value. compatible:
default: false | |
| keys | array | “keys” defines several yaml key paths to update with the same value. compatible:
remark:
example:
| |
| searchpattern | boolean | “searchpattern” treats “file” and “files” as path patterns instead of exact paths. The pattern must match the whole path, not just a substring. The pattern syntax is: compatible:
default: false remark:
| |
| value | string | “value” defines the value associated with the yaml key. compatible:
default: in a condition or a target, the output of the associated source. |
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