diff --git a/api/v1beta2/git.go b/api/v1beta2/git.go index 060870a0..057e2fff 100644 --- a/api/v1beta2/git.go +++ b/api/v1beta2/git.go @@ -67,6 +67,10 @@ type CommitSpec struct { // into which will be interpolated the details of the change made. // +optional MessageTemplate string `json:"messageTemplate,omitempty"` + + // MessageTemplateValues provides additional values to be available to the + // templating rendering. + MessageTemplateValues map[string]string `json:"messageTemplateValues,omitempty"` } type CommitUser struct { diff --git a/api/v1beta2/zz_generated.deepcopy.go b/api/v1beta2/zz_generated.deepcopy.go index 2400f0a6..b3a5b5a5 100644 --- a/api/v1beta2/zz_generated.deepcopy.go +++ b/api/v1beta2/zz_generated.deepcopy.go @@ -34,6 +34,13 @@ func (in *CommitSpec) DeepCopyInto(out *CommitSpec) { *out = new(SigningKey) **out = **in } + if in.MessageTemplateValues != nil { + in, out := &in.MessageTemplateValues, &out.MessageTemplateValues + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommitSpec. diff --git a/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml b/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml index dd20002b..1ff8c40c 100644 --- a/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml +++ b/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml @@ -427,6 +427,13 @@ spec: MessageTemplate provides a template for the commit message, into which will be interpolated the details of the change made. type: string + messageTemplateValues: + additionalProperties: + type: string + description: |- + MessageTemplateValues provides additional values to be available to the + templating rendering. + type: object signingKey: description: SigningKey provides the option to sign commits with a GPG key diff --git a/docs/api/v1beta2/image-automation.md b/docs/api/v1beta2/image-automation.md index 107ebb96..3ac1c78b 100644 --- a/docs/api/v1beta2/image-automation.md +++ b/docs/api/v1beta2/image-automation.md @@ -70,6 +70,18 @@ string into which will be interpolated the details of the change made.

+ + +messageTemplateValues
+ +map[string]string + + + +

MessageTemplateValues provides additional values to be available to the +templating rendering.

+ + diff --git a/docs/spec/v1beta2/imageupdateautomations.md b/docs/spec/v1beta2/imageupdateautomations.md index 413add00..feded61a 100644 --- a/docs/spec/v1beta2/imageupdateautomations.md +++ b/docs/spec/v1beta2/imageupdateautomations.md @@ -384,6 +384,7 @@ type TemplateData struct { Name, Namespace string } Changed update.ResultV2 + Values map[string]string } // ResultV2 contains the file changes made during the update. It contains @@ -481,6 +482,25 @@ There are over 70 available functions. Some of them are defined by the [Go template language](https://pkg.go.dev/text/template) itself. Most of the others are part of the [Sprig template library](http://masterminds.github.io/sprig/). +Additional data can be provided with `.spec.git.commit.messageTemplateValues`. + +This is a key/value mapping with string values. + +```yaml +--- +apiVersion: image.toolkit.fluxcd.io/v1beta2 +kind: ImageUpdateAutomation +metadata: + name: +spec: + git: + commit: + messageTemplate: |- + Automated image update by Flux for cluster {{ .Values.cluster }}. + messageTemplateValues: + cluster: prod +``` + #### Push `.spec.git.push` is an optional field that specifies how the commits are pushed diff --git a/internal/source/source.go b/internal/source/source.go index 7e9f10d6..8172adf6 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -54,6 +54,7 @@ type TemplateData struct { AutomationObject types.NamespacedName Updated update.Result Changed update.ResultV2 + Values map[string]string } // SourceManager manages source. @@ -245,6 +246,7 @@ func (sm SourceManager) CommitAndPush(ctx context.Context, obj *imagev1.ImageUpd AutomationObject: sm.automationObjKey, Updated: policyResult.ImageResult, Changed: policyResult, + Values: obj.Spec.GitSpec.Commit.MessageTemplateValues, } commitMsg, err := templateMsg(obj.Spec.GitSpec.Commit.MessageTemplate, templateValues) if err != nil { diff --git a/internal/source/source_test.go b/internal/source/source_test.go index e99ff96c..00c6e575 100644 --- a/internal/source/source_test.go +++ b/internal/source/source_test.go @@ -93,6 +93,14 @@ Automation: {{ .AutomationObject }} {{ end -}} {{ end -}} {{ end -}} +` + + testCommitTemplateWithValues = `Commit summary + +Automation: {{ .AutomationObject }} + +Cluster: {{ index .Values "cluster" }} +Testing: {{ .Values.testing }} ` ) @@ -472,6 +480,34 @@ Automation: test-ns/test-update - helloworld:1.0.0 -> helloworld:1.0.1 `, }, + { + name: "push to cloned branch with template and values", + gitSpec: &imagev1.GitSpec{ + Push: &imagev1.PushSpec{ + Branch: "main", + }, + Commit: imagev1.CommitSpec{ + MessageTemplate: testCommitTemplateWithValues, + MessageTemplateValues: map[string]string{ + "cluster": "prod", + "testing": "value", + }, + }, + }, + gitRepoReference: &sourcev1.GitRepositoryRef{ + Branch: "main", + }, + latestImage: "helloworld:1.0.1", + wantErr: false, + wantCommitMsg: `Commit summary + +Automation: test-ns/test-update + +Cluster: prod +Testing: value +`, + }, + { name: "push to different branch", gitSpec: &imagev1.GitSpec{