Marcel Krüger

Grav Deploy-Script

A 1-Klick Grav deploy script using Rsync und SSH

22. Feb. 2020

Deploying a Grav website is easy. It only takes two steps:

  1. Upload files
  2. Clearing the Cache

It is annoying to do this with an FTP client. Clicking around in the GUI is inefficient for command-line-people like me. Furthermore FTP cannot carry out decent diff uploads. So it takes longer until the files are up or is less reliable. To log in via SSH to clear the cache is as annoying.

In addition, an FTP server must of course be running on the server. On this server, there's no FTP server running. The SSH daemon, on the other hand, is of course required and therefore running.

The solution: Rsync and SSH

The following bash script does the two steps above. All files relevant for operation are synchronized via rsync. The acces rights are then adjusted via SSH and the cache is deleted. Done.

If public key authentication is set up on the server, no password has to be entered when executing the script.

The script is stored as deploy.sh in the root directory of the grav installation. The variables at the beginning of the file must of course be adapted to the respective situation.

#!/bin/bash

SERVER_DIR=/var/www/dermarcel
SERVER_USER=www-data
SSH_USER=root
SSH_HOST=dermarcel.de

rsync -rlptD --progress --delete \
    --exclude '/deploy.sh' \
    --exclude '.git' \
    --exclude '/cache/*' \
    --exclude '/logs/*' \
    --exclude '/images/*' \
    --exclude '/backup/*' \
    --exclude '/assets/*' \
    --exclude '/tmp/*' \
    . $SSH_USER@$SSH_HOST:$SERVER_DIR

ssh $SSH_USER@$SSH_HOST "cd $SERVER_DIR && chown -R $SERVER_USER:$SERVER_USER . && sudo -u $SERVER_USER php bin/grav clearcache"

Simply make the script executable: chmod +x deploy.sh and your future deploy is simply done via executing ./deploy.sh from your grav root folder.

No idea how rsync --exclude works? Have a look here.