forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (137 loc) · 3.65 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
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
// The .github/workflows/docker.yml uses it as an github action
// and run it like this:
//
// DOCKER_TOKEN=$TOKEN go run ./lib/utils/docker $GITHUB_REF
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/apedem/rod/lib/utils"
)
func main() {
event := os.Args[1]
fmt.Println("Event:", event)
master := regexp.MustCompile(`^refs/heads/master$`).MatchString(event)
m := regexp.MustCompile(`^refs/tags/(v[0-9]+\.[0-9]+\.[0-9]+)$`).FindStringSubmatch(event)
ver := ""
if len(m) > 1 {
ver = m[1]
}
at := getArchType()
if master {
releaseLatest(at)
} else if ver != "" {
releaseWithVer(ver, at)
} else {
test(at)
}
}
func releaseLatest(at archType) {
login()
test(at)
utils.Exec("docker push", at.tagDev())
utils.Exec("docker push", at.tag())
}
func releaseWithVer(ver string, at archType) {
login()
verImageDev := registry + ":" + ver + "-dev"
utils.Exec("docker manifest create", verImageDev, archAmd.tagDev(), archArm.tagDev())
utils.Exec("docker manifest push", verImageDev)
verImage := registry + ":" + ver
utils.Exec("docker manifest create", verImage, archAmd.tag(), archArm.tag())
utils.Exec("docker manifest push", verImage)
registryDev := registry + ":dev"
utils.Exec("docker manifest create", registryDev, archAmd.tagDev(), archArm.tagDev())
utils.Exec("docker manifest push", registryDev)
utils.Exec("docker manifest create", registry, archAmd.tag(), archArm.tag())
utils.Exec("docker manifest push", registry)
}
func test(at archType) {
utils.Exec("docker build -f=lib/docker/Dockerfile", "--platform", at.platform(), "-t", at.tag(), description(false), ".")
utils.Exec("docker build -f=lib/docker/dev.Dockerfile",
"--platform", at.platform(),
"--build-arg", "golang="+at.golang(),
"--build-arg", "nodejs="+at.nodejs(),
"-t", at.tagDev(),
description(true), ".",
)
utils.Exec("docker run", at.tag(), "rod-manager", "-h")
// TODO: arm cross execution for chromium doesn't work well on github actions.
if at != archArm {
wd, err := os.Getwd()
utils.E(err)
utils.Exec("docker run -w=/t -v", fmt.Sprintf("%s:/t", wd), at.tagDev(), "go", "run", "./lib/utils/ci-test")
}
}
func login() {
cmd := exec.Command("docker", "login", "-u=rod-robot", "-p", os.Getenv("DOCKER_TOKEN"), registry)
out, err := cmd.CombinedOutput()
utils.E(err)
utils.E(os.Stdout.Write(out))
}
var headSha = strings.TrimSpace(utils.ExecLine(false, "git", "rev-parse", "HEAD"))
func description(dev bool) string {
f := "Dockerfile"
if dev {
f = "dev." + f
}
return `--label=org.opencontainers.image.description=https://github.com/apedem/rod/blob/` + headSha + "/lib/docker/" + f
}
const registry = "ghcr.io/go-rod/rod"
type archType int
const (
archAmd archType = iota
archArm
)
func getArchType() archType {
arch := os.Getenv("ARCH")
switch arch {
case "arm":
return archArm
default:
return archAmd
}
}
func (at archType) platform() string {
switch at {
case archArm:
return "linux/arm64"
default:
return "linux/amd64"
}
}
func (at archType) tag() string {
switch at {
case archArm:
return registry + ":arm"
default:
return registry + ":amd"
}
}
func (at archType) tagDev() string {
switch at {
case archArm:
return registry + ":arm-dev"
default:
return registry + ":amd-dev"
}
}
func (at archType) golang() string {
switch at {
case archArm:
return "https://go.dev/dl/go1.19.1.linux-arm64.tar.gz"
default:
return "https://go.dev/dl/go1.19.1.linux-amd64.tar.gz"
}
}
func (at archType) nodejs() string {
switch at {
case archArm:
return "https://nodejs.org/dist/v16.17.0/node-v16.17.0-linux-arm64.tar.xz"
default:
return "https://nodejs.org/dist/v16.17.0/node-v16.17.0-linux-x64.tar.xz"
}
}