Compare commits
4 Commits
v0.4
...
f6e4e64e81
| Author | SHA1 | Date | |
|---|---|---|---|
| f6e4e64e81 | |||
| eaf0b1ff69 | |||
| 9c735ccf64 | |||
| adf47e4810 |
@@ -7,13 +7,14 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Secret string `yaml:"secret"`
|
||||
Port int `yaml:"port"`
|
||||
Projects map[string]map[string]ConfigScript `yaml:"projects"`
|
||||
Secret string `yaml:"secret"`
|
||||
Port int `yaml:"port"`
|
||||
Projects map[string][]ConfigScript `yaml:"projects"`
|
||||
}
|
||||
type ConfigScript struct {
|
||||
BinaryPath string `yaml:"binary"`
|
||||
ScriptPath string `yaml:"script"`
|
||||
Environment string `yaml:"environment"`
|
||||
Command string `yaml:"command"`
|
||||
Arguments []string `yaml:"args"`
|
||||
}
|
||||
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
|
||||
21
main.go
21
main.go
@@ -28,10 +28,11 @@ func main() {
|
||||
log.Fatalf("Error loading config: %v", err)
|
||||
}
|
||||
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
|
||||
log.Printf("server is up on %d\n", cfg.Port)
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
||||
}
|
||||
|
||||
func handlePayload(secret string, projects map[string]map[string]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
|
||||
func handlePayload(secret string, projects map[string][]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
|
||||
return (func(w http.ResponseWriter, r *http.Request) {
|
||||
// Read the request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
@@ -56,6 +57,7 @@ func handlePayload(secret string, projects map[string]map[string]config.ConfigSc
|
||||
proj, found := projects[project]
|
||||
if !found {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
log.Printf("project %s not found\n", project)
|
||||
return
|
||||
}
|
||||
var payload internal.WebhookPayload
|
||||
@@ -64,23 +66,32 @@ func handlePayload(secret string, projects map[string]map[string]config.ConfigSc
|
||||
return
|
||||
}
|
||||
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
|
||||
found = false
|
||||
var scr config.ConfigScript
|
||||
for i := range proj {
|
||||
if proj[i].Environment == branchName {
|
||||
found = true
|
||||
scr = proj[i]
|
||||
break
|
||||
}
|
||||
|
||||
scr, found := proj[branchName]
|
||||
}
|
||||
if !found {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
log.Printf("project %s with branch %s not found\n", project, branchName)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
|
||||
if err := execute(scr.Command, scr.Arguments...); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func execute(binaryPath, scriptPath string) error {
|
||||
cmd := exec.Command(binaryPath, scriptPath)
|
||||
func execute(command string, args ...string) error {
|
||||
cmd := exec.Command(command, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
|
||||
Reference in New Issue
Block a user