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"`
|
||||
}
|
||||
type ConfigScript struct {
|
||||
Environment string `yaml:"environment"`
|
||||
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) {
|
||||
|
|
46
main.go
46
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -8,15 +9,21 @@ 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
|
||||
|
@ -28,7 +35,17 @@ func main() {
|
|||
log.Fatalf("Error loading config: %v", err)
|
||||
}
|
||||
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) {
|
||||
|
@ -56,6 +73,7 @@ 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
|
||||
|
@ -64,6 +82,7 @@ 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 {
|
||||
|
@ -76,25 +95,44 @@ 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.BinaryPath, scr.ScriptPath); err != nil {
|
||||
if err := execute(scr.Command, scr.Arguments...); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
log.Println("script was deployed successful")
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue