Spin up a Jekyll Blog + git Revisioning System on Your RHEL Server Part II
Deploy jekyll with git hooks
Preliminaries: (1) see above TASK: Install Latest ruby, rvm, rubygems, bundler and jekyll (2) Execute PART 1
Locally
mkdir myblog
cd myblog
git init
git add .
git commit -m "Initial commit"
In this section we will deploy our project based on hooks your-project/hooks/post-receive
On RHEL server
cd ~/
mkdir myblog.git && cd myblog.eu.git
git init --bare
cd hooks && touch post-receive && vim post-receive
In post-receive
hook paste, the following script. Don’t forget to replace myblog.git with your specific project name
#!/usr/bin/env bash
GIT_REPO=$HOME/myblog.git
TMP_GIT_CLONE=/tmp/myblog.git
PUBLIC_WWW=/var/www/html/myblog
git clone $GIT_REPO $TMP_GIT_CLONE
pushd $TMP_GIT_CLONE
tmp_dir=$(pwd)
printf '%s\n' "You are in .. ${tmp_dir}"
sleep 5
printf '%s\n' "Output in... ${PUBLIC_WWW}"
bundle exec jekyll build -d $PUBLIC_WWW
popd
rm -rf $TMP_GIT_CLONE
exit
Make it executable: chmod +x post-receive
Here is an improved version
#!/usr/bin/env bash
# This is added lately to ensure the environment won't change
export PATH="/home/git/.rvm/gems/ruby-3.1.0/bin:$PATH"
export GEM_HOME="/home/git/.rvm/gems/ruby-3.1.0"
export BUNDLE_PATH="/home/git/.rvm/gems/ruby-3.1.0"
GIT_REPO=$HOME/myblog.git
TMP_GIT_CLONE=/tmp/myblog.git
PUBLIC_WWW=/var/www/html/myblog
# Perform a cleaning
rm -rf $TMP_GIT_CLONE
# Clean cloning
git clone $GIT_REPO $TMP_GIT_CLONE
# Switch Remember the temp folder.
pushd $TMP_GIT_CLONE
# Ensure dependencies are correctly set in the cloned repo before running Jekyll:
cd $TMP_GIT_CLONE
bundle install --path vendor/bundle
# You are in the /temp folder now
tmp_dir=$(pwd)
printf '%s\n' "You are in .. ${tmp_dir}"
sleep 5
printf '%s\n' "Output will go in... ${PUBLIC_WWW}"
# Create/Update the site (all the content from _site folder)
bundle exec jekyll build -d $PUBLIC_WWW --trace
popd
rm -rf $TMP_GIT_CLONE
echo "Executing post-receive hook at $(date)" >> /tmp/post-receive-debug.log
exit
Now every time you git push origin
your files, the post-receive script will deploy and build your site on the server in place.
Sticky Bit
If jekyll won’t run in a specific directory try to run post-receive hook line by line A possible issue causing it the missing /tmp file set as so in linux server:
chmod +t /tmp
Now you have a sticky bit (all of the files in that directory will be modifiable only by their owners). We don’t want other users will modify it.
drwxrwxrwt. 11 root root 4096 Jun 26 19:03 tmp
chmod 777 /tmp
removes the sticky bit from the directory. Without the sticky bit, anyone can
remove, rename or replace a file from/in the directory at any time
Delete Gemfile.lock and run again the bundle install to rebuild all project gem’s dependencies
bundle install.