Tag Archives: repository

Auto deploy from BitBucket repository via Cron

Setup the environment

Create the environment for Apache

mkdir /home/deployment
chown apache:apache /home/deployment

Create ssh key in /home/deployment/.ssh/id_rsa

Create wrapper for git to auth via SSH keys

cat > /home/deployment/git.sh <<EOF

if [ $# -eq 0 ]; then
    echo "Git wrapper script that can specify an ssh-key file
    git.sh -i ssh-key-file git-command
    exit 1

# remove temporary file on exit
trap 'rm -f /home/deployment/.git_ssh.$$' 0

if [ "$1" = "-i" ]; then
    SSH_KEY=$2; shift; shift
    echo "ssh -o StrictHostKeyChecking=no -i $SSH_KEY \$@" > /home/deployment/.git_ssh.$$
    chmod +x /home/deployment/.git_ssh.$$
    export GIT_SSH=/home/deployment/.git_ssh.$$

# in case the git command is repeated
[ "$1" = "git" ] && shift

# Run the git command
git "$@"

Run the fist clone

~# su - apache -s /bin/bash
Last login: Tue Apr  5 16:03:53 BST 2016 on pts/0
-bash-4.2$ cd /var/www/vhosts/

-bash-4.2$ /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa clone [email protected]:repository01.git

Test the Re-Base

Make sure to get into the folder with .git subfolder
(in this case we did the clone into /var/www/vhosts/ BUT the code is stored into a new subfolder pulled with the domain name. So we cd into that one)

-bash-4.2$ cd /var/www/vhosts/blog.com && /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa pull --rebase
Could not create directory '/usr/share/httpd/.ssh'.
Failed to add the host to the list of known hosts (/usr/share/httpd/.ssh/known_hosts).
Current branch master is up to date.

Install the CRON

echo "*/5 * * * * apache cd /var/www/vhosts/blog.com && /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa pull --rebase > /dev/null 2>&1" >> /etc/crontab