/
magefile.go
178 lines (155 loc) · 4.63 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
// ⚡ Core Mage Tasks.
package main
import (
"os"
"os/exec"
"path/filepath"
"github.com/DelineaXPM/dsv-github-action/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/req"
"github.com/sheldonhull/magetools/tooling"
// mage:import
"github.com/sheldonhull/magetools/gittools"
// mage:import
"github.com/sheldonhull/magetools/gotools"
// mage:import
"github.com/sheldonhull/magetools/precommit"
//mage:import
_ "github.com/sheldonhull/magetools/secrets"
"github.com/bitfield/script"
)
// Test contains mage tasks for testing.
type Test mg.Namespace
// createDirectories creates the local working directories for build artifacts and tooling.
func createDirectories() error {
for _, dir := range []string{constants.ArtifactDirectory, constants.CacheDirectory} {
if err := os.MkdirAll(dir, constants.PermissionUserReadWriteExecute); err != nil {
pterm.Error.Printf("failed to create dir: [%s] with error: %v\n", dir, err)
return err
}
pterm.Success.Printf("✅ [%s] dir created\n", dir)
}
return nil
}
// Init runs multiple tasks to initialize all the requirements for running a project for a new contributor.
func Init() error {
pterm.DefaultHeader.Println("running Init()")
mg.SerialDeps(
Clean,
createDirectories,
)
mg.Deps(
(gotools.Go{}.Tidy),
(InstallSyft),
)
if err := tooling.SilentInstallTools(CIToolList); err != nil {
return err
}
if ci.IsCI() {
pterm.Debug.Println("CI detected, done with init")
return nil
}
pterm.DefaultSection.Println("Setup Project Specific Tools")
if err := tooling.SilentInstallTools(ToolList); err != nil {
return err
}
// These can run in parallel as different toolchains.
mg.Deps(
(gotools.Go{}.Init),
(gittools.Gittools{}.Init),
(precommit.Precommit{}.Init),
(InstallTrunk),
)
return nil
}
// InstallTrunk installs trunk.io tooling.
func InstallTrunk() error {
_, err := script.Exec("curl https://get.trunk.io -fsSL").Exec("bash").Stdout()
if err != nil {
return err
}
return nil
}
// InstallSyft installs SBOM tooling for goreleaser.
func InstallSyft() error {
_, err := script.Exec("curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh").Exec("sh -s -- -b /usr/local/bin").Stdout()
if err != nil {
return err
}
return nil
}
// Clean up after yourself.
func Clean() {
pterm.Success.Println("Cleaning...")
for _, dir := range []string{constants.ArtifactDirectory, constants.CacheDirectory} {
err := os.RemoveAll(dir)
if err != nil {
pterm.Error.Printf("failed to removeall: [%s] with error: %v\n", dir, err)
}
pterm.Success.Printf("🧹 [%s] dir removed\n", dir)
}
mg.Deps(createDirectories)
}
// ActIntegration runs local act cli to test the integration.
func (Test) ActIntegration() error {
binary, err := exec.LookPath("act")
if err != nil {
pterm.Error.Println("unable to resolve act as installed. please install via directions here: https://github.com/nektos/act#installation")
return err
}
return sh.RunV(
binary,
"--job", "integration",
"--secret-file", constants.SecretFile,
)
}
// Unit runs go unit tests.
func (Test) Unit() {
mg.Deps(gotools.Go{}.TestSum("./..."))
}
// PullImage pulls the latest docker image published to the hub for testing.
func PullImage() error {
if err := sh.RunV("docker", "pull", constants.DockerRegistryPathQualified+":"+constants.DockerTag); err != nil {
pterm.Error.Printfln("unable to pull docker image: %v", err)
return err
}
return nil
}
// TestIntegration runs local act cli to test the integration.
func (Test) Integration() error {
// Mg.Deps(PullImage).
currentDescribedTag, err := sh.Output("git", "describe", "--tags", "--abbrev=0")
if err != nil {
pterm.Error.Printfln("unable to retrieve tag: %v", err)
return err
}
pterm.Info.Printfln("using tag: %s", currentDescribedTag)
pterm.Info.Println("running docker now")
wd, err := os.Getwd()
if err != nil {
return err
}
return sh.RunV(
"docker",
"run",
"--env-file", constants.SecretFile,
// NOTE: Optional way to invoke: "--env", "DSV_RETRIEVE="+"{"secretPath": "ci:tests:dsv-github-action", "secretKey": "secret-01", "outputVariable": "RETURN_VALUE_1"}",.
"--rm",
"-v", filepath.Join(wd, ".cache")+":"+"/app/.cache:rw",
constants.LocalDockerRegistryPathQualified+":"+currentDescribedTag,
)
}
func Changelog() error {
binary, err := req.ResolveBinaryByInstall("git-chglog", "github.com/git-chglog/git-chglog/cmd/git-chglog@latest")
if err != nil {
pterm.Error.Println("unable to install binary")
return err
}
return sh.RunV(
binary,
"--output", "CHANGELOG.md",
)
}