on master help sign in
Merge pull request #405 from narrative/narrative-patch-1

Create github-pages-tutorial.md

commitd1461f299ebac3382aa1147f32839d6cda7b715b authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 30 Mar 2016, 12:52 (11 years ago) parent4a5f26cc0e576a
1 file changed+174
web/tutorials/github-pages-tutorial.md+174
web/tutorials/github-pages-tutorial.mdnew file+174
@@ -0,0 +1,174 @@
1+---
2+title: Using Hakyll with GitHub Pages
3+author: Erik Stevenson
4+---
5+
6+## Introduction
7+
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.
9+
10+Working 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.
11+
12+This guide will walkthrough the creation and setup of a GitHub site that has two independent branches.
13+
14+1. ```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.
15+2. ```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.
16+
17+When you're finished, you will be able to, with one command, refresh your website's contents and send any changes to your GitHub Page.
18+
19+In the interest of keeping this guide as simple as possible, I'll be making a few assumptions.
20+
21+1. Haskell is being used with [Stack](http://docs.haskellstack.org/en/stable/README/).
22+2. Creating a user/organization site (vice a project site).
23+3. You haven't changed Hakyll's default output directory of '_site/'.
24+
25+These instructions should be easy to adapt for any situation though.
26+
27+## GitHub Setup
28+
29+1. If required, create a new repository for your blog.
30+2. If required, create a ```master``` branch.
31+2. 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/).
32+3. Create a ```.gitignore``` file with at a minimum, the following entries:
33+
34+```
35+_cache/
36+_site/
37+.stack-work/
38+```
39+
40+## Local setup
41+
42+1. If required, create a new Hakyll project. If you're a stack user, there is a Hakyll template available that makes this step easy.
43+
44+```stack new myblog hakyll-template```
45+
46+2. Create a ```.gitignore``` file in your blog's directory with at a minimum, the same directories listed as in the GitHub repository.
47+3. Use the following git commands to setup your local repository.
48+
49+```
50+git init
51+# create new branch called develop and switch to it.
52+git checkout -b develop
53+# track all the source files for our blog.
54+git add .
55+# make our first commit
56+git commit -m "initial commit."
57+# and add the GitHub repository as a remote.
58+git remote add origin <URL to your GitHub pages repository>
59+```
60+
61+## Deployment
62+
63+So everything's all setup and we're ready to deploy.
64+
65+> **Note:** Performing the following commands from your ```develop``` branch is recommended since you will end up back in that branch at the end.
66+
67+Temporarily save any uncommitted changes that may exist in the current branch.
68+
69+```
70+git stash
71+```
72+
73+Ensure we are in the correct branch.
74+
75+```
76+git checkout develop
77+```
78+
79+Get a clean build of our site.
80+
81+```
82+stack exec myblog clean
83+stack exec myblog build
84+```
85+
86+Update the local list of remote branches to ensure we're able to checkout the branch we want in the next step.
87+
88+```
89+git fetch -all
90+```
91+
92+Switch to the `master` branch.
93+
94+> **Note:** Checking out this branch does not overwrite the files that Hakyll just produced because we have '_site' listed in both .gitignore files.
95+
96+```
97+git checkout -b master --track origin/master
98+```
99+
100+Next, copy the freshly made contents of '_site' over the old ones.
101+
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.
103+
104+```
105+cp -a _site/. .
106+```
107+
108+Commit our changes.
109+
110+```
111+git add -A
112+git commit -m "Publish."
113+```
114+
115+And send them to GitHub.
116+
117+```
118+git push origin master:master
119+```
120+
121+Final clean up and return to the original state.
122+
123+```
124+git checkout develop
125+git branch -D master
126+git stash pop
127+```
128+
129+## Putting it all together
130+
131+Below 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).
132+
133+```
134+# Temporarily store uncommited changes
135+git stash
136+
137+# Verify correct branch
138+git checkout develop
139+
140+# Build new files
141+stack exec myblog clean
142+stack exec myblog build
143+
144+# Get previous files
145+git fetch -all
146+git checkout -b master --track origin/master
147+
148+# Overwrite existing files with new files
149+cp -a _site/. .
150+
151+# Commit
152+git add -A
153+git commit -m "Publish."
154+
155+# Push
156+git push origin master:master
157+
158+# Restoration
159+git checkout develop
160+git branch -D master
161+git stash pop
162+```
163+
164+*And that's it.*
165+
166+## Removing old files with `rsync`
167+
168+Earlier 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`.
169+
170+```
171+rsync -a --filter='P _site/' --delete-excluded _site/ .
172+```
173+
174+The 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.