hugo/content/hosting-and-deployment/hosting-on-github.md
Bjørn Erik Pedersen 337d0c5f51 Squashed 'docs/' changes from 56c34962c..dce236ad1
dce236ad1 Wrap up the bundle etc. edits for now
27d058566 Add the bundle tree to the organization bundle
a83f750dd Move organization.md to its own bundle
01ec4f462 Make the image docs a bundle
60de1e090 Some more resources copy-edits
05d763c0c Some resource copy-edits
6299d6dbb Update the imgproc shortcode
59e0fc209 Add headless bundle documentation
a3bbf60bf Link Page Resources page from Front Matter page
830576f86 Update order significance section, add counter section
3b1836509 Revert the recent change made to default list template
886ed0e10 Page Bundles draft rev 2
f530d1a7a image processing and page resources made into regular .md
ec47cecda Finalised Page Resources and Image Processing Moved Page Resources and Impage Processing out of the Bundle section and at the root of the Content Management section
253092335 Modified front matter metadata exemple. Added yaml version.
da5e4f476 Adding date in the front-matter; missed in previous commit
6bc3ced13 Add rough draft for page and section bundles
a0e44458f Image processing first draft, Resources second read/fix
2367f0b78 data: Remove duplicate layouts in table
c2f179839 First draft of bundles/resources (covers resources and metadata)
2a3f9a613 Add weights to pages in Bundles branch
9a0146cc0 Switch front-matter format of Bundles doc to yaml; add front-matter
1295fc083 First draft for Bundles documentation organization structure
5a2e52231 Fix archetype paths
9c2e5c063 Merge commit '22cced34fc608256f8271ad591a5ccca991bb164'
22cced34f Squashed 'themes/gohugoioTheme/' changes from 75da2f6b..ecad8247
55d16c9a1 Fix broken sentence in multilingual sections
a76895ad2 Replace the outdated Emacs package with new one
e6cf1dec0 Remove obsolete link to hugo roadmap
dd2fd145b Add GitLab Pages to mentioned hosters (#309)
a05ce6bf6 Add 0.34 release notes poster
5c0ebdfca Release 0.34
13c2f3dc8 Merge branch 'temp34'
e6b5ffa04 Add 0.34 poster
1e1960496 releaser: Add release notes to /docs for release of 0.34
ac3efe182 releaser: Bump versions for release of 0.34
8f91f62d8 Fixes #222
cca35dbe4 Fix example
eaaa21ca1 Add missing params key
00d0b0363 Adding new Blogger utility to tools/migrations
7d36d579e Updated the line number for Dockerfile pointer
852188f85 Update installing.md with Fedora instructions
4d151a3ab Update search.md
4c2750bfb Update deployment-with-nanobox.md
c3cc9cd49 configuration: Remove defaultExtension from docs
f7c96b4b5 Update GitHub Pages documentation
55787f09a Merge branch 'rmetzler-menu-link-title'
2abbd9bd9 Merge branch 'master' into menu-link-title
e1fd710b7 Bring archetypes in from theme.
daf6f51c0 Mention the significance of leading 0 in int fn string input
07f498755 Add documentation for `cond` function.
050ccd12b Add documentation for the .HasShortcode function
919af9071 Correct anchor under 'Add custom metadata to a Taxonomy Term'
55600b4ff More layouts work
201cf4f67 Add some more single page layout variants
d5e7c03e2 Rework the layouts doc
84622e67c Cleans up the code sample
c231c9bd5 Add a new note to 0.33 relnotes
328ec9930 Release 0.33
b108fcc7b Merge branch 'temp33' into next
ab9d9ee65 releaser: Prepare repository for 0.34-DEV
e20c75320 releaser: Add release notes to /docs for release of 0.33
49f24dcd1 releaser: Bump versions for release of 0.33
9c8e5e207 Update 0.33 poster
7655603c8 Regenerate the docshelper data
16dc99583 Add Hugo 0.33 poster
ce40cc197 Merge commit '3cf4300097610bb8b5bd0686d96d1df5db641895'
9a3085523 releaser: Prepare repository for 0.33-DEV
a52db97d8 fixing typos and syntax for consistency
64525670f ádd title to some menu entries. This needs hugo >= v0.32
85d415ab2 ádd examples for menu .Title and .Page

git-subtree-dir: docs
git-subtree-split: dce236ad1258a9d9a0ee209f02b2e1f65b46f0fb
2018-01-31 11:07:47 +01:00

11 KiB

title linktitle description date publishdate lastmod categories keywords authors menu weight sections_weight draft toc aliases
Host on GitHub Host on GitHub Deploy Hugo as a GitHub Pages project or personal/organizational site and automate the whole process with a simple shell script. 2014-03-21 2014-03-21 2017-03-30
hosting and deployment
github
git
deployment
hosting
Spencer Lyon
Gunnar Morling
docs
parent weight
hosting-and-deployment 30
30 30 false true
/tutorials/github-pages-blog/

GitHub provides free and fast static hosting over SSL for personal, organization, or project pages directly from a GitHub repository via its GitHub Pages service.

Assumptions

  1. You have Git 2.5 or greater installed on your machine.
  2. You have a GitHub account. Signing up for GitHub is free.
  3. You have a ready-to-publish Hugo website or have at least completed the Quick Start.

Types of GitHub Pages

There are 2 types of GitHub Pages:

  • User/Organization Pages (https://<USERNAME|ORGANIZATION>.github.io/)
  • Project Pages (https://<USERNAME|ORGANIZATION>.github.io/<PROJECT>/)

Please refer to the GitHub Pages documentation to decide which type of site you would like to create as it will determine which of the below methods to use.

To create a User/Organization Pages site, follow the single method in the GitHub User and Organization Pages section below.

To create a Project Pages site, choose a method from the Project Pages section below.

GitHub User or Organization Pages

As mentioned the GitHub Pages documentation, you can host a user/organization page in addition to project pages. Here are the key differences in GitHub Pages websites for Users and Organizations:

  1. You must use a <USERNAME>.github.io to host your generated content
  2. Content from the master branch will be used to publish your GitHub Pages site

This is a much simpler setup as your Hugo files and generated content are published into two different repositories.

Step-by-step Instructions

  1. Create a <YOUR-PROJECT> (e.g. blog) repository on GitHub. This repository will contain Hugo's content and other source files.
  2. Create a <USERNAME>.github.io GitHub repository. This is the repository that will contain the fully rendered version of your Hugo website.
  3. git clone <YOUR-PROJECT-URL> && cd <YOUR-PROJECT>
  4. Make your website work locally (hugo server or hugo server -t <YOURTHEME>) and open your browser to http://localhost:1313.
  5. Once you are happy with the results:
    • Press Ctrl+C to kill the server
    • rm -rf public to completely remove the public directory
  6. git submodule add -b master git@github.com:<USERNAME>/<USERNAME>.github.io.git public. This creates a git submodule. Now when you run the hugo command to build your site to public, the created public directory will have a different remote origin (i.e. hosted GitHub repository). You can automate some of these steps with the following script.

Put it Into a Script

You're almost done. You can also add a deploy.sh script to automate the preceding steps for you. You can also make it executable with chmod +x deploy.sh.

The following are the contents of the deploy.sh script:

#!/bin/bash

echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"

# Build the project.
hugo # if using a theme, replace with `hugo -t <YOURTHEME>`

# Go To Public folder
cd public
# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site `date`"
if [ $# -eq 1 ]
  then msg="$1"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin master

# Come Back up to the Project Root
cd ..

You can then run ./deploy.sh "Your optional commit message" to send changes to <USERNAME>.github.io. Note that you likely will want to commit changes to your <YOUR-PROJECT> repository as well.

That's it! Your personal page should be up and running at https://<USERNAME>.github.io within a couple minutes.

GitHub Project Pages

{{% note %}} Make sure your baseURL key-value in your site configuration reflects the full URL of your GitHub pages repository if you're using the default GH Pages URL (e.g., <USERNAME>.github.io/<PROJECT>/) and not a custom domain. {{% /note %}}

Deployment of Project Pages from /docs folder on master branch

As described in the GitHub Pages documentation, you can deploy from a folder called docs/ on your master branch. To effectively use this feature with Hugo, you need to change the Hugo publish directory in your site's config.toml and config.yaml, respectively:

publishDir = "docs"
publishDir: docs

After running hugo, push your master branch to the remote repository and choose the docs/ folder as the website source of your repo. Do the following from within your GitHub project:

  1. Go to SettingsGitHub Pages
  2. From Source, select "master branch /docs folder". If the option isn't enabled, you likely do not have a docs/ folder in the root of your project.

{{% note %}} The docs/ option is the simplest approach but requires you set a publish directory in your site configuration. You cannot currently configure GitHub pages to publish from another directory on master, and not everyone prefers the output site live concomitantly with source files in version control. {{% /note %}}

Deployment of Project Pages From Your gh-pages branch

You can also tell GitHub pages to treat your master branch as the published site or point to a separate gh-pages branch. The latter approach is a bit more complex but has some advantages:

  • It keeps your source and generated website in different branches and therefore maintains version control history for both.
  • Unlike the preceding docs/ option, it uses the default public folder.

Preparations for gh-pages Branch

These steps only need to be done once. Replace upstream with the name of your remote; e.g., origin:

Add the public Folder

First, add the public folder to your .gitignore file at the project root so that the directory is ignored on the master branch:

echo "public" >> .gitignore
Initialize Your gh-pages Branch

You can now initialize your gh-pages branch as an empty orphan branch:

git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initializing gh-pages branch"
git push upstream gh-pages
git checkout master

Build and Deployment

Now check out the gh-pages branch into your public folder using git's worktree feature. Essentially, the worktree allows you to have multiple branches of the same local repository to be checked out in different directories:

rm -rf public
git worktree add -B gh-pages public upstream/gh-pages

Regenerate the site using the hugo command and commit the generated files on the gh-pages branch:

{{< code file="commit-gh-pages-files.sh">}} hugo cd public && git add --all && git commit -m "Publishing to gh-pages" && cd .. {{< /code >}}

If the changes in your local gh-pages branch look alright, push them to the remote repo:

git push upstream gh-pages
Set gh-pages as Your Publish Branch

In order to use your gh-pages branch as your publishing branch, you'll need to configure the repository within the GitHub UI. This will likely happen automatically once GitHub realizes you've created this branch. You can also set the branch manually from within your GitHub project:

  1. Go to SettingsGitHub Pages
  2. From Source, select "gh-pages branch" and then Save. If the option isn't enabled, you likely have not created the branch yet OR you have not pushed the branch from your local machine to the hosted repository on GitHub.

After a short while, you'll see the updated contents on your GitHub Pages site.

Put it Into a Script

To automate these steps, you can create a script with the following contents:

{{< code file="publish_to_ghpages.sh" >}} #!/bin/sh

DIR=$(dirname "$0")

cd $DIR/..

if $(git status -s) then echo "The working directory is dirty. Please commit any pending changes." exit 1; fi

echo "Deleting old publication" rm -rf public mkdir public git worktree prune rm -rf .git/worktrees/public/

echo "Checking out gh-pages branch into public" git worktree add -B gh-pages public upstream/gh-pages

echo "Removing existing files" rm -rf public/*

echo "Generating site" hugo

echo "Updating gh-pages branch" cd public && git add --all && git commit -m "Publishing to gh-pages (publish.sh)" {{< /code >}}

This will abort if there are pending changes in the working directory and also makes sure that all previously existing output files are removed. Adjust the script to taste, e.g. to include the final push to the remote repository if you don't need to take a look at the gh-pages branch before pushing. Or adding echo yourdomainname.com >> CNAME if you set up for your gh-pages to use customize domain.

Deployment of Project Pages from Your master Branch

To use master as your publishing branch, you'll need your rendered website to live at the root of the GitHub repository. Steps should be similar to that of the gh-pages branch, with the exception that you will create your GitHub repository with the public directory as the root. Note that this does not provide the same benefits of the gh-pages branch in keeping your source and output in separate, but version controlled, branches within the same repo.

You will also need to set master as your publishable branch from within the GitHub UI:

  1. Go to SettingsGitHub Pages
  2. From Source, select "master branch" and then Save.

Use a Custom Domain

If you'd like to use a custom domain for your GitHub Pages site, create a file static/CNAME. Your custom domain name should be the only contents inside CNAME. Since it's inside static, the published site will contain the CNAME file at the root of the published site, which is a requirements of GitHub Pages.

Refer to the official documentation for custom domains for further information.