Compare commits
8 Commits
c54366a21e
...
a4ce793707
Author | SHA1 | Date |
---|---|---|
Urko | a4ce793707 | |
Urko | 8d533a5ca6 | |
Urko | 28c7a03d9c | |
Urko | 103016bc39 | |
Urko | d50a9afde8 | |
Urko | 49126da9d9 | |
Urko | 7513abffbb | |
Urko | 70a777db3a |
|
@ -0,0 +1,4 @@
|
|||
SCRIPT_BINARY_PATH=
|
||||
WEBHOOK_SCRIPT_PATH=
|
||||
FILE_TO_WATCH_PATH=
|
||||
TEST_FILE_TO_WATCH_PATH=
|
|
@ -1 +1,3 @@
|
|||
.env
|
||||
coverage/*
|
||||
test_monitor.txt
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
COVERAGE_DIR=coverage
|
||||
|
||||
lint:
|
||||
golangci-lint run ./...
|
||||
goreportcard:
|
||||
goreportcard-cli -v
|
||||
test:
|
||||
go test ./...
|
||||
test-coverage:
|
||||
rm -rf ${COVERAGE_DIR}
|
||||
mkdir ${COVERAGE_DIR}
|
||||
go test -v -coverprofile ${COVERAGE_DIR}/cover.out ./...
|
||||
go tool cover -html ${COVERAGE_DIR}/cover.out -o ${COVERAGE_DIR}/cover.html
|
||||
cd ${COVERAGE_DIR}
|
||||
open cover.html
|
|
@ -0,0 +1,18 @@
|
|||
# git-webhook-ci
|
||||
Tool to automatize your deploy based on file write changes
|
||||
|
||||
## Context
|
||||
As a security risk that could be allow a webhook listener on a VPS for your git repository, I've decided
|
||||
to create this package which is a listener to file changes on write. This will trigger a bash script which
|
||||
you shold place on your server to run desired tasks. In my case I've done this to run a bash deploy command like
|
||||
- git pull
|
||||
- build
|
||||
- move build to desired dir
|
||||
- restart services like nginx or whatever
|
||||
|
||||
## Installation
|
||||
### Requirements
|
||||
- [Go version > 1.19](https://go.dev/dl/)
|
||||
- [GNU Make 4.3](https://www.gnu.org/software/make/)
|
||||
- [goreportcard-cli](https://github.com/gojp/goreportcard)
|
||||
- [golangci-lint](https://golangci-lint.run/)
|
|
@ -13,6 +13,7 @@ type Config struct {
|
|||
ScriptBinaryPath string `required:"true" split_words:"true"`
|
||||
WebhookScriptPath string `required:"true" split_words:"true"`
|
||||
FileToWatchPath string `required:"true" split_words:"true"`
|
||||
TestFileToWatchPath string `required:"true" split_words:"true"`
|
||||
}
|
||||
|
||||
func rootDir() string {
|
||||
|
|
8
go.mod
8
go.mod
|
@ -6,6 +6,12 @@ require (
|
|||
github.com/fsnotify/fsnotify v1.6.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
github.com/stretchr/testify v1.8.2
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
17
go.sum
17
go.sum
|
@ -1,8 +1,25 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
|
||||
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
|
||||
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -13,6 +13,11 @@ type watcher struct {
|
|||
deploy pkgwatcher.DeployFunc
|
||||
}
|
||||
|
||||
var (
|
||||
errEventsClosedChan = errors.New("events is closed")
|
||||
errErrorsClosedChan = errors.New("errors is closed")
|
||||
)
|
||||
|
||||
func NewWatcher(deploy pkgwatcher.DeployFunc) *watcher {
|
||||
wt, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
|
@ -36,8 +41,8 @@ func (w *watcher) Listen(binaryPath, scriptPath string, outputErr chan<- error)
|
|||
select {
|
||||
case event, ok := <-events:
|
||||
if !ok {
|
||||
log.Printf("!ok <-events \n")
|
||||
outputErr <- errors.New("!ok <-events")
|
||||
log.Println(errEventsClosedChan)
|
||||
outputErr <- errEventsClosedChan
|
||||
return
|
||||
}
|
||||
if !event.Has(fsnotify.Write) {
|
||||
|
@ -47,14 +52,17 @@ func (w *watcher) Listen(binaryPath, scriptPath string, outputErr chan<- error)
|
|||
|
||||
if err := w.deploy(binaryPath, scriptPath); err != nil {
|
||||
log.Printf("deploy: %s\n", err)
|
||||
outputErr <- err
|
||||
continue
|
||||
}
|
||||
case err, ok := <-errChan:
|
||||
if !ok {
|
||||
log.Printf("!ok <-errors\n")
|
||||
log.Println(errErrorsClosedChan)
|
||||
outputErr <- errErrorsClosedChan
|
||||
return
|
||||
}
|
||||
log.Printf("<-errors: %s\n", err)
|
||||
outputErr <- err
|
||||
}
|
||||
}
|
||||
}(w.fswatcher.Events, w.fswatcher.Errors, outputErr)
|
||||
|
|
|
@ -0,0 +1,136 @@
|
|||
package watcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.urkob.com/urko/git-webhook-ci/cfg"
|
||||
pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
mockDeploy pkgwatcher.DeployFunc
|
||||
mockErrorDeploy pkgwatcher.DeployFunc
|
||||
errIntentional = errors.New("intentional error")
|
||||
binaryPath = ""
|
||||
scriptPath = ""
|
||||
executionMaxTimeout = time.Second * 2
|
||||
config *cfg.Config
|
||||
events = []fsnotify.Event{
|
||||
{
|
||||
Name: "test event",
|
||||
Op: fsnotify.Create,
|
||||
},
|
||||
{
|
||||
Name: "Write",
|
||||
Op: fsnotify.Write,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
mockDeploy = func(binaryPath, scriptPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
mockErrorDeploy = func(binaryPath, scriptPath string) error {
|
||||
return errIntentional
|
||||
}
|
||||
|
||||
config = cfg.NewConfig(false)
|
||||
}
|
||||
|
||||
func sendTestEvents(w *watcher) {
|
||||
for _, event := range events {
|
||||
w.fswatcher.Events <- event
|
||||
}
|
||||
}
|
||||
|
||||
func getNewWatcher() *watcher {
|
||||
return NewWatcher(mockDeploy)
|
||||
}
|
||||
|
||||
func getNewWatcherWithError() *watcher {
|
||||
return NewWatcher(mockErrorDeploy)
|
||||
}
|
||||
|
||||
func Test_NewWatcher(t *testing.T) {
|
||||
w := getNewWatcher()
|
||||
require.NotNil(t, w)
|
||||
}
|
||||
|
||||
func Test_Close(t *testing.T) {
|
||||
w := getNewWatcher()
|
||||
err := w.Close()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_Monitor(t *testing.T) {
|
||||
w := getNewWatcher()
|
||||
err := w.Monitor(config.TestFileToWatchPath)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_ListenSuccess(t *testing.T) {
|
||||
w := getNewWatcher()
|
||||
ctx, errors := listenWithSendEvents(w)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case err := <-errors:
|
||||
require.NoError(t, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ListenError(t *testing.T) {
|
||||
w := getNewWatcherWithError()
|
||||
ctx, errors := listenWithSendEvents(w)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case err := <-errors:
|
||||
require.Error(t, err)
|
||||
require.EqualError(t, err, errIntentional.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ListenErrorChanClose(t *testing.T) {
|
||||
w := getNewWatcher()
|
||||
ctx, errors := listenWithSendEvents(w)
|
||||
close(w.fswatcher.Events)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case err := <-errors:
|
||||
require.Error(t, err)
|
||||
require.EqualError(t, err, errEventsClosedChan.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func listenWithSendEvents(w *watcher) (context.Context, chan error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), executionMaxTimeout)
|
||||
errors := make(chan error)
|
||||
w.Listen(binaryPath, scriptPath, errors)
|
||||
go func(cancel context.CancelFunc) {
|
||||
time.Sleep(executionMaxTimeout)
|
||||
cancel()
|
||||
}(cancel)
|
||||
|
||||
sendTestEvents(w)
|
||||
return ctx, errors
|
||||
}
|
72
main.go
72
main.go
|
@ -33,17 +33,14 @@ func main() {
|
|||
watcherIface.Listen(config.ScriptBinaryPath, config.WebhookScriptPath, errors)
|
||||
|
||||
// Handle termination on ctrl+signalChan
|
||||
signalChan := make(chan os.Signal)
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-signalChan
|
||||
os.Exit(1)
|
||||
}()
|
||||
|
||||
go func(errors chan error) {
|
||||
for {
|
||||
select {
|
||||
case <-signalChan:
|
||||
os.Exit(1)
|
||||
case err := <-errors:
|
||||
if err != nil {
|
||||
log.Fatalf("watcherIface.Monitor: %s\n", err)
|
||||
|
@ -53,67 +50,6 @@ func main() {
|
|||
}
|
||||
}(errors)
|
||||
|
||||
// Block main goroutine forever.
|
||||
// TODO: Improve this: Block main goroutine forever.
|
||||
<-make(chan struct{})
|
||||
|
||||
// watcher, err := fsnotify.NewWatcher()
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// defer watcher.Close()
|
||||
|
||||
// // Start listening for events.
|
||||
// go func() {
|
||||
// for {
|
||||
// select {
|
||||
// case event, ok := <-watcher.Events:
|
||||
// if !ok {
|
||||
// log.Printf("<-watcher.Events: %s\n", err)
|
||||
// return
|
||||
// }
|
||||
// if !event.Has(fsnotify.Write) {
|
||||
// log.Printf("is not Write: %s\n", event.Name)
|
||||
// continue
|
||||
// }
|
||||
|
||||
// if err := deploy(config.ScriptBinaryPath, config.WebhookScriptPath); err != nil {
|
||||
// log.Printf("deploy: %s\n", err)
|
||||
// continue
|
||||
// }
|
||||
// case err, ok := <-watcher.Errors:
|
||||
// if !ok {
|
||||
// return
|
||||
// }
|
||||
// log.Printf("<-watcher.Errors: %s\n", err)
|
||||
// }
|
||||
// }
|
||||
// }()
|
||||
|
||||
// if err = watcher.Add(config.FileToWatchPath); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
|
||||
// // Block main goroutine forever.
|
||||
// <-make(chan struct{})
|
||||
}
|
||||
|
||||
// func deploy(binaryPath, scriptPath string) error {
|
||||
// err := execute(binaryPath, scriptPath)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("execute: %s", err)
|
||||
// }
|
||||
// log.Println("deploy done")
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// 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 %s", err)
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// }
|
||||
|
|
Loading…
Reference in New Issue