mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
feat: IDs filter for Checksums pipe (#1805)
* Add IDs filter to checksums pipe This allows specific artifacts to be excluded from the checksums file. The reason for introducing this, is the requirement of Terraform Registry releases to only contain the prescribed archives in the checksums file. If the file contains more, the release is not accepted by the Terraform Registry. * Add test case for IDs filter of checksums pipe * Document IDs filter of checksum pipe * Always apply type filter for artifacts This is more in line with how the other ID-filters work.
This commit is contained in:
parent
c10e597953
commit
e3f8178b35
@ -40,14 +40,17 @@ func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
if ctx.Config.Checksum.Disable {
|
||||
return pipe.Skip("checksum.disable is set")
|
||||
}
|
||||
artifactList := ctx.Artifacts.Filter(
|
||||
artifact.Or(
|
||||
artifact.ByType(artifact.UploadableArchive),
|
||||
artifact.ByType(artifact.UploadableBinary),
|
||||
artifact.ByType(artifact.UploadableSourceArchive),
|
||||
artifact.ByType(artifact.LinuxPackage),
|
||||
),
|
||||
).List()
|
||||
filter := artifact.Or(
|
||||
artifact.ByType(artifact.UploadableArchive),
|
||||
artifact.ByType(artifact.UploadableBinary),
|
||||
artifact.ByType(artifact.UploadableSourceArchive),
|
||||
artifact.ByType(artifact.LinuxPackage),
|
||||
)
|
||||
if len(ctx.Config.Checksum.IDs) > 0 {
|
||||
filter = artifact.And(filter, artifact.ByIDs(ctx.Config.Checksum.IDs...))
|
||||
}
|
||||
|
||||
artifactList := ctx.Artifacts.Filter(filter).List()
|
||||
if len(artifactList) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
@ -17,44 +17,90 @@ func TestDescription(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPipe(t *testing.T) {
|
||||
var binary = "binary"
|
||||
var checksums = binary + "_bar_checksums.txt"
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
require.NoError(t, err)
|
||||
var file = filepath.Join(folder, binary)
|
||||
require.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0644))
|
||||
var ctx = context.New(
|
||||
config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: binary,
|
||||
Checksum: config.Checksum{
|
||||
NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
|
||||
Algorithm: "sha256",
|
||||
const binary = "binary"
|
||||
const archive = binary + ".tar.gz"
|
||||
const linuxPackage = binary + ".rpm"
|
||||
const checksums = binary + "_bar_checksums.txt"
|
||||
|
||||
tests := map[string]struct {
|
||||
ids []string
|
||||
want []string
|
||||
}{
|
||||
"default": {
|
||||
want: []string{
|
||||
binary,
|
||||
archive,
|
||||
linuxPackage,
|
||||
},
|
||||
},
|
||||
"select ids": {
|
||||
ids: []string{
|
||||
"id-1",
|
||||
"id-2",
|
||||
},
|
||||
want: []string{
|
||||
binary,
|
||||
archive,
|
||||
},
|
||||
},
|
||||
)
|
||||
ctx.Git.CurrentTag = "1.2.3"
|
||||
ctx.Env = map[string]string{"FOO": "bar"}
|
||||
ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Name: binary,
|
||||
Path: file,
|
||||
Type: artifact.UploadableBinary,
|
||||
})
|
||||
ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Name: binary + ".tar.gz",
|
||||
Path: file,
|
||||
Type: artifact.UploadableArchive,
|
||||
})
|
||||
require.NoError(t, Pipe{}.Run(ctx))
|
||||
var artifacts []string
|
||||
for _, a := range ctx.Artifacts.List() {
|
||||
artifacts = append(artifacts, a.Name)
|
||||
}
|
||||
require.Contains(t, artifacts, checksums, binary)
|
||||
bts, err := ioutil.ReadFile(filepath.Join(folder, checksums))
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary")
|
||||
require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary.tar.gz")
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
require.NoError(t, err)
|
||||
var file = filepath.Join(folder, binary)
|
||||
require.NoError(t, ioutil.WriteFile(file, []byte("some string"), 0644))
|
||||
var ctx = context.New(
|
||||
config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: binary,
|
||||
Checksum: config.Checksum{
|
||||
NameTemplate: "{{ .ProjectName }}_{{ .Env.FOO }}_checksums.txt",
|
||||
Algorithm: "sha256",
|
||||
IDs: tt.ids,
|
||||
},
|
||||
},
|
||||
)
|
||||
ctx.Git.CurrentTag = "1.2.3"
|
||||
ctx.Env = map[string]string{"FOO": "bar"}
|
||||
ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Name: binary,
|
||||
Path: file,
|
||||
Type: artifact.UploadableBinary,
|
||||
Extra: map[string]interface{}{
|
||||
"ID": "id-1",
|
||||
},
|
||||
})
|
||||
ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Name: archive,
|
||||
Path: file,
|
||||
Type: artifact.UploadableArchive,
|
||||
Extra: map[string]interface{}{
|
||||
"ID": "id-2",
|
||||
},
|
||||
})
|
||||
ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Name: linuxPackage,
|
||||
Path: file,
|
||||
Type: artifact.LinuxPackage,
|
||||
Extra: map[string]interface{}{
|
||||
"ID": "id-3",
|
||||
},
|
||||
})
|
||||
require.NoError(t, Pipe{}.Run(ctx))
|
||||
var artifacts []string
|
||||
for _, a := range ctx.Artifacts.List() {
|
||||
artifacts = append(artifacts, a.Name)
|
||||
}
|
||||
require.Contains(t, artifacts, checksums, binary)
|
||||
bts, err := ioutil.ReadFile(filepath.Join(folder, checksums))
|
||||
require.NoError(t, err)
|
||||
for _, want := range tt.want {
|
||||
require.Contains(t, string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc "+want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPipeSkipTrue(t *testing.T) {
|
||||
|
@ -473,9 +473,10 @@ type Snapshot struct {
|
||||
|
||||
// Checksum config.
|
||||
type Checksum struct {
|
||||
NameTemplate string `yaml:"name_template,omitempty"`
|
||||
Algorithm string `yaml:"algorithm,omitempty"`
|
||||
Disable bool `yaml:"disable,omitempty"`
|
||||
NameTemplate string `yaml:"name_template,omitempty"`
|
||||
Algorithm string `yaml:"algorithm,omitempty"`
|
||||
IDs []string `yaml:"ids,omitempty"`
|
||||
Disable bool `yaml:"disable,omitempty"`
|
||||
}
|
||||
|
||||
// Docker image config.
|
||||
|
@ -19,6 +19,14 @@ checksum:
|
||||
# Default is sha256.
|
||||
algorithm: sha256
|
||||
|
||||
# IDs of artifacts to include in the checksums file.
|
||||
# If left empty, all published binaries, archives, linux packages and source archives
|
||||
# are included in the checksums file.
|
||||
# Default is an empty list.
|
||||
ids:
|
||||
- foo
|
||||
- bar
|
||||
|
||||
# Disable the generation/upload of the checksum file.
|
||||
# Default is false.
|
||||
disable: true
|
||||
|
Loading…
x
Reference in New Issue
Block a user