forked from grafana/detect-angular-dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagefile.go
357 lines (313 loc) · 9.29 KB
/
Magefile.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
//go:build mage
package main
import (
"archive/zip"
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v53/github"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Build mg.Namespace
const (
distFolder = "dist"
artifactsFolder = "artifacts"
droneServerURL = "https://drone.grafana.net"
gitHubOrg = "grafana"
gitHubRepo = "detect-angular-dashboards"
droneRepo = gitHubOrg + "/" + gitHubRepo
)
// Go builds the go binary for the specified os and arch into dist/<os>_<arch>/detect-angular-dashboards.
func (Build) Go(goOs, goArch string) error {
fmt.Println("building for", goOs, goArch)
const programName = "detect-angular-dashboards"
args := []string{"build", "-o", filepath.Join(distFolder, goOs+"_"+goArch, programName)}
ldFlags := []string{"-s", "-w"}
const buildPkg = "github.com/grafana/detect-angular-dashboards/build"
if commitSha := gitCommitSha(); commitSha != "" {
// If commit sha was determined, add it to ldflags
ldFlags = append(ldFlags, fmt.Sprintf("-X %s.LinkerCommitSHA=%s", buildPkg, commitSha))
}
if droneTag := os.Getenv("DRONE_TAG"); droneTag != "" {
// Add drone tag as linker version
ldFlags = append(ldFlags, fmt.Sprintf("-X %s.LinkerVersion=%s", buildPkg, droneTag))
}
// Add all ldflags to args
if len(ldFlags) > 0 {
args = append(args, "-ldflags", strings.Join(ldFlags, " "))
}
// Run `go build` command
return sh.RunWithV(
map[string]string{
"CGO_ENABLED": "0",
"GOOS": goOs,
"GOARCH": goArch,
},
"go",
args...,
)
}
// Current builds the binary for the current os and arch.
func (b Build) Current() error {
return b.Go(runtime.GOOS, runtime.GOARCH)
}
// All builds all supported binaries into the dist folder.
func (b Build) All() error {
oses := []string{"linux", "darwin", "windows"}
archs := []string{"amd64", "arm64"}
var deps []interface{}
for _, os := range oses {
for _, arch := range archs {
deps = append(deps, mg.F(b.Go, os, arch))
}
}
mg.Deps(deps...)
return nil
}
// zipFolder zips all files in inFolder into the outFileName .zip file (which will be created).
func (b Build) zipFolder(inFolder string, outFileName string) error {
w, err := os.Create(outFileName)
if err != nil {
return fmt.Errorf("%q zip create: %w", outFileName, err)
}
defer w.Close()
zw := zip.NewWriter(w)
defer zw.Close()
if err := filepath.WalkDir(inFolder, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("%q open: %w", path, err)
}
defer f.Close()
relFn, err := filepath.Rel(inFolder, path)
if err != nil {
return fmt.Errorf("filepath rel %q: %w", path, err)
}
zfw, err := zw.Create(relFn)
if err != nil {
return fmt.Errorf("%q create: %w", relFn, err)
}
if _, err := io.Copy(zfw, f); err != nil {
return fmt.Errorf("io copy: %w", err)
}
return nil
}); err != nil {
return fmt.Errorf("walkdir: %w", err)
}
return nil
}
// Docker builds the docker image with the specified tag.
func (Build) Docker(tag string) error {
return sh.RunV("docker", "build", "-t", "detect-angular-dashboards:"+tag, ".")
}
// Package runs build:all and creates multiple .zip files inside dist/artifacts/<releaseName>, one for each folder in dist/*.
func Package(releaseName string) error {
var b Build
mg.Deps(b.All)
// Join all zip and general walkdir error
errs := make(chan error)
var finalErr error
go func() {
for err := range errs {
finalErr = errors.Join(finalErr, err)
}
}()
var wg sync.WaitGroup
errs <- filepath.WalkDir(distFolder, func(path string, d fs.DirEntry, err error) error {
// Skip dist folder (first call)
if path == distFolder {
return nil
}
// Recursively skip artifacts folder (output folder)
if d.Name() == artifactsFolder {
return filepath.SkipDir
}
if !d.IsDir() {
return nil
}
outFolder := filepath.Join(distFolder, artifactsFolder, releaseName)
if err := os.MkdirAll(outFolder, os.ModePerm); err != nil {
return fmt.Errorf("mkdir %q: %w", outFolder, err)
}
zipFn := filepath.Join(outFolder, fmt.Sprintf("%s_%s_%s.zip", gitHubRepo, releaseName, d.Name()))
wg.Add(1)
go func() {
fmt.Println("creating release package", zipFn)
if err := b.zipFolder(path, zipFn); err != nil {
errs <- fmt.Errorf("zip folder %q: %w", zipFn, err)
}
wg.Done()
}()
return nil
})
// Wait for everyone to terminate
wg.Wait()
close(errs)
return finalErr
}
// Clean deletes all the build binaries and artifacts from dist.
func Clean() error {
if err := os.RemoveAll(distFolder); err != nil {
return fmt.Errorf("removeall: %w", err)
}
if err := os.MkdirAll(distFolder, os.ModePerm); err != nil {
return fmt.Errorf("mkdirall: %w", err)
}
return nil
}
// Test runs the test suite.
func Test() error {
return sh.RunV("go", "test", "./...")
}
// Lint runs golangci-lint.
func Lint() error {
if err := sh.RunV("golangci-lint", "run", "./..."); err != nil {
return err
}
return nil
}
// Drone runs drone lint to ensure .drone.yml is valid and it signs the Drone configuration file.
// This needs to be run everytime the .drone.yml file is modified.
// See https://github.com/grafana/deployment_tools/blob/master/docs/infrastructure/drone/signing.md for more info
func Drone() error {
if err := sh.RunV("drone", "lint", "--trusted"); err != nil {
return err
}
if err := sh.RunV("drone", "--server", droneServerURL, "sign", "--save", droneRepo); err != nil {
return err
}
return nil
}
type GitHub mg.Namespace
// Release pushes a GitHub release
func (g GitHub) Release(releaseName string) error {
mg.Deps(mg.F(Package, releaseName))
// Determine files to upload
artifactsRoot := filepath.Join(distFolder, artifactsFolder, releaseName)
toUploadFileNames := map[string]struct{}{}
if err := filepath.WalkDir(artifactsRoot, func(path string, d fs.DirEntry, err error) error {
if path == artifactsRoot {
// Skip folder itself
return nil
}
if d.IsDir() {
// Do not recurse
return filepath.SkipDir
}
if filepath.Ext(d.Name()) != ".zip" {
// Skip non-zip files
return nil
}
toUploadFileNames[filepath.Join(artifactsRoot, d.Name())] = struct{}{}
return nil
}); err != nil {
return fmt.Errorf("walkdir: %w", err)
}
// Ensure we have files to attach to the release
if len(toUploadFileNames) == 0 {
return fmt.Errorf("could not find artifacts to upload for %q", releaseName)
}
// Check and get GitHub app env vars
var ghAppID, ghInstallationID int64
for _, o := range []struct {
dst *int64
envVar string
}{
{&ghAppID, "GITHUB_APP_ID"},
{&ghInstallationID, "GITHUB_APP_INSTALLATION_ID"},
} {
var err error
v := os.Getenv(o.envVar)
*o.dst, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("%q (value of env var %q) is not an integer", v, o.envVar)
}
}
// Create GitHub client
ghTransport, err := ghinstallation.New(http.DefaultTransport, ghAppID, ghInstallationID, []byte(os.Getenv("GITHUB_APP_PRIVATE_KEY")))
if err != nil {
return fmt.Errorf("ghinstallation new: %w", err)
}
ghClient := github.NewClient(&http.Client{Transport: ghTransport})
ctx, canc := context.WithTimeout(context.Background(), time.Minute*10)
defer canc()
// Create release
release, _, err := ghClient.Repositories.CreateRelease(ctx, gitHubOrg, gitHubRepo, &github.RepositoryRelease{
Name: github.String(releaseName),
TagName: github.String(releaseName),
Draft: github.Bool(false),
Prerelease: github.Bool(false),
MakeLatest: github.String("true"),
})
if err != nil {
return fmt.Errorf("create github release: %w", err)
}
fmt.Println("created github release", releaseName)
// Set up error handling
var finalErr error
errs := make(chan error)
go func() {
for err := range errs {
finalErr = errors.Join(finalErr, err)
}
}()
// Upload all artifacts and attach them to the release
var wg sync.WaitGroup
wg.Add(len(toUploadFileNames))
for fn := range toUploadFileNames {
fn := fn
go func() {
defer wg.Done()
fmt.Println("uploading", fn, "...")
f, err := os.Open(fn)
if err != nil {
errs <- fmt.Errorf("open %q: %w", fn, err)
return
}
defer func() {
if err := f.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
errs <- fmt.Errorf("close %q: %w", fn, err)
}
}()
if _, _, err := ghClient.Repositories.UploadReleaseAsset(ctx, gitHubOrg, gitHubRepo, *release.ID, &github.UploadOptions{
Name: filepath.Base(fn),
}, f); err != nil {
errs <- fmt.Errorf("upload release artifact %q: %w", fn, err)
return
}
fmt.Println("upload", fn, "ok!")
}()
}
// Wait for upload goroutines to finish
wg.Wait()
close(errs)
return finalErr
}
// gitCommitSha returns the git commit sha for the current repo or "" if none.
// It tries to get it from DRONE_COMMIT_SHA env var (set from drone).
// If it's not set, it invokes `git`.
// If it's not possible to run `git`, it returns an empty string.
func gitCommitSha() string {
// Try to get git commit sha, prioritize env var from drone
if commitSha := os.Getenv("DRONE_COMMIT_SHA"); commitSha != "" {
return commitSha
}
// If not possible, try invoking `git` command
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}