/
manifest.go
145 lines (129 loc) · 3.97 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package docker
import (
"fmt"
"sort"
"strings"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/ids"
"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 an implementation for the docker manifest feature,
// allowing to publish multi-arch docker images.
type ManifestPipe struct{}
func (ManifestPipe) String() string { return "docker manifests" }
func (ManifestPipe) Skip(ctx *context.Context) bool {
return len(ctx.Config.DockerManifests) == 0 || ctx.SkipDocker
}
// Default sets the pipe defaults.
func (ManifestPipe) Default(ctx *context.Context) error {
ids := ids.New("docker_manifests")
for i := range ctx.Config.DockerManifests {
manifest := &ctx.Config.DockerManifests[i]
if manifest.ID != "" {
ids.Inc(manifest.ID)
}
if manifest.Use == "" {
manifest.Use = useDocker
}
if err := validateManifester(manifest.Use); err != nil {
return err
}
}
return ids.Validate()
}
// Publish the docker manifests.
func (ManifestPipe) Publish(ctx *context.Context) error {
g := semerrgroup.NewSkipAware(semerrgroup.New(1))
for _, manifest := range ctx.Config.DockerManifests {
manifest := manifest
g.Go(func() error {
if strings.TrimSpace(manifest.SkipPush) == "true" {
return pipe.Skip("docker_manifest.skip_push is set")
}
if strings.TrimSpace(manifest.SkipPush) == "auto" && ctx.Semver.Prerelease != "" {
return pipe.Skip("prerelease detected with 'auto' push, skipping docker manifest")
}
name, err := manifestName(ctx, manifest)
if err != nil {
return err
}
images, err := manifestImages(ctx, manifest)
if err != nil {
return err
}
manifester := manifesters[manifest.Use]
log.WithField("manifest", name).WithField("images", images).Info("creating")
if err := manifester.Create(ctx, name, images, manifest.CreateFlags); err != nil {
return err
}
art := &artifact.Artifact{
Type: artifact.DockerManifest,
Name: name,
Path: name,
Extra: map[string]interface{}{},
}
if manifest.ID != "" {
art.Extra[artifact.ExtraID] = manifest.ID
}
ctx.Artifacts.Add(art)
log.WithField("manifest", name).Info("pushing")
return manifester.Push(ctx, name, manifest.PushFlags)
})
}
return g.Wait()
}
func validateManifester(use string) error {
valid := make([]string, 0, len(manifesters))
for k := range manifesters {
valid = append(valid, k)
}
for _, s := range valid {
if s == use {
return nil
}
}
sort.Strings(valid)
return fmt.Errorf("docker manifest: invalid use: %s, valid options are %v", use, valid)
}
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) {
artifacts := ctx.Artifacts.Filter(artifact.ByType(artifact.DockerImage)).List()
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, withDigest(str, artifacts))
}
if strings.TrimSpace(strings.Join(manifest.ImageTemplates, "")) == "" {
return imgs, pipe.Skip("manifest has no images")
}
return imgs, nil
}
func withDigest(name string, images []*artifact.Artifact) string {
for _, art := range images {
if art.Name == name {
if digest := artifact.ExtraOr(*art, artifact.ExtraDigest, ""); digest != "" {
return name + "@" + digest
}
break
}
}
log.Warnf("did not find the digest for %s, defaulting to insecure mode")
return name
}