GitHub webhook

Time:2021-2-25

Recently, I wrote a small tool to receive GitHub webhook messages to automatically execute scripts on the server

gaopengfei123123/hook

github hook server

A server used to receive webhook

Environmental Science

Golang v1.11 + (because go mod is used)

install

Implementation:

go get -u github.com/gaopengfei123123/hook

create a filemain.go

package main

import (
    "github.com/gaopengfei123123/hook"
)

func main() {
    hook.Execute()
}

Execute the order

go build -o app main.go

appThis binary file is the ontology. Put it in a suitable place and execute the following steps:

./app start -d

Start in the background, and the app will be created in the same directoryscripts, logsTwo directories, and onehook.pidSave PID

Check whether to start or not:

[[email protected]]# curl localhost:8080/ping
{"message":"pong v5"}

Indicates that the service has been started successfully. The service address is0.0.0.0:8080

Interface

  • 0.0.0.0:8080/pingTest interface
  • 0.0.0.0:8080/pushAccept GitHub webhook request according to Repository.Name To determine what scripts to execute

Available instructions

Reload reloads the log and restarts smoothly
Start command, - D background running
Stop terminate command
Version program version

Directory function:

  • Logs stores the request log
  • Scripts store hook scripts. When a request for the corresponding library name comes in, the configured scripts will be executed
  • hook.pid Storing process PID

configuration file

{
    "github_ Hook ": {// to request the repository.name  Field to do the key
        "Secret": "XXXXXX", // if the secret is set, it will be used for verification. If it is empty, it will not be verified
        "script_ Path ':', // the absolute directory of the script. If it is empty, it is the current script directory
        "event": {
            "Push": "test" // script for pushing event execution
        }
    }
}

characteristic

  • One click initialization
  • Smooth restart, signal communication
  • Support secret verification
  • Execute script asynchronously
  • Avoid replay
  • Script passing parameters (todo)
  • Event hook (todo)

Dependent component

  • HTTP framework
  • Log component logrus
  • Cobra, a command line component

GitHub address

gaopengfei123123/hook

Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]