-
Notifications
You must be signed in to change notification settings - Fork 5
/
magefile.go
287 lines (233 loc) · 5.58 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
// Copyright 2023 uwu-tools 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.
//
// SPDX-License-Identifier: Apache-2.0
//go:build mage
package main
import (
"errors"
"fmt"
"os"
"github.com/magefile/mage/sh"
"sigs.k8s.io/release-utils/mage"
"github.com/uwu-tools/magex/pkg"
)
// 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"
// Module variables.
orgName = "uwu-tools"
toolName = "gh-jira-issue-sync"
// Versions.
golangciVersion = "v1.50.1"
minGoVersion = "1.20"
// Environment variables.
envLDFLAGS = "GHJIRA_LDFLAGS"
envMinGoVersion = "MIN_GO_VERSION"
// Test variables.
coverMode = "atomic"
coverProfileFilename = "unit-coverage.out"
)
var (
moduleName = fmt.Sprintf(
"github.com/%s/%s",
orgName,
toolName,
)
)
// 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 {
// TODO(mage): Upstream should support setting covermode/coverprofile.
return sh.RunV(
"go",
"test",
"-v",
"-covermode", coverMode,
"-coverprofile", coverProfileFilename,
"./...",
)
}
// Verify runs repository verification scripts
func Verify() error {
fmt.Println("Ensuring mage is available...")
if err := pkg.EnsureMage(""); err != nil {
return err
}
// TODO(mage): Support verifying headers.
/*
fmt.Println("Running copyright header checks...")
if err := mage.VerifyBoilerplate("v0.2.5", binDir, boilerplateDir, false); err != nil {
return err
}
*/
// TODO(mage): Support verifying external dependencies.
/*
fmt.Println("Running external dependency checks...")
if err := mage.VerifyDeps("v0.3.0", "", "", true); err != nil {
return err
}
*/
// TODO(mage): Support verifying go module.
// TODO(mage): Upstream should support setting minimum version.
/*
os.Setenv(envMinGoVersion, minGoVersion)
fmt.Println("Running go module linter...")
if err := mage.VerifyGoMod(scriptDir); err != nil {
return err
}
*/
fmt.Println("Running golangci-lint...")
if err := mage.RunGolangCILint(golangciVersion, 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(envLDFLAGS, ldFlag)
if err := mage.VerifyBuild(scriptDir); err != nil {
return err
}
fmt.Println("Binaries available in the output directory.")
return nil
}
func BuildBinaries() error {
fmt.Println("Building binaries with goreleaser...")
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv(envLDFLAGS, 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(envLDFLAGS, ldFlag)
return sh.RunV(
"goreleaser",
"release",
"--clean",
"--snapshot",
"--skip-sign",
)
}
// BuildImages build 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(envLDFLAGS, 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,
moduleName,
)
}
// BuildImagesLocal build images locally and not push
func BuildImagesLocal() error {
fmt.Println("Building image with ko for local test...")
if err := mage.EnsureKO(""); err != nil {
return err
}
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv(envLDFLAGS, ldFlag)
os.Setenv("KOCACHE", "/tmp/ko")
return sh.RunV(
"ko",
"build",
"--bare",
"--local",
"--platform=linux/amd64",
moduleName,
)
}
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 Clean() {
fmt.Println("Cleaning workspace...")
toClean := []string{"output"}
for _, clean := range toClean {
sh.Rm(clean)
}
fmt.Println("Done.")
}
// getCommit gets the hash of the current commit
func getCommit() string {
commit, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return commit
}
// 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"
}