-
Notifications
You must be signed in to change notification settings - Fork 9
/
magefile.go
255 lines (196 loc) · 5.28 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
//go:build mage
// +build mage
/*
Copyright 2022 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 (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/sh"
"github.com/uwu-tools/magex/pkg"
"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 go module linter...")
if err := mage.VerifyGoMod(); err != nil {
return err
}
if err := Build(); err != nil {
return err
}
return nil
}
func GolangCILint() error {
fmt.Println("Running golangci-lint...")
if err := mage.RunGolangCILint("", false); 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("TEJOLOTE_LDFLAGS", ldFlag)
if err := mage.VerifyBuild(scriptDir); err != nil {
return err
}
fmt.Println("Binaries available in the output directory.")
return nil
}
// BuildImages build tejolote 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("TEJOLOTE_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/tejolote/cmd/tejolote")
}
// BuildImagesLocal build images locally and not push
func BuildImagesLocal() error {
fmt.Println("Building image with ko for local test...")
if err := mage.EnsureKO("0.13.0"); err != nil {
return err
}
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv("TEJOLOTE_LDFLAGS", ldFlag)
os.Setenv("KOCACHE", "/tmp/ko")
return sh.RunV("ko", "build", "--bare",
"--local", "--platform=linux/amd64",
"sigs.k8s.io/tejolote/cmd/tejolote")
}
func BuildStaging() error {
fmt.Println("Ensuring mage is available...")
if err := pkg.EnsureMage(""); err != nil {
return err
}
if err := mage.EnsureKO(""); err != nil {
return err
}
if err := BuildImages(); err != nil {
return fmt.Errorf("building the images: %w", err)
}
return nil
}
func BuildBinaries() error {
fmt.Println("Building binaries with goreleaser...")
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv("TEJOLOTE_LDFLAGS", ldFlag)
return sh.RunV("goreleaser", "release", "--clean")
}
func BuildBinariesSnapshot() error {
fmt.Println("Building binaries with goreleaser in snapshot mode...")
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv("TEJOLOTE_LDFLAGS", ldFlag)
return sh.RunV("goreleaser", "release", "--clean",
"--snapshot", "--skip=sign")
}
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", "--always")
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
}