1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

feat: store artifact list into dist/artifacts.json (#2715)

* feat: store artifact list into dist/artifacts.json

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* chore: fmt

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-12-01 09:54:58 -03:00 committed by GitHub
parent 535a383435
commit ecb800aef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 95 additions and 9 deletions

View File

@ -107,7 +107,7 @@ func buildProject(options buildOpts) (*context.Context, error) {
return nil, err
}
return ctx, ctrlc.Default.Run(ctx, func() error {
for _, pipe := range pipeline.BuildPipeline {
for _, pipe := range pipeline.BuildCmdPipeline {
if err := skip.Maybe(
pipe,
logging.Log(

View File

@ -99,6 +99,10 @@ func (t Type) String() string {
}
}
func (t Type) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t)), nil
}
const (
ExtraID = "ID"
ExtraBinary = "Binary"
@ -111,14 +115,14 @@ const (
// Artifact represents an artifact and its relevant info.
type Artifact struct {
Name string
Path string
Goos string
Goarch string
Goarm string
Gomips string
Type Type
Extra map[string]interface{}
Name string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
Goos string `json:"goos,omitempty"`
Goarch string `json:"goarch,omitempty"`
Goarm string `json:"goarm,omitempty"`
Gomips string `json:"gomips,omitempty"`
Type Type `json:"type,omitempty"`
Extra map[string]interface{} `json:"extra,omitempty"`
}
// ExtraOr returns the Extra field with the given key or the or value specified

View File

@ -367,10 +367,16 @@ func TestTypeToString(t *testing.T) {
} {
t.Run(a.String(), func(t *testing.T) {
require.NotEqual(t, "unknown", a.String())
bts, err := a.MarshalJSON()
require.NoError(t, err)
require.Equal(t, []byte(`"`+a.String()+`"`), bts)
})
}
t.Run("unknown", func(t *testing.T) {
require.Equal(t, "unknown", Type(9999).String())
bts, err := Type(9999).MarshalJSON()
require.NoError(t, err)
require.Equal(t, []byte(`"unknown"`), bts)
})
}

View File

@ -49,6 +49,13 @@ func RequireEqualNakedYaml(tb testing.TB, out []byte) {
doRequireEqual(tb, out, ".yaml", "")
}
func RequireReadFile(tb testing.TB, path string) []byte {
tb.Helper()
bts, err := os.ReadFile(path)
require.NoError(tb, err)
return bts
}
func doRequireEqual(tb testing.TB, out []byte, ext, suffix string) {
tb.Helper()

View File

@ -0,0 +1,28 @@
// Package artifacts provides the pipe implementation that creates a artifacts.json file in the dist folder.
package artifacts
import (
"encoding/json"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe implementation.
type Pipe struct{}
func (Pipe) String() string { return "storing artifact list" }
func (Pipe) Skip(ctx *context.Context) bool { return false }
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
bts, err := json.Marshal(ctx.Artifacts.List())
if err != nil {
return err
}
path := filepath.Join(ctx.Config.Dist, "artifacts.json")
log.Log.WithField("file", path).Info("writing")
return os.WriteFile(path, bts, 0o600)
}

View File

@ -0,0 +1,34 @@
package artifacts
import (
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestArtifacts(t *testing.T) {
tmp := t.TempDir()
ctx := context.New(config.Project{
Dist: tmp,
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo",
Path: "foo.txt",
Type: artifact.Binary,
Goos: "darwin",
Goarch: "amd64",
Goarm: "7",
Extra: map[string]interface{}{
"foo": "bar",
},
})
require.NoError(t, Pipe{}.Run(ctx))
golden.RequireEqualJSON(t, golden.RequireReadFile(t, filepath.Join(tmp, "artifacts.json")))
}

View File

@ -0,0 +1 @@
[{"name":"foo","path":"foo.txt","goos":"darwin","goarch":"amd64","goarm":"7","type":"Binary","extra":{"foo":"bar"}}]

View File

@ -6,6 +6,7 @@ import (
"github.com/goreleaser/goreleaser/internal/pipe/announce"
"github.com/goreleaser/goreleaser/internal/pipe/archive"
"github.com/goreleaser/goreleaser/internal/pipe/artifacts"
"github.com/goreleaser/goreleaser/internal/pipe/before"
"github.com/goreleaser/goreleaser/internal/pipe/brew"
"github.com/goreleaser/goreleaser/internal/pipe/build"
@ -58,6 +59,10 @@ var BuildPipeline = []Piper{
universalbinary.Pipe{}, // universal binary handling
}
// BuildCmdPipeline is the pipeline run by goreleaser build.
// nolint:gochecknoglobals
var BuildCmdPipeline = append(BuildPipeline, artifacts.Pipe{})
// Pipeline contains all pipe implementations in order.
// nolint: gochecknoglobals
var Pipeline = append(
@ -73,6 +78,7 @@ var Pipeline = append(
checksums.Pipe{}, // checksums of the files
sign.Pipe{}, // sign artifacts
docker.Pipe{}, // create and push docker images
artifacts.Pipe{}, // creates an artifacts.json in the dist folder
publish.Pipe{}, // publishes artifacts
announce.Pipe{}, // announce releases
)