From e1c2c19330911d9e35b03b8b08681e0a7cd0cb70 Mon Sep 17 00:00:00 2001 From: noqcks Date: Thu, 29 Mar 2018 13:12:46 -0400 Subject: [PATCH] feat(templater) Add a template function to insert surrounding repo's Git hash A template function has been added that allows one to template the Git hash of the surrounding repo. This is useful to be able to inspect the deployment revision of an object in Kubernetes. --- docs/templates.md | 4 ++++ templater/templater.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/docs/templates.md b/docs/templates.md index 11488f573..6c44035d0 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -75,6 +75,7 @@ Some template functions come from Go's standard library and are listed in the available in kontemplate, as well as three custom functions: * `json`: Encodes any supplied data structure as JSON. +* `gitHEAD`: Retrieves the commit hash at Git `HEAD`. * `passLookup`: Looks up the supplied key in [pass][]. * `insertFile`: Insert the contents of the given file in the resource set folder as a string. @@ -96,6 +97,9 @@ certKeyPath: my-website/cert-key {{ .certKeyPath | passLookup }} -> Returns content of 'my-website/cert-key' from pass + +{{ gitHEAD }} +-> Returns the Git commit hash at HEAD. ``` ## Conditionals & ranges diff --git a/templater/templater.go b/templater/templater.go index bfd2af1f6..89cc1cd73 100644 --- a/templater/templater.go +++ b/templater/templater.go @@ -15,6 +15,7 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path" "strings" "text/template" @@ -169,6 +170,14 @@ func templateFuncs(rs *context.ResourceSet) template.FuncMap { return string(b) } m["passLookup"] = GetFromPass + m["gitHEAD"] = func() (string, error) { + out, err := exec.Command("sh", "-c", "git rev-parse HEAD").Output() + if err != nil { + return "", err + } + output := strings.TrimSpace(string(out)) + return output, nil + } m["lookupIPAddr"] = GetIPsFromDNS m["insertFile"] = func(file string) (string, error) { data, err := ioutil.ReadFile(path.Join(rs.Path, file))