Skip to content

Commit

Permalink
feat: output checksums to artifacts info (#3548)
Browse files Browse the repository at this point in the history
Following #3540, output artifacts' checksums to the artifact info.
This addition makes it easier to consume the checksums, especially when
running from e.g. GitHub Actions.

New tests:
1. Add a check for the checksum in the extra field. 
2. Add a test for every checksum algorithm (to see that it doesn't break
for any algo's output).
3. Add a case of a binary and an extra file (to see that the logic
doesn't break when there's a mix).

p.s.
While working on that, I noticed that the convention for extra fields is
actually to use UpperCamelCase rather than lower.
I was mistaken because I looked at the subfields of the "DockerConfig"
extra field.
I think it's a good idea to fix it quickly, before the next release
rolls and it becomes a compatibility issue.
I took the liberty to fix it here as an extra commit. Please let me know
if you want it to be in a separate PR.

---
Tests:
```
go test
  • refreshing checksums                             file=binary_bar_checksums.txt
  • refreshing checksums                             file=binary_bar_checksums.txt
  • refreshing checksums                             file=binary_bar_checksums.txt
PASS
ok  	github.com/goreleaser/goreleaser/internal/pipe/checksums	0.184s
```

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
gal-legit and caarlos0 committed Nov 14, 2022
1 parent 6df90e5 commit 76dc0b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
18 changes: 14 additions & 4 deletions internal/pipe/checksums/checksums.go
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/goreleaser/goreleaser/pkg/context"
)

const (
artifactChecksumExtra = "Checksum"
)

var (
errNoArtifacts = errors.New("there are no artifacts to sign")
lock sync.Mutex
Expand Down Expand Up @@ -137,11 +141,17 @@ func refresh(ctx *context.Context, filepath string) error {
return err
}

func checksums(algorithm string, artifact *artifact.Artifact) (string, error) {
log.WithField("file", artifact.Name).Debug("checksumming")
sha, err := artifact.Checksum(algorithm)
func checksums(algorithm string, a *artifact.Artifact) (string, error) {
log.WithField("file", a.Name).Debug("checksumming")
sha, err := a.Checksum(algorithm)
if err != nil {
return "", err
}
return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil

if a.Extra == nil {
a.Extra = make(artifact.Extras)
}
a.Extra[artifactChecksumExtra] = fmt.Sprintf("%s:%s", algorithm, sha)

return fmt.Sprintf("%v %v\n", sha, a.Name), nil
}
13 changes: 13 additions & 0 deletions internal/pipe/checksums/checksums_test.go
Expand Up @@ -344,6 +344,19 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) {
require.Contains(t, string(bts), "3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 "+want)
}
}

_ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error {
if a.Path != file {
return nil
}
if len(tt.ids) > 0 {
return nil
}
checkSum, err := artifact.Extra[string](*a, artifactChecksumExtra)
require.Nil(t, err)
require.NotEmptyf(t, checkSum, "failed: %v", a.Path)
return nil
})
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/docker/docker.go
Expand Up @@ -20,7 +20,7 @@ import (

const (
dockerConfigExtra = "DockerConfig"
dockerDigestExtra = "digest"
dockerDigestExtra = "Digest"

useBuildx = "buildx"
useDocker = "docker"
Expand Down

0 comments on commit 76dc0b5

Please sign in to comment.