2title: Using Hakyll with GitHub Pages
8[GitHub Pages](http://pages.github.com) has become a popular static website hosting solution due to its simplicity. Simply push a couple files to a repository and it's off to the races.
10Working with Hakyll on a GitHub Pages-hosted website is complicated slightly due to Hakyll outputting files to a ```_site``` subdirectory, our desire to have the source code as well as the compiled site stored in a single repository, and our desire to automate it.
12This guide will walkthrough the creation and setup of a GitHub site that has two independent branches.
141. ```master``` - This is where your site lives. It's what you see when you go to ```https://<your repo>.github.io```. This branch *needs* to be called master.
152. ```develop``` - This is where your website's source is. That's all your Haskell code, your posts and templates, etc, and it's where you do work from. This name was chosen arbitrarily and can be freely substituted for any name of your choosing.
17When you're finished, you will be able to, with one command, refresh your website's contents and send any changes to your GitHub Page.
19In the interest of keeping this guide as simple as possible, I'll be making a few assumptions.
211. Haskell is being used with [Stack](http://docs.haskellstack.org/en/stable/README/).
222. Creating a user/organization site (vice a project site).
233. You haven't changed Hakyll's default output directory of '_site/'.
25These instructions should be easy to adapt for any situation though.
291. If required, create a new repository for your blog.
302. If required, create a ```master``` branch.
312. If applicable/desired, create/add to your repository any files that your site needs that will not be produced by your Hakyll project. For example, ```CNAME``` as outlined [here](https://help.github.com/articles/setting-up-your-pages-site-repository/).
323. Create a ```.gitignore``` file with at a minimum, the following entries:
421. If required, create a new Hakyll project. If you're a stack user, there is a Hakyll template available that makes this step easy.
44```stack new myblog hakyll-template```
462. Create a ```.gitignore``` file in your blog's directory with at a minimum, the same directories listed as in the GitHub repository.
473. Use the following git commands to setup your local repository.
51# create new branch called develop and switch to it.
52git checkout -b develop
53# track all the source files for our blog.
55# make our first commit
56git commit -m "initial commit."
57# and add the GitHub repository as a remote.
58git remote add origin <URL to your GitHub pages repository>
63So everything's all setup and we're ready to deploy.
65> **Note:** Performing the following commands from your ```develop``` branch is recommended since you will end up back in that branch at the end.
67Temporarily save any uncommitted changes that may exist in the current branch.
73Ensure we are in the correct branch.
79Get a clean build of our site.
82stack exec myblog clean
83stack exec myblog build
86Update the local list of remote branches to ensure we're able to checkout the branch we want in the next step.
92Switch to the `master` branch.
94> **Note:** Checking out this branch does not overwrite the files that Hakyll just produced because we have '_site' listed in both .gitignore files.
97git checkout -b master --track origin/master
100Next, copy the freshly made contents of '_site' over the old ones.
102> **Note:** Deleting a file from your site's source will not remove it from your `master` repository if it has already been published. An alternative to `cp` is discussed at the end of this guide.
112git commit -m "Publish."
115And send them to GitHub.
118git push origin master:master
121Final clean up and return to the original state.
129## Putting it all together
131Below is a complete listing of all the commands used to automate deployment to Github Pages. A `deployCommand` can be set as part of Hakyll's configuration options. More information and an example is provided [here](https://jaspervdj.be/hakyll/reference/Hakyll-Core-Configuration.html).
134# Temporarily store uncommited changes
137# Verify correct branch
141stack exec myblog clean
142stack exec myblog build
146git checkout -b master --track origin/master
148# Overwrite existing files with new files
153git commit -m "Publish."
156git push origin master:master
166## Removing old files with `rsync`
168Earlier it was mentioned a flaw is that deleted files will persist in the published site until deleted manually. This is easily overcome by using `rsync` instead of `cp`.
171rsync -a --filter='P _site/' --filter='P _cache/' --filter='P .git/' --filter='P .gitignore' --delete-excluded _site/ .
174The only drawback this approach has is the requirement that *every* file in your site "go through" Hakyll. Fortunately, in many cases this is not an issue.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list