forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (116 loc) · 3.46 KB
/
main.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
package main
import (
"context"
_ "embed"
"errors"
"fmt"
"html/template"
"net/http"
"os"
"os/exec"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/Yamashou/gqlgenc/clientv2"
"github.com/bilus/scenarigo/scripts/cross-build/gen"
)
var (
token = os.Getenv("GITHUB_TOKEN")
ver = os.Getenv("GO_VERSION")
rootDir = os.Getenv("PJ_ROOT")
errImageNotFound = errors.New("image not found")
go1_19_1 = semver.MustParse("1.19.1")
)
func main() {
if err := release(); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func release() error {
tag, err := imageTag(ver, token)
if err != nil {
if errors.Is(err, errImageNotFound) {
fmt.Printf("golang-cross:v%s: %s\n", ver, err)
return nil
}
return fmt.Errorf("failed to get image tag: %w", err)
}
v, err := semver.NewVersion(ver)
if err != nil {
return fmt.Errorf("failed to parse version: %w", err)
}
cc := "aarch64-apple-darwin20.2-clang"
if v.GreaterThan(go1_19_1) || ver == "1.18.7" {
cc = "aarch64-apple-darwin21.4-clang"
}
if err := build(ver, tag, cc); err != nil {
return fmt.Errorf("failed to build: %w", err)
}
return nil
}
func imageTag(ver, token string) (string, error) {
github := &gen.Client{
Client: clientv2.NewClient(http.DefaultClient, "https://api.github.com/graphql", func(ctx context.Context, req *http.Request, gqlInfo *clientv2.GQLRequestInfo, res interface{}, next clientv2.RequestInterceptorFunc) error {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return next(ctx, req, gqlInfo, res)
}),
}
ctx := context.Background()
first := 100
getTags, err := github.GetTags(ctx, "gythialy", "golang-cross", &first)
if err != nil {
if handledError, ok := err.(*clientv2.ErrorResponse); ok {
return "", fmt.Errorf("handled error: %sn", handledError.Error())
}
return "", fmt.Errorf("unhandled error: %s", err.Error())
}
v := fmt.Sprintf("v%s", ver)
prefix := fmt.Sprintf("%s-", v)
for _, node := range getTags.Repository.Refs.Nodes {
if node.Name == v || strings.HasPrefix(node.Name, prefix) {
return node.Name, nil
}
}
return "", errImageNotFound
}
//go:embed templates/goreleaser.yml.tmpl
var tmplBytes []byte
func build(ver, tag, cc string) error {
if err := os.Mkdir(fmt.Sprintf("%s/assets", rootDir), 0o755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
tmpl := template.New("config").Delims("<<", ">>")
tmpl, err := tmpl.Parse(string(tmplBytes))
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
f, err := os.OpenFile(fmt.Sprintf("%s/.goreleaser.yml", rootDir), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
return fmt.Errorf("failed to open .goreleaser.yml: %w", err)
}
defer f.Close()
if err := tmpl.Execute(f, map[string]interface{}{
"GoVersion": ver,
"CC": cc,
}); err != nil {
return fmt.Errorf("failed to create .goreleaser.yml: %w", err)
}
if err := goreleaser(ver, tag); err != nil {
return fmt.Errorf("goreleaser failed (go%s, golang-cross:%s): %w", ver, tag, err)
}
return nil
}
func goreleaser(ver, tag string) error {
out, err := exec.Command(
"docker", "run", "--rm", "--privileged",
"-v", fmt.Sprintf("%s:/scenarigo", rootDir),
"-v", "/var/run/docker.sock:/var/run/docker.sock",
"-w", "/scenarigo",
fmt.Sprintf("ghcr.io/gythialy/golang-cross:%s", tag),
"--skip-publish", "--rm-dist",
).CombinedOutput()
if err != nil {
return fmt.Errorf("%s:\n%s", err, out)
}
return nil
}