Compare commits

..

No commits in common. "main" and "v0.5" have entirely different histories.
main ... v0.5

2 changed files with 7 additions and 45 deletions

View File

@ -12,9 +12,9 @@ type Config struct {
Projects map[string][]ConfigScript `yaml:"projects"`
}
type ConfigScript struct {
Environment string `yaml:"environment"`
Command string `yaml:"command"`
Arguments []string `yaml:"args"`
Environment string `yaml:"environment"`
BinaryPath string `yaml:"binary"`
ScriptPath string `yaml:"script"`
}
func LoadConfig(path string) (*Config, error) {

46
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
@ -9,21 +8,15 @@ import (
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"runtime"
"strings"
"syscall"
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
)
func main() {
var err error
ctx, cancel := context.WithCancel(signalContext(context.Background()))
defer cancel()
cfgFile := os.Getenv("CONFIG_FILE")
if cfgFile == "" {
// Get root path
@ -35,17 +28,7 @@ func main() {
log.Fatalf("Error loading config: %v", err)
}
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
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")
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
}
func handlePayload(secret string, projects map[string][]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
@ -73,7 +56,6 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
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
@ -82,7 +64,6 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
return
}
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
log.Println("branchName", branchName)
found = false
var scr config.ConfigScript
for i := range proj {
@ -95,44 +76,25 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
}
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.Command, scr.Arguments...); err != nil {
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
log.Println(err)
}
log.Println("script was deployed successful")
}()
})
}
func execute(command string, args ...string) error {
cmd := exec.Command(command, args...)
func execute(binaryPath, scriptPath string) error {
cmd := exec.Command(binaryPath, scriptPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("cmd.Run %w", err)
}
log.Println()
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
}