Posting from multiple computers
One of the issues with my current Jekyll/NearlyFreeSpeech.NET setup is that I haven’t been able to post if I’m not working on my laptop which has the only copy of the site’s git repository. That’s not been a problem previously, but lately I’ve been working from my desktop a bit more and have a project on that machine that I’d like to write up. My goal with this little tweak was to migrate the repository to a server, though setting this up has been more useful than I expected.
Git calls a repository that doesn’t have a local copy of the code a ‘bare’ repository, and to make one you just have to run
git init --bare site.git on the server. I was wondering why all of the information I could find included the .git extension - it turns out that it’s a convention for identifying bare repositories.
To migrate this to a server, I used commands I found in the answers to this StackOverflow question:
# I already had an 'origin' remote, so I had to remove it first: git remote remove origin # Begin tracking the new bare repository git remote add origin ssh://server/path/to/site.git/ # Push the master branch of the local repository to the server git push origin master
Goal achieved! Running
git clone ssh://server/path/to/site.git/ on my desktop worked as intended.
However, while I was looking up how to do this, I found a few posts about automating deployment of Jekyll sites on NFSN using git hooks. My original plan was to use this method by Bart Lantz, updating it with Jesse Squires’ more recent post. However, I ended up taking a slightly different approach as I didn’t need to install or update Jekyll (as it’s already present on NFSN), and cloning/deleting the repository each time caused
Directory not empty errors, even when using
rm -rf. My solution to this was to use a ‘normal’ git repository with a working tree, which I could then build from.
Although pushing to a non-bare repository isn’t recommended, git has an option for repositories where the working tree needs to be present on the server. The only caveat is that the server’s working tree has to be kept clean - if you use the server to work on the site, you have to commit all of your changes, then pull them down to the local machine. Since I don’t work on the server at all, this was perfect for my situation.
- Create a new repository in
git config --local receive.denyCurrentBranch updateInstead- this allows the server to be updated as long as its working tree is clean.
- Set the local repository’s
originto the server address and push it to the server (as outlined above)
- Create a bash script called
#!/bin/bash # set up variables REPO_PATH=$HOME/git/site PUBLIC_WWW=/home/public # build the site, putting it in the PUBLIC_WWW directory jekyll build --source $REPO_PATH --destination $PUBLIC_WWW exit
- Make the script executable:
chmod ug+x /home/private/git/site/.git/hooks/post-receive
This script will then run every time the server’s repository is updated via
git push, building the site directly into the
/home/public directory. No need to build the site locally and
rsync to the server!