-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMagefile.go
143 lines (127 loc) · 3.32 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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
"github.com/Masterminds/semver/v3"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var distDir = "./bin"
func Clean() error {
return sh.Rm(distDir)
}
func buildBinary() error {
env := map[string]string{
"CGO_ENABLED": "0",
"GO111MODULE": "on",
"GOOS": getOrDefault("GOOS", "linux"),
}
if err := sh.RunWith(env, "go", "build", "-o", filepath.Join(distDir, "grafana-ntfy"), "./pkg"); err != nil {
return err
}
return nil
}
// Runs go mod download and then installs the binary.
func Build() error {
mg.Deps(Clean)
mg.Deps(buildBinary)
return nil
}
func RunLocal() error {
mg.Deps(Build)
env := map[string]string{
"DEBUG": "1",
}
return sh.RunWith(env, filepath.Join(distDir, "grafana-ntfy"), "-ntfy-url", "https://ntfy.sh/academo")
}
func Test() error {
return sh.RunV("go", "test", "./...")
}
func Deploy() error {
mg.Deps(Build)
var err error
// fail if git is not clean
if err = sh.Run("git", "diff-index", "--quiet", "HEAD", "--"); err != nil {
return fmt.Errorf("git is not clean: %w", err)
}
version, err := getNextVersion()
if err != nil {
return err
}
// create the tag
if err = sh.Run("git", "tag", "-a", version, "-m", "Release "+version); err != nil {
return err
}
// push the tag
if err = sh.Run("git", "push", "origin", version); err != nil {
return err
}
fmt.Println("Tag pushed to origin")
// create the release
err = sh.Run("gh", "release", "create", version, "--latest", "-t", "Release "+version, "--verify-tag", "--generate-notes")
if err != nil {
return err
}
// build docker image
// Replace with these commands for multi-platform build:
err = sh.Run("docker", "buildx", "create", "--use") // Create and use buildx builder
if err != nil {
return err
}
err = sh.Run("docker", "buildx", "build",
"--network=host", // docker dns keep messing things up
"--platform", "linux/amd64,linux/arm64", // Build for both AMD64 and ARM64
"-t", "academo/grafana-ntfy:"+version,
"-t", "academo/grafana-ntfy:latest",
"--push", // Push all tags at once
".")
if err != nil {
return err
}
return nil
}
func getNextVersion() (string, error) {
//ask if patch, minor or major default to minor
releaseTypes := []string{"patch", "minor", "major"}
typeRelease := &survey.Select{
Message: "What kind of release is this?",
Options: releaseTypes,
Default: releaseTypes[1],
}
var releaseType string
err := survey.AskOne(typeRelease, &releaseType)
if err != nil {
return "", err
}
// get the current version
version, err := sh.Output("git", "describe", "--tags", "--abbrev=0")
if err != nil {
return "", fmt.Errorf("failed to get current version: %w", err)
}
// create the new version
currentVersion, err := semver.NewVersion(version)
var nextVersion semver.Version
if err != nil {
return "", fmt.Errorf("failed to parse current version: %w", err)
}
switch releaseType {
case "patch":
nextVersion = currentVersion.IncPatch()
case "minor":
nextVersion = currentVersion.IncMinor()
case "major":
nextVersion = currentVersion.IncMajor()
}
return nextVersion.String(), nil
}
func getOrDefault(envVar, defaultValue string) string {
value := os.Getenv(envVar)
if value == "" {
return defaultValue
}
return value
}