Skip to content

Instantly share code, notes, and snippets.

@eliyas5044
Last active November 6, 2019 08:38
Show Gist options
  • Save eliyas5044/6a466830d93050ba97e98b6ffb588651 to your computer and use it in GitHub Desktop.
Save eliyas5044/6a466830d93050ba97e98b6ffb588651 to your computer and use it in GitHub Desktop.
Git webhooks to automatically deploy in server from Gitlab webhook
#!/bin/bash
# redirect stdout/stderr to a file
exec > /home/daily/webhooks/dailycricket-dev/output.log 2>&1
# pull latest code from gitlab
echo "Starting to pull from gitlab..."
git pull origin master
# remove node_modules
echo "Removing node_modules folder..."
rm -rf node_modules
# remove .nuxt folder
echo "Removing .nuxt folder..."
rm -rf .nuxt
# install node packages
echo "Installing node packages..."
yarn install
# run production build command
echo "Building project..."
yarn build
# restart pm2 for dev
echo "Restarting pm2 for DEV..."
pm2 restart DEV
[
{
"id": "dailycricket-dev",
"execute-command": "/home/daily/deploy-dev.sh",
"command-working-directory": "/home/daily/DEV",
"response-message": "Executing live deploy script...",
"trigger-rule":
{
"and": [
{
"match":
{
"type": "value",
"value": "1VJrbGY6L8Jp30a7YF7dtJiR+9eAWkUaeGSexhHcZUM=",
"parameter":
{
"source": "header",
"name": "X-Gitlab-Token"
}
},
"match":
{
"parameter":
{
"source": "payload",
"name": "ref"
},
"type": "value",
"value": "refs/heads/master"
}
},
{
"or": [
{
"match":
{
"parameter":
{
"source": "payload",
"name": "object_kind"
},
"type": "value",
"value": "push"
}
},
{
"and": [
{
"match":
{
"parameter":
{
"source": "payload",
"name": "object_kind"
},
"type": "value",
"value": "merge_request"
}
},
{
"match":
{
"parameter":
{
"source": "payload",
"name": "object_attributes.state"
},
"type": "value",
"value": "merged"
}
}
]
}
]
}
]
}
}
]
[Unit]
Description=Webhooks
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=daily
Environment=PATH=/home/daily/local/bin:/home/daily/.nvm/versions/node/v10.14.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ExecStart=/usr/bin/env webhook -hooks /home/daily/webhooks/hooks.json -hotreload -verbose
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment