-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
125 lines (104 loc) · 3.05 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
package main
import (
"encoding/json"
"fmt"
"github.com/acarl005/stripansi"
"github.com/spf13/viper"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
)
type Payload struct {
Repository struct {
ID int `json:"id"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
} `json:"repository"`
}
var defaultPort = "8000"
var defaultPath = "/auto-deploy"
func main() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No config file found. Using default values.")
} else {
fmt.Println("Config file found. Using user defined values.")
}
port := viper.GetString("port")
if port == "" {
port = defaultPort
}
path := viper.GetString("path")
if path == "" {
path = defaultPath
}
http.HandleFunc(path, payloadHandler)
fmt.Println("Server listening on port " + port + " ...")
fmt.Println("Auto deploy path: " + path)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func payloadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
defer r.Body.Close()
var payload Payload
err = json.Unmarshal(body, &payload)
if err != nil {
http.Error(w, "Failed to parse JSON body", http.StatusBadRequest)
return
}
if payload.Repository.CloneURL == "" {
http.Error(w, "Missing CloneURL in payload", http.StatusBadRequest)
return
}
if payload.Repository.DefaultBranch == "" {
http.Error(w, "Missing DefaultBranch in payload", http.StatusBadRequest)
return
}
RepoURL := payload.Repository.CloneURL
DefaultBranch := payload.Repository.DefaultBranch
RepoID := strconv.Itoa(payload.Repository.ID)
// Execute build process in a goroutine
go func() {
buildContainer := exec.Command("./docker_builder.sh", "REPO_URL="+RepoURL, "DEFAULT_BRANCH="+DefaultBranch, "REPO_ID="+RepoID)
logFile, buildContainerError := os.Create("./logs/log-" + RepoID + ".log")
if buildContainerError != nil {
panic(buildContainerError)
}
defer logFile.Close()
buildContainer.Stdout = &cleanupWriter{writer: logFile}
buildContainer.Stderr = &cleanupWriter{writer: logFile}
buildContainerError = buildContainer.Start()
buildContainer.Wait()
if buildContainerError != nil {
fmt.Println("Deployment failed for "+RepoID+": ", buildContainerError)
} else {
fmt.Println("Deployment for " + RepoID + " successful")
}
}()
fmt.Fprint(w, "Deployment for "+RepoID+" started")
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
}
// cleanupWriter is a custom writer that strips ANSI escape codes before writing to a file
type cleanupWriter struct {
writer *os.File
}
// Write writes the cleaned output to the file
func (w *cleanupWriter) Write(p []byte) (n int, err error) {
cleanOutput := stripansi.Strip(string(p))
_, err = w.writer.WriteString(cleanOutput)
return len(p), err
}