Skip to content

Commit 56e1161

Browse files
mallendemolblak
andauthored
Update templating doc (#2638)
Co-authored-by: Olivier Vernin <olivier@vernin.me>
1 parent 8ee839b commit 56e1161

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

content/en/docs/plugins/resource/file.adoc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ It retrieves the content of the file at the location `file` and use it as the so
6565
* `content`
6666
* `forcecreate`
6767
* `replacepattern`
68+
* `template`
6869

6970
=== Filtering File Source Content
7071

@@ -233,6 +234,7 @@ or fails the pipeline (and never run the pipeline's targets).
233234

234235
* `forcecreate`
235236
* `replacepattern`
237+
* `template`
236238

237239
=== Condition Input Value
238240

@@ -427,10 +429,15 @@ The "Input Value" is the string to write to the specified file.
427429
* You can also specify a custom content with the `spec.content` attribute instead of using the input source value.
428430
Using the `spec.content` is useful when you need to templatize with the source input value (see example below).
429431

432+
* Alternatively, you can define a Go template file path with the `spec.template` attribute to render dynamic content based on the source value.
433+
The template is rendered using all https://masterminds.github.io/sprig/[Sprig template functions], with the source value accessible as `.source` in the template context.
434+
430435
* Finally, you can define a https://pkg.go.dev/regexp[Golang regexp] in the attribute `spec.ReplacePattern`,
431436
if and only if you also specified a `spec.matchpattern` (see <<Restricting File Content Update>> and <<Target Examples>> for more details).
432437
** Regexp's capturing group are supported (this is the recommended use case)
433438

439+
⚠️ The `spec.template` attribute is mutually exclusive with `spec.content`, `spec.matchpattern`, `spec.replacepattern`, and `spec.line`.
440+
434441
=== Restricting File Content Update
435442

436443
You may restrict which part of the specified file to be updated with the input value with the following options:
@@ -558,6 +565,70 @@ updateCopyrightYear
558565
--
559566

560567

568+
* The following target uses a Go template file to generate dynamic content based on the source value.
569+
The template has access to all https://masterminds.github.io/sprig/[Sprig functions] and the source value via `.source`:
570+
+
571+
[source,yaml]
572+
.updateCli.yaml:
573+
--
574+
# ..
575+
sources:
576+
getLatestVersion:
577+
kind: shell
578+
spec:
579+
command: "echo \"1.25.1\""
580+
targets:
581+
useTemplate:
582+
kind: file
583+
sourceid: getLatestVersion # Source Value is "1.25.1"
584+
spec:
585+
file: version-info.txt
586+
template: templates/version.tmpl
587+
forcecreate: true
588+
--
589+
+
590+
[source,go-template]
591+
.templates/version.tmpl:
592+
--
593+
Application Version Information
594+
================================
595+
596+
Current Version: {{ .source }}
597+
Updated: {{ now | date "2006-01-02" }}
598+
Release Notes: https://example.com/releases/{{ .source }}
599+
--
600+
+
601+
[source,shell]
602+
--
603+
$ updatecli apply
604+
605+
606+
source: getLatestVersion
607+
------------------------
608+
609+
The shell 🐚 command "/bin/sh /tmp/updatecli/bin/ab7e342dff922f8ba849dbf7d0c4ac2adc5855b83fe02401b781e7267f7233c6.sh" ran successfully with the following output:
610+
----
611+
1.25.1
612+
----
613+
✔ shell command executed successfully
614+
615+
target: useTemplate
616+
-------------------
617+
618+
Creating a new file at "version-info.txt"
619+
"version-info.txt" updated with content "Application Version Information\n================================\n\nCurrent Version: 1.25.1\nUpdated: 2026-01-21\nRelease Notes: https://example.com/releases/1.25.1\n"
620+
621+
```
622+
--- version-info.txt
623+
+++ version-info.txt
624+
@@ -1 +1,6 @@
625+
+Application Version Information
626+
+================================
627+
+
628+
+Current Version: 1.25.1
629+
+Updated: 2026-01-21
630+
+Release Notes: https://example.com/releases/1.25.1
631+
561632
== Reference
562633
563634
@@ -706,4 +777,10 @@ targets:
706777
spec:
707778
file: LICENSE
708779
matchpattern: 'Copyright \(c\) (\d*) (.*)'
780+
useTemplate:
781+
kind: file
782+
sourceid: getLatestVersion # Source Value is "1.25.1"
783+
spec:
784+
file: version-info.txt
785+
template: templates/version.tmpl
709786
----

0 commit comments

Comments
 (0)