Lessons learned while automating Jekyll Site Deployment
Create your jekyll site:
gem install jekyll bundler
jekyll new ccjordan.de
cd ccjordan.de
git init
git remote add origin ssh://cicd@ccjordan.de:/var/git/ccjordan.de.git/
On the server create the git repo and install ruby
sudo apt-get install ruby-dev
mkdir ccjordan.de
cd ccjordan.de
git init --bare
Create the folders and files for nginx
sudo mkdir -p /var/www/vhosts/ccjordan.de/httpfiles/
sudo chown cicd /var/www/vhosts/ccjordan.de/httpfiles/
echo "server {
listen 443;
gzip on;
gzip_types text/plain application/xml;
gzip_proxied no-cache no-store private expired auth;
gzip_min_length 1000;
server_name ccjordan.de;
root /var/www/vhosts/ccjordan.de/httpdocs/;
location / {
index index.html;
}
}" > /etc/nginx/sites-available/ccjordan.de
sudo certbot --nginx -d ccjordan.de
Certbot messed up the nginx config, you will need to look at your default config and correct it.
Clone Git Repo to local folder.
cd ~/
git clone /var/git/ccjordan.de.git/
Setup githook
echo "#!/bin/bash
bash /home/cicd/ccjordan.de/tools/buildAndUpdate.sh" > /var/git/ccjordan.de.git/hooks/post-update
chmod +x /var/git/ccjordan.de.git/hooks/post-update
In the script called by the githook the actual magic happens
#!/bin/bash
cd /home/cicd/ccjordan.de/
unset GIT*DIR
git reset --hard
git checkout develop
git pull
bundle install
bundle exec jekyll clean && bundle exec jekyll build
rm -rf /var/www/vhosts/ccjordan.de/httpdocs/*
cp -r \_site/\* /var/www/vhosts/ccjordan.de/httpdocs/
And that’s it.
The important part is the third line, it clears some variables and makes the git command work. Without this line, the directory is not recognized as a git repo.
You can edit your site localy and debug it, using the command
bundle exec jekyll serve --incremental
when you are happy, commit your changes and push them, then the post-update hook will take care of the update.
The only caveat is updates to the build script take two pushes to take effect.