Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
Urko. | 99236b5fb5 | |
Urko. | d22a6816c4 | |
Urko. | f6e4e64e81 | |
Urko. | eaf0b1ff69 | |
Urko. | 9c735ccf64 |
|
@ -12,9 +12,9 @@ type Config struct {
|
||||||
Projects map[string][]ConfigScript `yaml:"projects"`
|
Projects map[string][]ConfigScript `yaml:"projects"`
|
||||||
}
|
}
|
||||||
type ConfigScript struct {
|
type ConfigScript struct {
|
||||||
Environment string `yaml:"environment"`
|
Environment string `yaml:"environment"`
|
||||||
BinaryPath string `yaml:"binary"`
|
Command string `yaml:"command"`
|
||||||
ScriptPath string `yaml:"script"`
|
Arguments []string `yaml:"args"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(path string) (*Config, error) {
|
func LoadConfig(path string) (*Config, error) {
|
||||||
|
|
46
main.go
46
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -8,15 +9,21 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
|
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
|
||||||
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
|
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var err error
|
||||||
|
ctx, cancel := context.WithCancel(signalContext(context.Background()))
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
cfgFile := os.Getenv("CONFIG_FILE")
|
cfgFile := os.Getenv("CONFIG_FILE")
|
||||||
if cfgFile == "" {
|
if cfgFile == "" {
|
||||||
// Get root path
|
// Get root path
|
||||||
|
@ -28,7 +35,17 @@ func main() {
|
||||||
log.Fatalf("Error loading config: %v", err)
|
log.Fatalf("Error loading config: %v", err)
|
||||||
}
|
}
|
||||||
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
|
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
|
||||||
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
|
||||||
|
go func() {
|
||||||
|
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
||||||
|
}()
|
||||||
|
log.Printf("server is up on %d\n", cfg.Port)
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("some error happened", err)
|
||||||
|
}
|
||||||
|
log.Println("server shutdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePayload(secret string, projects 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) {
|
||||||
|
@ -56,6 +73,7 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
|
||||||
proj, found := projects[project]
|
proj, found := projects[project]
|
||||||
if !found {
|
if !found {
|
||||||
http.Error(w, "not found", http.StatusNotFound)
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
log.Printf("project %s not found\n", project)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var payload internal.WebhookPayload
|
var payload internal.WebhookPayload
|
||||||
|
@ -64,6 +82,7 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
|
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
|
||||||
|
log.Println("branchName", branchName)
|
||||||
found = false
|
found = false
|
||||||
var scr config.ConfigScript
|
var scr config.ConfigScript
|
||||||
for i := range proj {
|
for i := range proj {
|
||||||
|
@ -76,25 +95,44 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
http.Error(w, "not found", http.StatusNotFound)
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
log.Printf("project %s with branch %s not found\n", project, branchName)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
|
if err := execute(scr.Command, scr.Arguments...); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
log.Println("script was deployed successful")
|
||||||
}()
|
}()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func execute(binaryPath, scriptPath string) error {
|
func execute(command string, args ...string) error {
|
||||||
cmd := exec.Command(binaryPath, scriptPath)
|
cmd := exec.Command(command, args...)
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return fmt.Errorf("cmd.Run %w", err)
|
return fmt.Errorf("cmd.Run %w", err)
|
||||||
}
|
}
|
||||||
|
log.Println()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func signalContext(ctx context.Context) context.Context {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-sigs
|
||||||
|
signal.Stop(sigs)
|
||||||
|
close(sigs)
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue