func main() { app := cli.NewApp() app.Name = "hookserve" app.Usage = "A small little application that listens for commit / push webhook events from github and runs a specified command\n\n" app.Usage += "EXAMPLE:\n" app.Usage += " hookserve --secret=whiskey --port=8888 echo #Echo back the information provided\n" app.Usage += " hookserve logger -t PushEvent #log the push event to the system log (/var/log/message)" app.Version = "1.0" app.Author = "Patrick Hayes" app.Email = "*****@*****.**" app.Flags = []cli.Flag{ cli.IntFlag{ Name: "port, p", Value: 80, Usage: "port on which to listen for github webhooks", }, cli.StringFlag{ Name: "secret, s", Value: "", Usage: "Secret for HMAC verification. If not provided no HMAC verification will be done and all valid requests will be processed", }, cli.BoolFlag{ Name: "tags, t", Usage: "Also execute the command when a tag is pushed", }, } app.Action = func(c *cli.Context) { server := hookserve.NewServer() server.Port = c.Int("port") server.Secret = c.String("secret") server.IgnoreTags = !c.Bool("tags") server.GoListenAndServe() for commit := range server.Events { if args := c.Args(); len(args) != 0 { root := args[0] rest := append(args[1:], commit.Owner, commit.Repo, commit.Branch, commit.Commit) cmd := exec.Command(root, rest...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Run() } else { fmt.Println(commit.Owner + " " + commit.Repo + " " + commit.Branch + " " + commit.Commit) } } } app.Run(os.Args) }
func ServeHook(port int, secret string) { server := hookserve.NewServer() server.Port = port server.Secret = secret go func() { log.Print("Starting hook even listener") for event := range server.Events { log.Print(event.Owner + " " + event.Repo + " " + event.Branch + " " + event.Commit) if event.Branch == "master" { PublishBlog() } } }() log.Printf("Starting hook server on port %d", server.Port) server.ListenAndServe() }
func main() { var err error if pushPort, err = strconv.Atoi(os.Getenv("PUSH_PORT")); err != nil { log.Fatal("Env PUSH_PORT must be integer") } pushNotifHTTPPath = os.Getenv("PUSH_HTTP_PATH") if pushNotifHTTPPath == "" { log.Fatal("Env PUSH_HTTP_PATH must be present") } pushGithubSecret = os.Getenv("PUSH_GITHUB_SECRET") projectDir = os.Getenv("PUSH_PROJECT_DIR") if projectDir == "" { log.Fatal("Env PUSH_PROJECT_DIR must be present") } server := hookserve.NewServer() server.Port = pushPort server.Path = pushNotifHTTPPath server.Secret = pushGithubSecret server.GoListenAndServe() cliExecuter := cliRunner{os.Stderr} fmt.Printf("Starting server...\nExpecting push notification on :%d%s\nProject path %s\n", pushPort, pushNotifHTTPPath, projectDir) for { select { case event := <-server.Events: err := pull(event, &cliExecuter) if err != nil { log.Fatal(err) } err = build(&cliExecuter) if err != nil { log.Fatal(err) } } } }
func main() { // Create a new map. mymap = cmap.New() log.Println(os.Getenv("GH_TOKEN")) server := hookserve.NewServer() server.Port = 8120 server.Secret = "absolutesecret" server.GoListenAndServe() fmt.Printf("Listening on %d\n", server.Port) for { select { case commit := <-server.Events: fmt.Println("Got: ", commit.Owner, commit.Repo, commit.Branch, commit.Commit) go func() { build(&commit) }() default: time.Sleep(100) //fmt.Println("No activity...") } } }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) InitConfig() InitDB() InitANSI2HTML() glog.Info("starting main thread.") // Set up HTTP paths githubreceive := hookserve.NewServer() if Config.Github.Enabled { githubreceive.Secret = Config.Github.Secret http.Handle("/postreceive", githubreceive) fmt.Println("Github webhook URL: http://" + Config.Host + ":" + strconv.Itoa(Config.Port) + "/postreceive") } http.HandleFunc("/", handleUI) // Listen and serve HTTP go func() { fmt.Println("Listening on port " + strconv.Itoa(Config.Port)) err := http.ListenAndServe(":"+strconv.Itoa(Config.Port), nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }() // Launch workers for running jobs // We have a number of workers equal to the number of cores for i := 1; i <= runtime.NumCPU(); i++ { go func() { for { event, err := PopEvent() if err != nil { log.Println(err) } else if event != nil { err = event.Report() if err != nil { // If we can't update the status on the source-control system, then just log it and continue event.Log = append(event.Log, []byte(err.Error()+"\n")...) event.Update() log.Println(err) } status, err := event.Run() err = event.Finalize(status, err) if err != nil { log.Println(err) } } // Wait 100 ms then check again time.Sleep(100 * time.Millisecond) // If we are shutting down, stop processing if InShutdown { return } } }() } // Handle a sigint to gracefully shutdown sigint := make(chan os.Signal, 1) signal.Notify(sigint, syscall.SIGINT) go func() { for _ = range sigint { log.Println("Got shutdown signal. Will shutdown when actively running jobs are finished. To shutdown immidaitely, use sigquit.") InShutdown = true // Wait until we have no running jobs then shut down. for { numEvents, err := NumEvent("running") if err != nil { log.Fatal(err) } if numEvents == 0 { log.Println("Shutting down") os.Exit(0) } else { // Wait 100 ms then check again time.Sleep(100 * time.Millisecond) } } } }() // Handle a sigquit to shut down immidiately sigquit := make(chan os.Signal, 1) signal.Notify(sigquit, syscall.SIGQUIT) go func() { for _ = range sigquit { log.Println("Got quit signal. Shutting down immidaitely. To shutdown gracefully, use sigint.") os.Exit(1) } }() // Add new events to the queue as they come in for commit := range githubreceive.Events { // Only run tets on pull-requests if there is new code to test if commit.Type == "pull_request" && (commit.Action != "synchronize" && commit.Action != "opened") { continue } event := Event{ Event: commit, Domain: "github.com", // For now we only support github Status: StatusPending, Time: time.Now(), } // First check to see if the event already exists, and if it is reque it if it's not running checkEvent, err := GetEvent(event.Domain, event.Owner, event.Repo, event.Branch, event.Commit) if err != nil { log.Println(err) } if checkEvent != nil { // It's an old event, requeue it if we can if checkEvent.Status != StatusRunning { checkEvent.Status = StatusPending err = checkEvent.Update() if err != nil { log.Println(err) } err = checkEvent.Report() if err != nil { log.Println(err) } } } else { // It's a new event, insert it anew err = event.Insert() if err != nil { log.Println(err) } err = event.Report() if err != nil { log.Println(err) } } } }