forked from kubernetes-sigs/bom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
300 lines (240 loc) · 6.9 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
//go:build mage
// +build mage
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/pkg/archive"
"github.com/carolynvs/magex/pkg/downloads"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
"sigs.k8s.io/release-utils/mage"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
var Default = Verify
const (
binDir = "bin"
scriptDir = "scripts"
)
var boilerplateDir = filepath.Join(scriptDir, "boilerplate")
// All runs all targets for this repository
func All() error {
if err := Verify(); err != nil {
return err
}
if err := Test(); err != nil {
return err
}
return nil
}
// Test runs various test functions
func Test() error {
if err := mage.TestGo(true); err != nil {
return err
}
return nil
}
// Verify runs repository verification scripts
func Verify() error {
fmt.Println("Ensuring mage is available...")
if err := pkg.EnsureMage(""); err != nil {
return err
}
fmt.Println("Running copyright header checks...")
if err := mage.VerifyBoilerplate("v0.2.5", binDir, boilerplateDir, false); err != nil {
return err
}
fmt.Println("Running external dependency checks...")
if err := mage.VerifyDeps("v0.3.0", "", "", true); err != nil {
return err
}
fmt.Println("Running go module linter...")
if err := mage.VerifyGoMod(scriptDir); err != nil {
return err
}
fmt.Println("Running golangci-lint...")
if err := mage.RunGolangCILint("v1.46.2", false); err != nil {
return err
}
if err := Build(); err != nil {
return err
}
return nil
}
// Build runs go build
func Build() error {
fmt.Println("Running go build...")
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv("BOM_LDFLAGS", ldFlag)
if err := mage.VerifyBuild(scriptDir); err != nil {
return err
}
fmt.Println("Binaries available in the output directory.")
return nil
}
// BuildImages build bom image using ko
func BuildImages() error {
fmt.Println("Building images with ko...")
gitVersion := getVersion()
gitCommit := getCommit()
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv("BOM_LDFLAGS", ldFlag)
os.Setenv("KOCACHE", "/tmp/ko")
if os.Getenv("KO_DOCKER_REPO") == "" {
return errors.New("missing KO_DOCKER_REPO environment variable")
}
return sh.RunV("ko", "build", "--bare",
"--platform=all", "--tags", gitVersion, "--tags", gitCommit,
"sigs.k8s.io/bom/cmd/bom")
}
// BuildImagesLocal build images locally and not push
func BuildImagesLocal() error {
fmt.Println("Building image with ko for local test...")
if err := EnsureKO(""); err != nil {
return err
}
os.Setenv("BOM_LDFLAGS", generateLDFlags())
os.Setenv("KOCACHE", "/tmp/ko")
return sh.RunV("ko", "build", "--bare",
"--local", "--platform=linux/amd64",
"sigs.k8s.io/bom/cmd/bom")
}
func BuildStaging() error {
fmt.Println("Ensuring mage is available...")
if err := pkg.EnsureMage(""); err != nil {
return err
}
if err := EnsureKO(""); err != nil {
return err
}
if err := Build(); err != nil {
return errors.Wrap(err, "building the binaries")
}
if err := BuildImages(); err != nil {
return errors.Wrap(err, "building the images")
}
// if err := sh.RunV("cd", "output"); err != nil {
// return errors.Wrap(err, "cd into output directory")
// }
// if err := sh.RunV("./bom-linux-amd64", "output", "generate", "-f", "bom-darwin-amd64",
// "-f", "bom-darwin-arm64", "-f", "bom-linux-amd64", "-f", "bom-linux-arm64",
// "-f", "bom-windows-amd64.exe", "-d", "../", "-o", "bom-sbom.sdpx"); err != nil {
// return errors.Wrap(err, "generating the bom")
// }
return nil
}
func Clean() {
fmt.Println("Cleaning workspace...")
toClean := []string{"output"}
for _, clean := range toClean {
sh.Rm(clean)
}
fmt.Println("Done.")
}
// getVersion gets a description of the commit, e.g. v0.30.1 (latest) or v0.30.1-32-gfe72ff73 (canary)
func getVersion() string {
version, _ := sh.Output("git", "describe", "--tags", "--match=v*")
if version != "" {
return version
}
// repo without any tags in it
return "v0.0.0"
}
// getCommit gets the hash of the current commit
func getCommit() string {
commit, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return commit
}
// getGitState gets the state of the git repository
func getGitState() string {
_, err := sh.Output("git", "diff", "--quiet")
if err != nil {
return "dirty"
}
return "clean"
}
// getBuildDateTime gets the build date and time
func getBuildDateTime() string {
result, _ := sh.Output("git", "log", "-1", "--pretty=%ct")
if result != "" {
sourceDateEpoch := fmt.Sprintf("@%s", result)
date, _ := sh.Output("date", "-u", "-d", sourceDateEpoch, "+%Y-%m-%dT%H:%M:%SZ")
return date
}
date, _ := sh.Output("date", "+%Y-%m-%dT%H:%M:%SZ")
return date
}
func generateLDFlags() string {
pkg := "sigs.k8s.io/bom/pkg/version"
return fmt.Sprintf("-X %[1]s.GitVersion=%[2]s -X %[1]s.gitCommit=%[3]s -X %[1]s.gitTreeState=%[4]s -X %[1]s.buildDate=%[5]s", pkg, getVersion(), getCommit(), getGitState(), getBuildDateTime())
}
// Maybe we can move this to release-utils
func EnsureKO(version string) error {
versionToInstall := version
if versionToInstall == "" {
versionToInstall = "0.11.2"
}
fmt.Printf("Checking if `ko` version %s is installed\n", versionToInstall)
found, err := pkg.IsCommandAvailable("ko", versionToInstall, "version")
if err != nil {
return err
}
if !found {
fmt.Println("`ko` not found")
return InstallKO(versionToInstall)
}
fmt.Println("`ko` is installed!")
return nil
}
// Maybe we can move this to release-utils
func InstallKO(version string) error {
fmt.Println("Will install `ko`")
target := "ko"
if runtime.GOOS == "windows" {
target = "ko.exe"
}
opts := archive.DownloadArchiveOptions{
DownloadOptions: downloads.DownloadOptions{
UrlTemplate: "https://github.com/google/ko/releases/download/v{{.VERSION}}/ko_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}",
Name: "ko",
Version: version,
OsReplacement: map[string]string{
"darwin": "Darwin",
"linux": "Linux",
"windows": "Windows",
},
ArchReplacement: map[string]string{
"amd64": "x86_64",
},
},
ArchiveExtensions: map[string]string{
"linux": ".tar.gz",
"darwin": ".tar.gz",
"windows": ".tar.gz",
},
TargetFileTemplate: target,
}
return archive.DownloadToGopathBin(opts)
}