-
Notifications
You must be signed in to change notification settings - Fork 59
/
build_release.go
102 lines (87 loc) · 2.34 KB
/
build_release.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
//go:build ignore
// +build ignore
// This is a tool to assist with building release artifacts.
// It is run automatically from github actions to produce the
// artifacts.
//
// Instructions for cutting a new release:
// - Update version/version.go
// - Make new git tag (e.g. v1.0.x)
// - Push tag to github
// - Github release.yml action will `go run build_release.go` at that tag
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/psanford/wormhole-william/version"
)
var ignoreTagMismatch = flag.Bool("ignore-tag-mismatch", false, "Don't check if current tag matches in code version")
func main() {
flag.Parse()
if !*ignoreTagMismatch {
checkTagMatchesVersion()
}
os.MkdirAll("release", 0777)
for _, t := range targets {
cmd := exec.Command("go", "build", "-trimpath", "-o", filepath.Join("release", t.binaryName()))
env := []string{"GOOS=" + t.goos, "GOARCH=" + t.garch, "GO111MODULE=on"}
if t.goarm != "" {
env = append(env, "GOARM="+t.goarm)
}
cmd.Env = append(os.Environ(), env...)
fmt.Printf("run: %s %s %s\n", strings.Join(env, " "), cmd.Path, strings.Join(cmd.Args[1:], " "))
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s err: %s, out: %s\n", t.goos, t.garch, err, out)
os.Exit(1)
}
}
}
func checkTagMatchesVersion() {
codeVersion := version.AgentVersion
headSha := gitCmd("rev-parse", "HEAD")
tagSha := gitCmd("rev-parse", codeVersion)
if headSha != tagSha {
log.Fatalf("Tag for %s does not match HEAD ref: HEAD:%s TAG:%s", codeVersion, headSha, tagSha)
}
}
func gitCmd(args ...string) string {
out, err := exec.Command("git", args...).Output()
if err != nil {
log.Fatalf("git %s failed: %s", args, err)
}
return string(bytes.TrimSpace(out))
}
type target struct {
goos string
garch string
goarm string
}
func (t *target) binaryName() string {
ext := ""
if t.goos == "windows" {
ext = ".exe"
}
tmpl := "wormhole-william-%s-%s%s%s"
return fmt.Sprintf(tmpl, t.goos, t.garch, t.goarm, ext)
}
var targets = []target{
{"linux", "amd64", ""},
{"linux", "arm64", ""},
{"linux", "arm", "5"},
{"linux", "arm", "6"},
{"linux", "arm", "7"},
{"darwin", "amd64", ""},
{"darwin", "arm64", ""},
{"windows", "amd64", ""},
{"windows", "arm64", ""},
{"windows", "386", ""},
{"freebsd", "amd64", ""},
{"netbsd", "amd64", ""},
}