Skip to content

Instantly share code, notes, and snippets.

@amfg
Last active July 8, 2021 10:17
Show Gist options
  • Save amfg/d84329fdd56b2733a5980308334428bd to your computer and use it in GitHub Desktop.
Save amfg/d84329fdd56b2733a5980308334428bd to your computer and use it in GitHub Desktop.
Simple GitLab webhook using netcat & bash
#!/bin/bash
read request
TOKEN='secret token from gitlab' # from https://gitlab.com/{user}/{project}/settings/integrations
HAS_TOKEN=0
while /bin/true; do
read header
[[ "$header" =~ 'X-Gitlab-Token' ]] && [[ "$header" =~ $TOKEN ]] && HAS_TOKEN=1;
[[ "$header" == $'\r' ]] && break;
done
[[ $HAS_TOKEN -eq 0 ]] && exit 0;
url="${request#POST }"
url="${url% HTTP/*}"
echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nOK\r\n\r\n"
([[ $url = "/optional/endpoint" ]] && $(cd /project && git checkout master && git pull)&>/dev/null) &
while [ 1 ]; do nc -l -p 9000 -q 5 -e ./handle.sh ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment