Skip to content

Instantly share code, notes, and snippets.

@ogero
Last active January 4, 2019 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ogero/26f5739da3451134ac13d34097a16521 to your computer and use it in GitHub Desktop.
Save ogero/26f5739da3451134ac13d34097a16521 to your computer and use it in GitHub Desktop.
Install webhook as upstart service

Webhook basic configuration for Bitbucket

Webhook basic configuration for Bitbucket.

Getting Started

These instructions will get you a clean daemonized webhook installation, ready to execute gulp deploy when bitbucket receives a push on a certain branch.

Installing

Webhook (binary at /usr/bin/webhook and configuration file at /etc/hooks.json)

wget https://gist.githubusercontent.com/ogero/26f5739da3451134ac13d34097a16521/raw/webhook-install.sh?r=2 && chmod +x webhook-install.sh && ./webhook-install.sh

Configure your hooks.

nano /etc/hooks.json

Optional (if using gulp and/or git as hook command)

Gulp

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
npm install -g gulp

Add an identity key to ssh config allowing user running webhook to acces the repo.

nano /root/.ssh/config

Host bitbucket.org
   IdentityFile ~/.ssh/some_private_key

chmod 0400 ~/.ssh/some_private_key

Then call your hooks manually or add the hook to bitbicket

Manual trigger: domain:9000/hooks/my-hook-name?token=--some-token-for-manual-deploy--
Bitbucket hook: domain:9000/hooks/my-hook-name

Built With

#!/bin/bash
# This script will deploy appname app using mage (in background and detaching the process)
# Daemon author https://stackoverflow.com/a/6169006/13116
# Call it without --daemon argument for foreground deploy
# Call it with --daemon argument for background deploy
case "$1" in
-d|--daemon)
$0 < /dev/null &> /dev/null & disown
exit 0
;;
*)
;;
esac
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/root/go/bin"
export GOROOT="/usr/local/go"
export GOPATH="/root/go"
export GOCACHE="/root/.cache/go-build"
cd /root/go/src/bitbucket.org/ogero/appname
mage deploy >> /var/log/appname_deploy.log 2>&1
[
{
"id": "my-hook-name",
"command-working-directory": "/home/user/src",
"execute-command": "/usr/local/bin/gulp",
"pass-arguments-to-command": [
{
"source": "string",
"name": "deploy"
}
],
"include-command-output-in-response": true,
"include-command-output-in-response-on-error": true,
"trigger-rule": {
"or": [
{
"match": {
"type": "value",
"value": "--some-token-for-manual-deploy--",
"parameter": {
"source": "url",
"name": "token"
}
}
},
{
"and": [
{
"match": {
"type": "value",
"value": "--ANOTHER-token-for-AUTO-deploy--",
"parameter": {
"source": "url",
"name": "token"
}
}
},
{
"match": {
"type": "value",
"value": "repo:push",
"parameter": {
"source": "header",
"name": "X-Event-Key"
}
}
},
{
"match": {
"type": "value",
"value": "branch",
"parameter": {
"source": "payload",
"name": "push.changes.0.new.type"
}
}
},
{
"match": {
"type": "value",
"value": "production",
"parameter": {
"source": "payload",
"name": "push.changes.0.new.name"
}
}
}
]
}
]
}
},
{
"id": "my-mage-hook-name",
"command-working-directory": "/root/go/src/bitbucket.org/ogero/appname",
"execute-command": "/usr/local/bin/deploy_appname.sh",
"pass-arguments-to-command": [
{
"source": "string",
"name": "--daemon"
}
],
"include-command-output-in-response": true,
"include-command-output-in-response-on-error": true,
"trigger-rule": {
"or": [
{
"match": {
"type": "value",
"value": "--some-token-for-manual-deploy--",
"parameter": {
"source": "url",
"name": "token"
}
}
},
{
"and": [
{
"match": {
"type": "value",
"value": "--ANOTHER-token-for-AUTO-deploy--",
"parameter": {
"source": "url",
"name": "token"
}
}
},
{
"match": {
"type": "value",
"value": "repo:push",
"parameter": {
"source": "header",
"name": "X-Event-Key"
}
}
},
{
"match": {
"type": "value",
"value": "tag",
"parameter": {
"source": "payload",
"name": "push.changes.0.new.type"
}
}
},
{
"match": {
"type": "regex",
"value": "^v\\d+\\.\\d+\\.\\d+(?:-.+)?$",
"parameter": {
"source": "payload",
"name": "push.changes.0.new.name"
}
}
}
]
}
]
}
}
]
#!/bin/bash
echo "Stopping service..."
service webhook stop
echo "Downloading webhook..."
wget -qO- https://github.com/adnanh/webhook/releases/download/2.6.9/webhook-linux-amd64.tar.gz | tar xvz -C /tmp
echo "Copying webhook binary..."
mv /tmp/webhook-linux-amd64/webhook /usr/bin/webhook && rm -rf /tmp/webhook-linux-amd64
chown root:root /usr/bin/webhook
echo "Checking webhook binary version... "
/usr/bin/webhook --version
echo "Generating webhook config file if not exist..."
if [ ! -f /etc/hooks.json ]; then
cat >/tmp/hooks.json <<EOL
[
{
"id": "dummy",
"execute-command": "echo Hello Wordl",
"command-working-directory": "/root"
}
]
EOL
cp /tmp/hooks.json /etc/hooks.json
fi
echo "Generating upstart script..."
cat >/tmp/webhook.conf <<EOL
description "Webhook server"
author "Gero Onativia <geronimox@gmail.com>"
start on runlevel [2345]
stop on shutdown
respawn
chdir /root
script
#while [ 1 ]; do sleep 5; clear; tail /var/log/webhook.log; done
#exec /usr/bin/webhook -verbose -nopanic -hooks /etc/hooks.json >> /var/log/webhook.log 2>&1
exec /usr/bin/webhook -verbose -nopanic -hooks /etc/hooks.json
end script
EOL
sudo cp /tmp/webhook.conf /etc/init/webhook.conf
echo "Checking upstart script syntax and starting service... "
init-checkconf /etc/init/webhook.conf && service webhook start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment