-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
manifest.go
113 lines (104 loc) · 3.38 KB
/
manifest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package docker
import (
"fmt"
"os/exec"
"strings"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
// ManifestPipe is beta implementation of for the docker manifest feature,
// allowing to publish multi-arch docker images.
type ManifestPipe struct{}
func (ManifestPipe) String() string {
return "docker manifests"
}
// Publish the docker manifests.
func (ManifestPipe) Publish(ctx *context.Context) error {
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
var g = semerrgroup.NewSkipAware(semerrgroup.New(ctx.Parallelism))
for _, manifest := range ctx.Config.DockerManifests {
manifest := manifest
g.Go(func() error {
name, err := manifestName(ctx, manifest)
if err != nil {
return err
}
images, err := manifestImages(ctx, manifest)
if err != nil {
return err
}
if err := dockerManifestCreate(ctx, name, images, manifest.CreateFlags); err != nil {
return err
}
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.DockerManifest,
Name: name,
Path: name,
})
return dockerManifestPush(ctx, name, manifest.PushFlags)
})
}
return g.Wait()
}
func manifestName(ctx *context.Context, manifest config.DockerManifest) (string, error) {
name, err := tmpl.New(ctx).Apply(manifest.NameTemplate)
if err != nil {
return name, err
}
if strings.TrimSpace(name) == "" {
return name, pipe.Skip("manifest name is empty")
}
return name, nil
}
func manifestImages(ctx *context.Context, manifest config.DockerManifest) ([]string, error) {
var imgs = make([]string, 0, len(manifest.ImageTemplates))
for _, img := range manifest.ImageTemplates {
str, err := tmpl.New(ctx).Apply(img)
if err != nil {
return []string{}, err
}
imgs = append(imgs, str)
}
if strings.TrimSpace(strings.Join(manifest.ImageTemplates, "")) == "" {
return imgs, pipe.Skip("manifest has no images")
}
return imgs, nil
}
func dockerManifestCreate(ctx *context.Context, manifest string, images, flags []string) error {
log.WithField("manifest", manifest).Info("creating docker manifest")
var args = []string{"manifest", "create", manifest}
for _, img := range images {
args = append(args, "--amend", img)
}
args = append(args, flags...)
/* #nosec */
var cmd = exec.CommandContext(ctx, "docker", args...)
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to create docker manifest: %s: \n%s: %w", manifest, string(out), err)
}
log.Debugf("docker manifest output: \n%s", string(out))
return nil
}
func dockerManifestPush(ctx *context.Context, manifest string, flags []string) error {
log.WithField("manifest", manifest).Info("pushing docker manifest")
var args = []string{"manifest", "push", manifest}
args = append(args, flags...)
/* #nosec */
var cmd = exec.CommandContext(ctx, "docker", args...)
log.WithField("cmd", cmd.Args).WithField("cwd", cmd.Dir).Debug("running")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to push docker manifest: %s: \n%s: %w", manifest, string(out), err)
}
log.Debugf("docker manifest output: \n%s", string(out))
return nil
}