-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
83 lines (72 loc) · 2.27 KB
/
deploy.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
// Deploy script for github pages
// Loosely based on Docusaurus Github Pages deploy script
// https://github.com/facebook/docusaurus/blob/542228ee1beb5cfddd7ba8ae088f109f164e80c5/packages/docusaurus/src/commands/deploy.ts#L43
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/bitfield/script"
"github.com/plus3it/gorecurcopy"
)
const (
projectName = "pincerhex"
deploymentBranch = "gh-pages"
)
func execCmd(cmd string) error {
log.Println(cmd)
p := script.Exec(cmd)
p.Stdout()
return p.Error()
}
func getCmdOutput(cmd string) string {
out, err := script.Exec(cmd).String()
if err != nil {
log.Fatalf("failed command `%s`: %v", cmd, err)
}
return strings.TrimSpace(out)
}
func mkTmpDir() (string, func()) {
dir, err := os.MkdirTemp(os.TempDir(), projectName+"-"+deploymentBranch)
if err != nil {
log.Fatalf("failed to make temp dir: %v", err)
}
return dir, func() {
if err := os.RemoveAll(dir); err != nil {
log.Printf("failed to remove dir: %v\n", err)
}
}
}
func main() {
originUrl := getCmdOutput("git config --get remote.origin.url")
latestHash := getCmdOutput("git rev-parse HEAD")
output, cleanOutput := mkTmpDir()
defer cleanOutput()
gitPublish, cleanGitPublish := mkTmpDir()
defer cleanGitPublish()
if err := gorecurcopy.CopyDirectory("./pincerhex_gui/dist", output); err != nil {
log.Fatalf("failed to copy build to output dir: %v", err)
}
os.Chdir(gitPublish)
cloneCmd := fmt.Sprintf("git clone --depth 1 --branch %s %s %s", deploymentBranch, originUrl, gitPublish)
if err := execCmd(cloneCmd); err != nil {
// Branch doesn't exist, create new branch
execCmd("git init")
execCmd("git checkout -b " + deploymentBranch)
execCmd("git remote add origin " + originUrl)
} else {
execCmd("git rm -rf .") // Simply remove all files
}
if err := gorecurcopy.CopyDirectory(output, gitPublish); err != nil {
log.Fatalf("failed to copy output to publish dir: %v", err)
}
os.Chdir(gitPublish)
execCmd("git add --all")
commitErr := execCmd(fmt.Sprintf("git commit -m \"Deploy website - based on %s\"", latestHash))
if err := execCmd("git push --force origin " + deploymentBranch); err != nil {
log.Fatalf("failed to push to origin: %v", err)
} else if commitErr == nil {
fmt.Printf("Website is live at: https://%s\n", projectName)
}
}