Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1beta2/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions docs/api/v1beta2/image-automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ string
into which will be interpolated the details of the change made.</p>
</td>
</tr>
<tr>
<td>
<code>messageTemplateValues</code><br>
<em>
map[string]string
</em>
</td>
<td>
<p>MessageTemplateValues provides additional values to be available to the
templating rendering.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
20 changes: 20 additions & 0 deletions docs/spec/v1beta2/imageupdateautomations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: <automation-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
Expand Down
2 changes: 2 additions & 0 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type TemplateData struct {
AutomationObject types.NamespacedName
Updated update.Result
Changed update.ResultV2
Values map[string]string
}

// SourceManager manages source.
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions internal/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ Automation: {{ .AutomationObject }}
{{ end -}}
{{ end -}}
{{ end -}}
`

testCommitTemplateWithValues = `Commit summary

Automation: {{ .AutomationObject }}

Cluster: {{ index .Values "cluster" }}
Testing: {{ .Values.testing }}
`
)

Expand Down Expand Up @@ -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{
Expand Down