mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-04 03:11:55 +02:00
feat(ko): allow to have an empty tag name (#4043)
Empty tag names will then be filtered out. This allows to have optional tags depending on templates, for example, `{{if not .Prerelease}}latest{{end}}`, among other use cases. This already happens in the `dockers` section, and is now implemented in `kos` too. refs https://github.com/orgs/goreleaser/discussions/4042
This commit is contained in:
parent
fa36cde69d
commit
051381837d
@ -9,6 +9,7 @@ import (
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -312,7 +313,7 @@ func buildBuildOptions(ctx *context.Context, cfg config.Ko) (*buildOptions, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts.tags = tags
|
||||
opts.tags = removeEmpty(tags)
|
||||
|
||||
if cfg.CreationTime != "" {
|
||||
creationTime, err := getTimeFromTemplate(ctx, cfg.CreationTime)
|
||||
@ -367,6 +368,17 @@ func buildBuildOptions(ctx *context.Context, cfg config.Ko) (*buildOptions, erro
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
func removeEmpty(strs []string) []string {
|
||||
var res []string
|
||||
for _, s := range strs {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
continue
|
||||
}
|
||||
res = append(res, s)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func applyTemplate(ctx *context.Context, templateable []string) ([]string, error) {
|
||||
var templated []string
|
||||
for _, t := range templateable {
|
||||
|
@ -131,6 +131,7 @@ func TestPublishPipeSuccess(t *testing.T) {
|
||||
Labels map[string]string
|
||||
ExpectedLabels map[string]string
|
||||
Platforms []string
|
||||
Tags []string
|
||||
CreationTime string
|
||||
KoDataCreationTime string
|
||||
}{
|
||||
@ -172,12 +173,30 @@ func TestPublishPipeSuccess(t *testing.T) {
|
||||
Name: "kodata-creation-time",
|
||||
KoDataCreationTime: "1672531200",
|
||||
},
|
||||
{
|
||||
Name: "tag-templates",
|
||||
Tags: []string{
|
||||
"{{if not .Prerelease }}{{.Version}}{{ end }}",
|
||||
" ", // empty
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "tag-template-eval-empty",
|
||||
Tags: []string{
|
||||
"{{.Version}}",
|
||||
"{{if .Prerelease }}latest{{ end }}",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
repository := fmt.Sprintf("%sgoreleasertest/testapp", registry)
|
||||
|
||||
for _, table := range table {
|
||||
table := table
|
||||
t.Run(table.Name, func(t *testing.T) {
|
||||
if len(table.Tags) == 0 {
|
||||
table.Tags = []string{table.Name}
|
||||
}
|
||||
ctx := testctx.NewWithCfg(config.Project{
|
||||
ProjectName: "test",
|
||||
Builds: []config.Build{
|
||||
@ -199,20 +218,33 @@ func TestPublishPipeSuccess(t *testing.T) {
|
||||
Repository: repository,
|
||||
Labels: table.Labels,
|
||||
Platforms: table.Platforms,
|
||||
Tags: []string{table.Name},
|
||||
Tags: table.Tags,
|
||||
CreationTime: table.CreationTime,
|
||||
KoDataCreationTime: table.KoDataCreationTime,
|
||||
SBOM: table.SBOM,
|
||||
Bare: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}, testctx.WithVersion("1.2.0"))
|
||||
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
|
||||
tags, err := applyTemplate(ctx, table.Tags)
|
||||
require.NoError(t, err)
|
||||
tags = removeEmpty(tags)
|
||||
require.Len(t, tags, 1)
|
||||
|
||||
ref, err := name.ParseReference(
|
||||
fmt.Sprintf("%s:%s", repository, table.Name),
|
||||
fmt.Sprintf("%s:latest", repository),
|
||||
name.Insecure,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
_, err = remote.Index(ref)
|
||||
require.Error(t, err) // latest should not exist
|
||||
|
||||
ref, err = name.ParseReference(
|
||||
fmt.Sprintf("%s:%s", repository, tags[0]),
|
||||
name.Insecure,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
@ -58,12 +58,14 @@ kos:
|
||||
- linux/arm64
|
||||
|
||||
# Tag to build and push.
|
||||
# Empty tags are ignored.
|
||||
#
|
||||
# Default: 'latest'
|
||||
# Templates: allowed
|
||||
tags:
|
||||
- latest
|
||||
- '{{.Tag}}'
|
||||
- '{{if not .Prerelease}}stable{{end}}'
|
||||
|
||||
# Creation time given to the image
|
||||
# in seconds since the Unix epoch as a string.
|
||||
|
Loading…
Reference in New Issue
Block a user