-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
243 lines (228 loc) · 5.63 KB
/
publish.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"context"
"dagger/toolbox-fedora/internal/dagger"
"fmt"
"strings"
)
// publish builds and publishes the Fedora Atomic container image
func (ft *FedoraToolbox) publish(
ctx context.Context,
// registry url, e.g. ghcr.io
registry string,
// name of the image
imageName string,
// additional tags to publish in addition to the default tags
// default tags will be included unless skipDefaultTags is set:
// [majorVersion, majorVersion-date, date]
// +optional
additionalTags []string,
// registry username
// also used as the registry namespace
username string,
// registry auth password/secret
// +optional
secret *dagger.Secret,
// skip namespacing registry with username
// example:
// registry=ghcr.io username=foo
// default: => ghcr.io/foo/image
// skip: => ghcr.io/image
// +optional
// +default=false
skipRegistryNamespace bool,
// skip adding default tags
// +optional
// +default=false
skipDefaultTags bool,
// if true the "latest" tag will be published
// +optional
// +default=false
latest bool,
) (*FedoraToolbox, error) {
ctr, err := ft.Container(ctx)
if err != nil {
return nil, err
}
if secret != nil {
// NOTE: the auth step MUST be bare registry w/o username namespace
ctr = ctr.WithRegistryAuth(registry, username, secret)
}
if !skipRegistryNamespace {
registry = strings.ToLower(fmt.Sprintf("%s/%s", registry, username))
}
ctr = ctr.WithLabel("org.opencontainers.image.title", imageName)
tags := additionalTags
if !skipDefaultTags {
tags = append(tags, ft.ReleaseVersion)
}
if latest {
tags = append(tags, "latest")
}
for _, tag := range tags {
digest, err := ctr.Publish(
ctx,
fmt.Sprintf("%s/%s:%s", registry, imageName, tag),
)
if err != nil {
return nil, err
}
ft.Digests = append(ft.Digests, digest)
}
return ft, nil
}
// Publish build and publish the Fedora atomic container image
func (ft *FedoraToolbox) Publish(
ctx context.Context,
// registry url, e.g. ghcr.io
registry string,
// name of the image
imageName string,
// additional tags to publish in addition to the default tags
// default tags will be included unless skipDefaultTags is set:
// [majorVersion, majorVersion-date, date]
// +optional
additionalTags []string,
// registry username
// also used as the registry namespace
username string,
// registry auth password/secret
// +optional
secret *dagger.Secret,
// skip opinionated ublue-way of setting up signing config
// note: if basing off of ublue, this is already setup,
// but not for the source image
// +optional
// +default=false
skipSigningConfig bool,
// skip namespacing registry with username
// example:
// registry=ghcr.io username=foo
// default: => ghcr.io/foo/image
// skip: => ghcr.io/image
// +optional
// +default=false
skipRegistryNamespace bool,
// skip adding default tags
// +optional
// +default=false
skipDefaultTags bool,
// if true the "latest" tag will be published
// +optional
// +default=false
latest bool,
) ([]string, error) {
_, err := ft.publish(
ctx,
registry,
imageName,
additionalTags,
username,
secret,
skipRegistryNamespace,
skipDefaultTags,
latest,
)
if err != nil {
return nil, err
}
return ft.Digests, nil
}
// PublishAndSign build, publish, and sign (via cosign)
// the Fedora container image
func (ft *FedoraToolbox) PublishAndSign(
ctx context.Context,
// registry url, e.g. ghcr.io
registry string,
// name of the image
imageName string,
// additional tags to publish in addition to the default tags
// default tags will be included unless skipDefaultTags is set:
// [majorVersion, majorVersion-date, date]
// +optional
additionalTags []string,
// registry username
// also used as the registry namespace
username string,
// registry auth password/secret
// +optional
secret *dagger.Secret,
// skip opinionated ublue-way of setting up signing config
// note: if basing off of ublue, this is already setup,
// but not for the source image
// +optional
// +default=false
skipSigningConfig bool,
// skip namespacing registry with username
// example:
// registry=ghcr.io username=foo
// default: => ghcr.io/foo/image
// skip: => ghcr.io/image
// +optional
// +default=false
skipRegistryNamespace bool,
// skip adding default tags
// +optional
// +default=false
skipDefaultTags bool,
// Cosign private key
cosignPrivateKey dagger.Secret,
// Cosign password
cosignPassword dagger.Secret,
// Docker config
//+optional
dockerConfig *dagger.File,
// Cosign container image to be used to sign the digests
// +optional
// +default="chainguard/cosign:latest"
cosignImage *string,
// Cosign container image user
// +optional
// +default="nonroot"
cosignUser *string,
// if true the "latest" tag will be published
// +optional
// +default=false
latest bool,
) ([]string, error) {
_, err := ft.publish(
ctx,
registry,
imageName,
additionalTags,
username,
secret,
skipRegistryNamespace,
skipDefaultTags,
latest,
)
if err != nil {
return nil, err
}
opts := dagger.CosignSignOpts{
// Should never be nil due to Dagger setting default values
CosignImage: *cosignImage,
CosignUser: *cosignUser,
}
if secret != nil {
opts.RegistryUsername = username
opts.RegistryPassword = secret
}
if dockerConfig != nil {
opts.DockerConfig = dockerConfig
}
cosignStdout, err := dag.Cosign().Sign(
ctx,
&cosignPrivateKey,
&cosignPassword,
ft.Digests,
opts,
)
if err != nil {
return nil, err
}
output := append([]string{"Published:"}, ft.Digests...)
output = append(output, "")
output = append(output, cosignStdout...)
return output, nil
}