A lightweight webhook server written in Go that automatically pulls Git repositories when receiving push notifications from GitHub or GitLab. This software provides only one function - updates local copy ( using git pull ) of git repository.
- Support for both GitHub and GitLab webhooks
- Automatic
git pullon configured repositories - Configurable buffer size and timeout settings
- Secret validation for enhanced security
- Concurrent request handling with per-directory mutex locks
- Systemd service integration
- Support for both JSON and YAML configuration formats
- Go 1.20 or higher
- Git installed on the system
- Systemd (for service installation)
go installThis command compiles and installs binary into default go bin path. You should add this path to your PATH variable before run install script. Or you could build binary by command
go build -o <path_to_binary>/gitwhIt's necessary to add <path_to_binary> to your PATH environment variable.
Use the provided installation script to set up the service:
./install.shThis script will:
- Copy the service file to
/etc/systemd/system/ - Copy the configuration file to
/etc/gitwh.yaml(mode 640, it holds the secrets) - Enable and start the systemd service
The service runs git pull as the user who ran the script ($SUDO_USER under sudo). That user must own the repo folders, git refuses to pull in a repo owned by someone else. To pick another user:
SERVICE_USER=deploy ./install.shThe server supports both YAML and JSON configuration formats. By default, it looks for the configuration file at /etc/gitwh.yaml.
listen: ":8080"
buffer_size: 3
timeout: 10
repos:
my-repo:
secret: "optional-webhook-secret"
branch: "main"
folders:
- "/path/to/local/repo"
- "/path/to/another/repo"
after_pull: "bash tools/deploy.sh apply"
after_pull_timeout: 300my-repo - name of your repository. In case of https://github.com/tolixx/gitwh it is gitwh. folders - path to your local copy of this repo.
{
"listen": ":8080",
"buffer_size": 3,
"timeout": 10,
"repos": {
"my-repo": {
"secret": "optional-webhook-secret",
"folders": ["/path/to/local/repo"]
}
}
}listen: Server listening address and port (default::8080)buffer_size: Event buffer size for concurrent requests (default:3)timeout: Git pull timeout in seconds, must be positive (default:10)repos: Map of repository configurationssecret: Optional webhook secret, the same value you enter in GitHub or GitLab (see Secrets below)branch: Optional. Pull only on pushes to this branch, ignore the restfolders: Array of local repository paths to pull, at least oneafter_pull: Optional shell command run after a pull (see After pull below)after_pull_timeout: Seconds before the command is killed (default:300)
Unknown keys are an error, so a typo like folder: fails at startup instead of being ignored.
after_pull runs with /bin/sh -c in each folder of the repo, as the service user, after a git pull that moved HEAD. A relative path like tools/deploy.sh points into the pulled repo.
- It does not run when the pull failed or brought nothing new. A redelivery of the same push will not rerun a failed command: push a new commit or run it by hand.
- It runs in the folder's worker, so the next pull of that folder waits for it.
- Environment:
GITWH_REPO,GITWH_FOLDER,GITWH_OLD_HEAD,GITWH_NEW_HEAD, plus the service's own. - Output (the last 8 KB) and the exit status go to the log. On timeout the whole process group is killed, children included.
- Nothing from the webhook payload reaches the command, it is taken from the config only.
- A folder may get
after_pullfrom one repo only.
- GitHub never sends the secret itself. It signs the body with it and sends the signature in
X-Hub-Signature-256; gitwh checks that signature. Both content types (application/jsonandapplication/x-www-form-urlencoded) work. - GitLab sends the secret token as is in
X-Gitlab-Token; gitwh compares it.
Without a secret anyone who can reach /wh and knows the repo name can trigger a pull. Set one if the port is reachable from the internet.
# Use default config file (/etc/gitwh.yaml)
./gitwh
# Use custom config file
./gitwh -config /path/to/config.yamlSet up webhooks in your GitHub/GitLab repository to point to:
http://your-server:8080/wh
# Check service status
sudo systemctl status gitwh.service
# Start service
sudo systemctl start gitwh.service
# Stop service
sudo systemctl stop gitwh.service
# Restart service
sudo systemctl restart gitwh.service
# View logs
sudo journalctl -u gitwh.service -f- Webhook Reception: The server listens for HTTP POST requests on the
/whendpoint - Payload Processing: Tells GitHub from GitLab by the
X-GitHub-Event/X-Gitlab-Eventheader and parses the payload - Repository Validation: Checks that the repository is configured and verifies the signature or token if a secret is set
- Filtering: Pings, non-push events, tag pushes and pushes to other branches (when
branchis set) get202and no pull - Git Pull: Executes
git pullon configured local repository paths, git output goes to the log on failure - After Pull: Runs the repo's
after_pullcommand in the folder if the pull brought new commits - Concurrency Control: At most one pull per folder at a time. Pushes that arrive during a pull collapse into one follow-up pull
GET /: Returns 404 Not FoundPOST /wh: Webhook endpoint for GitHub/GitLab push events.200pull queued,202delivery accepted but ignored,400unknown repo, bad payload or bad secret
The application is structured into several packages:
main.go: Entry point and HTTP server setupconfig/: Configuration loading and parsinghandlers/: HTTP request handling and webhook processingpuller/: Git pull interface and implementationpuller/git/: Git-specific pull implementation with concurrency control
- GitHub HMAC signature and GitLab token validation, constant-time comparison
- Request body limit and HTTP server timeouts
- Configurable git timeouts, git never waits for a password prompt
- Mutex-based concurrency control to prevent race conditions
- Minimal attack surface with only one webhook endpoint
github.com/go-chi/chi/v5: HTTP router and middlewaregopkg.in/yaml.v3: YAML configuration parsing
This project is provided as-is without any specific license terms.