1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-22 04:08:49 +02:00
zhouhaibing089 bffa7e5e2f
feat: template header and footer (#2326)
A continuation of #2267 - the header and footer in release configuration
can be templatized as well to support more use cases like attaching
published docker image information(without using docker pipe).
2021-07-03 13:42:54 -03:00

72 lines
1.6 KiB
Go

package release
import (
"bytes"
"strings"
"text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)
const bodyTemplateText = `{{ with .Header }}{{ . }}{{ "\n" }}{{ end }}
{{- .ReleaseNotes }}
{{- with .DockerImages }}
## Docker images
{{ range $element := . }}
- ` + "`docker pull {{ . -}}`" + `
{{- end -}}
{{- end }}
{{- with .Footer }}{{ "\n" }}{{ . }}{{ end }}
`
func isLatest(img string) bool {
return strings.HasSuffix(img, ":latest") || !strings.Contains(img, ":")
}
func describeBody(ctx *context.Context) (bytes.Buffer, error) {
var out bytes.Buffer
// nolint:prealloc
var dockers []string
for _, a := range ctx.Artifacts.Filter(artifact.ByType(artifact.DockerManifest)).List() {
if isLatest(a.Name) {
continue
}
dockers = append(dockers, a.Name)
}
if len(dockers) == 0 {
for _, a := range ctx.Artifacts.Filter(artifact.ByType(artifact.DockerImage)).List() {
if isLatest(a.Name) {
continue
}
dockers = append(dockers, a.Name)
}
}
header, err := tmpl.New(ctx).Apply(ctx.Config.Release.Header)
if err != nil {
return out, err
}
footer, err := tmpl.New(ctx).Apply(ctx.Config.Release.Footer)
if err != nil {
return out, err
}
bodyTemplate := template.Must(template.New("release").Parse(bodyTemplateText))
err = bodyTemplate.Execute(&out, struct {
Header string
Footer string
ReleaseNotes string
DockerImages []string
}{
Header: header,
Footer: footer,
ReleaseNotes: ctx.ReleaseNotes,
DockerImages: dockers,
})
return out, err
}