forked from cncf/contribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
108 lines (88 loc) · 2.29 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
// +build mage
// This is a magefile, and is a "makefile for go".
// See https://magefile.org/
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/shx"
)
var (
prodBranch = "main"
)
const (
// Triggers a build of github.com/cncf/tag-contributor-strategy on the main branch
webhook = "https://api.netlify.com/build_hooks/60d5e1de44093546fdf14b3e"
)
// Ensure Mage is installed and on the PATH.
func EnsureMage() error {
return pkg.EnsureMage("")
}
// Deploy triggers a Netlify build for the website.
func Deploy() error {
// Put up a page on the preview that redirects to the live site
os.MkdirAll("public", 0755)
err := copy("build/deploy-redirect.html", "public/index.html")
if err != nil {
return err
}
return triggerNetlifyDeployment()
}
func copy(src string, dest string) error {
srcF, err := os.Open(src)
if err != nil {
return err
}
destF, err := os.Create(dest)
if err != nil {
return err
}
_, err = io.Copy(destF, srcF)
return err
}
func triggerNetlifyDeployment() error {
emptyMsg := "{}"
data := strings.NewReader(emptyMsg)
fmt.Println("POST", webhook)
fmt.Println(emptyMsg)
r, err := http.Post(webhook, "application/json", data)
if err != nil {
return err
}
if r.StatusCode >= 300 {
defer r.Body.Close()
msg, _ := ioutil.ReadAll(r.Body)
return fmt.Errorf("request failed (%d) %s: %s", r.StatusCode, r.Status, msg)
}
return nil
}
// DeployPreview builds the entire website for a preview.
func DeployPreview() error {
pwd, _ := os.Getwd()
websiteDir := filepath.Join(filepath.Dir(pwd), "tag-contributor-strategy")
if _, err := os.Stat(websiteDir); os.IsNotExist(err) {
err := shx.RunV("git", "clone", "-b", prodBranch, "--recurse-submodules", "https://github.com/cncf/tag-contributor-strategy.git", websiteDir)
if err != nil {
return err
}
}
// Build the website with our using our local changes
err := shx.Command("go", "mod", "edit", "-replace", "github.com/cncf/contribute="+pwd).
In(websiteDir).RunV()
if err != nil {
return err
}
err = shx.Command("go", "run", "mage.go", "DeployPreview").
In(websiteDir).RunV()
if err != nil {
return err
}
return shx.Command("bash", "-c", "cp -R website/public "+pwd).
In(websiteDir).RunV()
}