-
Notifications
You must be signed in to change notification settings - Fork 5
/
goreleaser.mage.custom.go
424 lines (387 loc) · 14.4 KB
/
goreleaser.mage.custom.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// This contains customized goreleaser tasks that take into account the GOOS and combine this with my standard approach of using changelog to drive the new semver release.
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/DelineaXPM/dsv-cli/magefiles/constants"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pterm/pterm"
"github.com/sheldonhull/magetools/ci"
"github.com/sheldonhull/magetools/pkg/magetoolsutils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// Build contains mage tasks specific to building without a release.
type (
Build mg.Namespace
// Release contains mage tasks specific to the release process, including upload of assets to s3, github, etc.
Release mg.Namespace
)
// checkEnv is the struct to pass into the checkEnvVar function to check and validate the environment variables.
// This builds a nice table summary when used to help summarize all the failed checks rather than doing this piecemeal.
type checkEnv struct {
Name string
IsSecret bool
IsRequired bool
Tbl pterm.TableData
Notes string
}
// checkEnvVar performs a check on environment variable and helps build a report summary of the failing conditions, missing variables, and bypasses logging if it's a secret.
// Yes this could be replaced by the `env` package but I had this in place and the output is nice for debugging so I left it. - Sheldon 😀
//
//nolint:unparam // ignoring as i'll want to use the values in the future, ok to leave for now.
func checkEnvVar(ckv checkEnv) (string, pterm.TableData, error) {
// loggedValue is used to make sure any secret isn't put into the table output.
var value, loggedValue string
var isSet bool
tbl := ckv.Tbl
value, isSet = os.LookupEnv(ckv.Name)
if ckv.IsSecret {
loggedValue = "***** secret set, but not logged *****"
} else {
loggedValue = value
}
// Required but not set is an error condition to report back to the user.
if !isSet && ckv.IsRequired {
tbl = append(tbl, []string{"❌", ckv.Name, loggedValue, ckv.Notes})
return "", tbl, fmt.Errorf("%s is required and not set", ckv.Name)
}
// Required but not a terminating error, then just put as information different from success, and no error.
if !isSet && !ckv.IsRequired {
tbl = append(tbl, []string{"👉", ckv.Name, loggedValue, ckv.Notes})
return value, tbl, nil
}
if isSet {
tbl = append(tbl, []string{"✅", ckv.Name, loggedValue, ckv.Notes})
return value, tbl, nil
}
return "", tbl, fmt.Errorf("unknown error (no conditions were hit so it's a PEKAB issue 😁) with evaluation of: %s", ckv.Name)
}
// func checkEnvVar(envVar string, required bool) (string, error) { //nolint:unused // leaving this as will use in future releases
// envVarValue := os.Getenv(envVar)
// if envVarValue == "" && required {
// pterm.Error.Printfln(
// "%s is required and unable to proceed without this being provided. terminating task.",
// envVar,
// )
// return "", fmt.Errorf("%s is required", envVar)
// }
// if envVarValue == "" {
// pterm.Debug.Printfln(
// "checkEnvVar() found no value for: %q, however this is marked as optional, so not exiting task",
// envVar,
// )
// }
// pterm.Debug.Printfln("checkEnvVar() found value: %q=%q", envVar, envVarValue)
// return envVarValue, nil
// }
// 🔨 Single builds the project for the current platform.
func (Build) Single() error {
magetoolsutils.CheckPtermDebug()
releaserArgs := []string{
"build",
"--rm-dist",
"--snapshot",
"--single-target",
}
pterm.Debug.Printfln("goreleaser: %+v", releaserArgs)
return sh.RunV("goreleaser", releaserArgs...) // "--skip-announce",.
}
// 🔨 All builds all the binaries defined in the project, for all platforms. This includes Docker image generation but skips publish.
// If there is no additional platforms configured in the task, then basically this will just be the same as `mage build`.
func (Build) All() error {
magetoolsutils.CheckPtermDebug()
releaserArgs := []string{
"release",
"--snapshot",
"--rm-dist",
"--skip-publish",
}
pterm.Debug.Printfln("goreleaser: %+v", releaserArgs)
return sh.RunV("goreleaser", releaserArgs...)
// To pass in explicit version mapping, you can do this. I'm not using at this time.
// Return sh.RunWithV(map[string]string{
// "GORELEASER_CURRENT_TAG": "latest",
// }, binary, releaserArgs...)
}
// SnapcraftLogin logs into snapcraft so Goreleaser can publish the snap.
//
// Requires environment variable: `SNAPCRAFT_STORE_CREDENTIALS`.
//
// To generate this file: `snapcraft export-login snapcraft-login`.
func (Release) SnapcraftLogin() error {
magetoolsutils.CheckPtermDebug()
if os.Getenv("SNAPCRAFT_STORE_CREDENTIALS") == "" {
if ci.IsCI() {
pterm.Error.Println("##vso[task.logissue type=error;]SNAPCRAFT_STORE_CREDENTIALS is a required environment variable to be able to run a release")
}
return fmt.Errorf("SNAPCRAFT_STORE_CREDENTIALS is a required environment variable to be able to run a release")
}
return sh.RunV("snapcraft", "login")
}
// 🔨 All generates a release with goreleaser. This does the whole shebang, including build, publish, and notify.
func (Release) All() error {
magetoolsutils.CheckPtermDebug()
// opting to always remove after running release to avoid possible non-snapshot artifact persisting.
defer func() {
err := sh.Rm(constants.TargetCLIVersionArtifact)
if err != nil {
pterm.Error.Printfln("error removing %s: %v", constants.TargetCLIVersionArtifact, err)
}
}()
// TODO: this will be checked once we publish cli to github
// if _, err := checkEnvVar("DOCKER_ORG", true); err != nil {
// return err
// }
// Run any dependent tasks first.
mg.SerialDeps(Release{}.GenerateCLIVersionFile) // Allowing it to run first as an uploaded artifact so even if failure to s3, the github json asset file is still available.
releaseVersion, err := sh.Output("changie", "latest")
if err != nil {
pterm.Error.Printfln("changie pulling latest release note version failure: %v", err)
return err
}
cleanVersion := strings.TrimSpace(releaseVersion)
cleanpath := filepath.Join(".changes", cleanVersion+".md")
if os.Getenv("GITHUB_WORKSPACE") != "" {
cleanpath = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".changes", cleanVersion+".md")
}
// NOTE: Merging all of this into a single goreleaser, not per-platform anymore.
releaserArgs := []string{
"release",
"--rm-dist",
"--skip-validate",
fmt.Sprintf("--release-notes=%s", cleanpath),
}
pterm.Debug.Printfln("goreleaser: %+v", releaserArgs)
if err := sh.RunWithV(map[string]string{
"GORELEASER_CURRENT_TAG": cleanVersion,
},
"goreleaser",
releaserArgs...,
); err != nil {
return err
}
mg.SerialDeps(Release{}.UploadCLIVersion)
pterm.Println("(Release).All() completed successfully")
return nil
}
// 🔨 Test runs release but with snapshot enabled and skips any type of remote push.
func (Release) Test() error {
magetoolsutils.CheckPtermDebug()
defer func() {
err := sh.Rm(constants.TargetCLIVersionArtifact)
if err != nil {
pterm.Error.Printfln("error removing %s: %v", constants.TargetCLIVersionArtifact, err)
}
}()
// TODO: this will be checked once we publish cli to github
// if _, err := checkEnvVar("DOCKER_ORG", true); err != nil {
// return err
// }
// Run any dependent tasks first.
mg.SerialDeps(Release{}.GenerateCLIVersionFile)
releaseVersion, err := sh.Output("changie", "latest")
if err != nil {
pterm.Error.Printfln("changie pulling latest release note version failure: %v", err)
return err
}
cleanVersion := strings.TrimSpace(releaseVersion)
cleanpath := filepath.Join(".changes", cleanVersion+".md")
if os.Getenv("GITHUB_WORKSPACE") != "" {
cleanpath = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".changes", cleanVersion+".md")
}
// NOTE: Merging all of this into a single goreleaser, not per-platform anymore.
releaserArgs := []string{
"release",
"--rm-dist",
"--skip-validate",
"--snapshot",
"--skip-publish",
"--skip-sign",
"--skip-sbom",
fmt.Sprintf("--release-notes=%s", cleanpath),
}
pterm.Debug.Printfln("goreleaser: %+v", releaserArgs)
if err := sh.RunWithV(map[string]string{
"GORELEASER_CURRENT_TAG": cleanVersion,
},
"goreleaser",
releaserArgs...,
); err != nil {
return err
}
pterm.Println("(Release).Test() completed successfully")
return nil
}
// getGoreleaserConfig returns the path to the goreleaser config file based on the current OS.
func getGoreleaserConfig() (string, error) {
magetoolsutils.CheckPtermDebug()
var configfile string
switch runtime.GOOS {
case "darwin":
configfile = ".goreleaser.darwin.yaml"
case "linux":
configfile = ".goreleaser.linux.yaml"
case "windows": //nolint:goconst // windows is used multiple times but not appropriate to be a constant
configfile = ".goreleaser.windows.yaml"
default:
pterm.Error.Printfln("Unsupported OS: %s", runtime.GOOS)
return "", fmt.Errorf("unsupported platform: %s", runtime.GOOS)
}
pterm.Info.Printfln("using config file: %s", configfile)
return configfile, nil
}
// GenerateCLIVersionFile generates a json object with an array of the containing a list of all the artifact versions and their links based on our standard download url.
func (Release) GenerateCLIVersionFile() error {
magetoolsutils.CheckPtermDebug()
releaseVersion, _, err := getVersion()
if err != nil {
return err
}
// Links is the url for all the assets published.
//nolint:tagliatelle // this is specifically what the CLI requires.
type Links struct {
DarwinAmd64 string `json:"darwin/amd64"`
DarwinArm64 string `json:"darwin/arm64"`
LinuxAmd64 string `json:"linux/amd64"`
Linux386 string `json:"linux/386"`
WindowsAmd64 string `json:"windows/amd64"`
Windows386 string `json:"windows/386"`
}
// cliVersions is the struct that will be turned into a json file.
type cliVersions struct {
Latest string `json:"latest"`
Links Links `json:"links"`
}
ver := strings.TrimPrefix(releaseVersion, "v")
newJSON := cliVersions{
Latest: ver,
Links: Links{
DarwinAmd64: fmt.Sprintf(constants.DownloadURLFString, ver, "dsv-darwin-x64"),
DarwinArm64: fmt.Sprintf(constants.DownloadURLFString, ver, "dsv-darwin-arm64"),
LinuxAmd64: fmt.Sprintf(constants.DownloadURLFString, ver, "dsv-linux-x64"),
Linux386: fmt.Sprintf(constants.DownloadURLFString, ver, "dsv-linux-x86"),
WindowsAmd64: fmt.Sprintf(constants.DownloadURLFString, ver, "dsv-win-x64.exe"),
Windows386: fmt.Sprintf(constants.DownloadURLFString, ver, "dsv-win-x86.exe"),
},
}
if err := os.MkdirAll(constants.ArtifactDirectory, constants.PermissionUserReadWriteExecute); err != nil {
return err
}
// Write the json file.
jf, err := os.Create(constants.TargetCLIVersionArtifact)
if err != nil {
pterm.Error.Printfln("error creating json file: %v", err)
return err
}
b, err := json.MarshalIndent(newJSON, "", " ")
if err != nil {
pterm.Error.Printfln("error marshaling json: %v", err)
return err
}
if _, err := jf.Write(b); err != nil {
pterm.Error.Printfln("error writing json file: %v", err)
return err
}
pterm.Success.Printfln("json file written: %s", jf.Name())
return nil
}
// getVersion returns the version and path for the changefile to use for the semver and release notes.
//
//nolint:unparam // this is a helper function, i'm ok leaving the additional parameter in there for now. - Sheldon 2022-12-15
func getVersion() (releaseVersion, cleanPath string, err error) {
releaseVersion, err = sh.Output("changie", "latest")
if err != nil {
pterm.Error.Printfln("changie pulling latest release note version failure: %v", err)
return "", "", err
}
cleanVersion := strings.TrimSpace(releaseVersion)
cleanPath = filepath.Join(".changes", cleanVersion+".md")
if os.Getenv("GITHUB_WORKSPACE") != "" {
cleanPath = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".changes", cleanVersion+".md")
}
return cleanVersion, cleanPath, nil
}
// UploadCLIVersion uploads the cli-versions.json file to the secrets s3 bucket.
func (Release) UploadCLIVersion() error {
// BucketInQuestion contains S3Client, an Amazon S3 service client that is used to perform bucket
// and object actions.
//
// Example from aws https://github.com/awsdocs/aws-doc-sdk-examples/blob/f45333bde292926451ba626e17be1c6a49c037f6/gov2/s3/actions/bucket_basics.go#LL103-L120
mg.Deps(Release{}.GenerateCLIVersionFile)
type BucketInQuestion struct {
S3Client *s3.Client
}
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(constants.AWSDefaultS3Region))
if err != nil {
return err
}
bucket := BucketInQuestion{
S3Client: s3.NewFromConfig(cfg),
}
file, err := os.Open(constants.TargetCLIVersionArtifact)
if err != nil {
pterm.Error.Printfln("Couldn't open file %v to upload. Here's why: %v", constants.TargetCLIVersionArtifact, err)
return err
} else {
defer file.Close()
_, err := bucket.S3Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(os.Getenv("S3_BUCKET")),
Key: aws.String(constants.S3CLIVersionPath),
Body: file,
})
if err != nil {
pterm.Error.Printfln("Couldn't upload file %v to %v:%v. Here's why: %v",
constants.TargetCLIVersionArtifact,
os.Getenv("S3_BUCKET"), constants.S3CLIVersionPath,
err,
)
return err
}
}
pterm.Success.Printfln("(Release) successfully uploaded file %v to %v:%v",
constants.TargetCLIVersionArtifact,
os.Getenv("S3_BUCKET"), constants.S3CLIVersionPath,
)
return nil
}
// 📦 Bump the application as an interactive command, prompting for semver change type, merging changelog, and running format and git add.
func (Release) Bump() error {
if err := sh.RunV("changie", "batch", "auto"); err != nil {
pterm.Warning.Printf("changie batch failure (non-terminating as might be repeating batch command): %v", err)
}
if err := sh.RunV("changie", "merge"); err != nil {
return err
}
if err := sh.RunV("trunk", "fmt"); err != nil {
return err
}
if err := sh.RunV("git", "add", ".changes/*"); err != nil {
return err
}
if err := sh.RunV("git", "add", "CHANGELOG.md"); err != nil {
return err
}
releaseVersion, _, err := getVersion()
if err != nil {
return err
}
response, err := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultText(fmt.Sprintf("feat: 🚀 release %s", releaseVersion)).Show()
if err != nil {
return err
}
if err := sh.RunV("git", "commit", "-m", response); err != nil {
return err
}
return nil
}