Skip to content

Instantly share code, notes, and snippets.

@faizalmansor
Last active December 15, 2020 12:39
Show Gist options
  • Save faizalmansor/4a02a0c5adfcabbe6b08771feb62e15c to your computer and use it in GitHub Desktop.
Save faizalmansor/4a02a0c5adfcabbe6b08771feb62e15c to your computer and use it in GitHub Desktop.
Setting up Auto-Deploy to Server with GitLab WebHook

Setting up Auto-Deploy to Server with GitLab WebHook

Original Resource (in Russian): http://yiiframework.ru/forum/viewtopic.php?t=22253

Create ssh keys for Apache or Nginx

Important: Server SSH-Key must be made without passphrase!

You could have passphrase, then you'll need to use ssh-agent to store & run the passphrase.

mkdir /var/www/.ssh
chown -R apache.apache /var/www/.ssh
sudo -u apache ssh-keygen -t rsa -C "[email protected]"

Register new public key (apache public key) to Gitlab

Make sure .git/FETCH_HEAD is owned and group for apache

apache apache     91 Nov 25 09:40 FETCH_HEAD

In /etc/sudoer

apache ALL = NOPASSWD: /usr/bin/git

test as apache to pull:

sudo -u apache git pull

Add hook.sh

#!/bin/bash
now=`date`
printf "\n\nHook started at $now"
echo '---------------------------------------'
echo `
SECONDS=0 &&
git fetch --all && 
git reset --hard origin/master && 
git pull origin master &&
php yii migrate/up --interactive=0 &&
php init --env=Production --overwrite=All &&
chmod +x yii
`

printf "Hook completed after ${SECONDS} seconds\n"
@tranphuoctien
Copy link

Hi how to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment