Skip to content

Instantly share code, notes, and snippets.

@nextlevelshit
Last active November 4, 2020 08:49
Show Gist options
  • Save nextlevelshit/555c5cc02e874ed630f8045b3b9e27b2 to your computer and use it in GitHub Desktop.
Save nextlevelshit/555c5cc02e874ed630f8045b3b9e27b2 to your computer and use it in GitHub Desktop.
Cronjob for Gatsby Deployment and Backup at Uberspace (https://uber.space)

1. Add both bash scripts to root folder of your repository to be deployed

Your root directory might look now like:

.
├── data
├── delete-old-backups.sh         # <------ Delete backups older than 15 days
├── deploy-and-backup-gatsby.sh   # <------ Deploy and backup "old" public directory
├── gatsby-browser.js
├── gatsby-config.js
├── gatsby-node.js
├── gatsby-ssr.js
├── manifest-config.js
├── node_modules
├── package.json
├── package-lock.json
├── public
├── README.md
├── src
├── tailwind.config.js
└── yarn.lock

2. Initiate cronjob

At Uberspace you'll only have to type

crontab -e

and a visual texteditor will open your cronjob table. Edit it and add the paths to your deploy and backup scripts.

3. Done

Now you will get every day two emails with a status log. If the deployment failed, the "old" public directory will be restored and your website will habe almost no downtime.

PATH=/home/roland/bin:/usr/bin:/bin
# Start Deployment and Backup Deletion every day at 4am
0 4 * * * ~/path/to/your/repository/root/directory/deploy-and-backup-gatsby.sh
0 4 * * * ~/path/to/your/repository/root/directory/delete-old-backups.sh
#!/bin/bash
find . -name "public_*" -type d -mtime +15 -exec rm -rf {} +
#!/bin/bash
echo "STARTING DEPLOYMENT ...";
BACKUP="public_`date +%Y%m%d%H%M%s`";
echo "MAKING BACKUP of /public ...";
cp -r public $BACKUP
echo "UPDATING REPO ..."
git pull
echo "INSTALLING DEPENDENCIES ..."
npm install
echo "STARTING BUILD ...";
if npm run build ; then
echo "SUCCESSFULLY DEPLOYED"
else
echo "FAILED TO DEPLOY"
echo "RESTORING BACKUP ..."
rm -r public
mv $BACKUP public
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment