github.com/jbramsden/hugo@v0.47.1/docs/content/en/hosting-and-deployment/hosting-on-github.md (about)

     1  ---
     2  title: Host on GitHub
     3  linktitle: Host on GitHub
     4  description: Deploy Hugo as a GitHub Pages project or personal/organizational site and automate the whole process with a simple shell script.
     5  date: 2014-03-21
     6  publishdate: 2014-03-21
     7  lastmod: 2017-03-30
     8  categories: [hosting and deployment]
     9  keywords: [github,git,deployment,hosting]
    10  authors: [Spencer Lyon, Gunnar Morling]
    11  menu:
    12    docs:
    13      parent: "hosting-and-deployment"
    14      weight: 30
    15  weight: 30
    16  sections_weight: 30
    17  draft: false
    18  toc: true
    19  aliases: [/tutorials/github-pages-blog/]
    20  ---
    21  
    22  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][].
    23  
    24  ## Assumptions
    25  
    26  1. You have Git 2.8 or greater [installed on your machine][installgit].
    27  2. You have a GitHub account. [Signing up][ghsignup] for GitHub is free.
    28  3. You have a ready-to-publish Hugo website or have at least completed the [Quick Start][].
    29  
    30  ## Types of GitHub Pages
    31  
    32  There are 2 types of GitHub Pages:
    33  
    34  - User/Organization Pages (`https://<USERNAME|ORGANIZATION>.github.io/`)
    35  - Project Pages (`https://<USERNAME|ORGANIZATION>.github.io/<PROJECT>/`)
    36  
    37  Please refer to the [GitHub Pages documentation][ghorgs] to decide which type of site you would like to create as it will determine which of the below methods to use.
    38  
    39  To create a User/Organization Pages site, follow the single method in the *GitHub User and Organization Pages* section below.
    40  
    41  To create a Project Pages site, choose a method from the *Project Pages* section below.
    42  
    43  ## GitHub User or Organization Pages
    44  
    45  As mentioned [the GitHub Pages documentation][ghorgs], 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:
    46  
    47  1. You must use a `<USERNAME>.github.io` to host your **generated** content
    48  2. Content from the `master` branch will be used to publish your GitHub Pages site
    49  
    50  This is a much simpler setup as your Hugo files and generated content are published into two different repositories.
    51  
    52  ### Step-by-step Instructions
    53  
    54  1. Create a `<YOUR-PROJECT>` (e.g. `blog`) repository on GitHub. This repository will contain Hugo's content and other source files.
    55  2. Create a `<USERNAME>.github.io` GitHub repository. This is the repository that will contain the fully rendered version of your Hugo website.
    56  3. `git clone <YOUR-PROJECT-URL> && cd <YOUR-PROJECT>`
    57  4. Make your website work locally (`hugo server` or `hugo server -t <YOURTHEME>`) and open your browser to <http://localhost:1313>.
    58  5. Once you are happy with the results:
    59      * Press <kbd>Ctrl</kbd>+<kbd>C</kbd> to kill the server
    60      * `rm -rf public` to completely remove the `public` directory
    61  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.
    62  
    63  ### Put it Into a Script
    64  
    65  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`.
    66  
    67  The following are the contents of the `deploy.sh` script:
    68  
    69  ```
    70  #!/bin/bash
    71  
    72  echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"
    73  
    74  # Build the project.
    75  hugo # if using a theme, replace with `hugo -t <YOURTHEME>`
    76  
    77  # Go To Public folder
    78  cd public
    79  # Add changes to git.
    80  git add .
    81  
    82  # Commit changes.
    83  msg="rebuilding site `date`"
    84  if [ $# -eq 1 ]
    85    then msg="$1"
    86  fi
    87  git commit -m "$msg"
    88  
    89  # Push source and build repos.
    90  git push origin master
    91  
    92  # Come Back up to the Project Root
    93  cd ..
    94  ```
    95  
    96  
    97  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.
    98  
    99  That's it! Your personal page should be up and running at `https://<USERNAME>.github.io` within a couple minutes.
   100  
   101  ## GitHub Project Pages
   102  
   103  {{% note %}}
   104  Make sure your `baseURL` key-value in your [site configuration](/getting-started/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.
   105  {{% /note %}}
   106  
   107  ### Deployment of Project Pages from `/docs` folder on `master` branch
   108  
   109  [As described in the GitHub Pages documentation][ghpfromdocs], 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] `config.toml` and `config.yaml`, respectively:
   110  
   111  ```
   112  publishDir = "docs"
   113  ```
   114  ```
   115  publishDir: docs
   116  ```
   117  
   118  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:
   119  
   120  1. Go to **Settings** &rarr; **GitHub Pages**
   121  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.
   122  
   123  {{% note %}}
   124  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.
   125  {{% /note %}}
   126  
   127  ### Deployment of Project Pages From Your `gh-pages` branch
   128  
   129  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:
   130  
   131  * It keeps your source and generated website in different branches and therefore maintains version control history for both.
   132  * Unlike the preceding `docs/` option, it uses the default `public` folder.
   133  
   134  #### Preparations for `gh-pages` Branch
   135  
   136  These steps only need to be done once. Replace `upstream` with the name of your remote; e.g., `origin`:
   137  
   138  ##### Add the `public` Folder
   139  
   140  First, add the `public` folder to your `.gitignore` file at the project root so that the directory is ignored on the master branch:
   141  
   142  ```
   143  echo "public" >> .gitignore
   144  ```
   145  
   146  ##### Initialize Your `gh-pages` Branch
   147  
   148  You can now initialize your `gh-pages` branch as an empty [orphan branch][]:
   149  
   150  ```
   151  git checkout --orphan gh-pages
   152  git reset --hard
   153  git commit --allow-empty -m "Initializing gh-pages branch"
   154  git push upstream gh-pages
   155  git checkout master
   156  ```
   157  
   158  #### Build and Deployment
   159  
   160  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:
   161  
   162  ```
   163  rm -rf public
   164  git worktree add -B gh-pages public upstream/gh-pages
   165  ```
   166  
   167  Regenerate the site using the `hugo` command and commit the generated files on the `gh-pages` branch:
   168  
   169  {{< code file="commit-gh-pages-files.sh">}}
   170  hugo
   171  cd public && git add --all && git commit -m "Publishing to gh-pages" && cd ..
   172  {{< /code >}}
   173  
   174  If the changes in your local `gh-pages` branch look alright, push them to the remote repo:
   175  
   176  ```
   177  git push upstream gh-pages
   178  ```
   179  
   180  ##### Set `gh-pages` as Your Publish Branch
   181  
   182  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:
   183  
   184  1. Go to **Settings** &rarr; **GitHub Pages**
   185  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.
   186  
   187  After a short while, you'll see the updated contents on your GitHub Pages site.
   188  
   189  #### Put it Into a Script
   190  
   191  To automate these steps, you can create a script with the following contents:
   192  
   193  {{< code file="publish_to_ghpages.sh" >}}
   194  #!/bin/sh
   195  
   196  DIR=$(dirname "$0")
   197  
   198  cd $DIR/..
   199  
   200  if [[ $(git status -s) ]]
   201  then
   202      echo "The working directory is dirty. Please commit any pending changes."
   203      exit 1;
   204  fi
   205  
   206  echo "Deleting old publication"
   207  rm -rf public
   208  mkdir public
   209  git worktree prune
   210  rm -rf .git/worktrees/public/
   211  
   212  echo "Checking out gh-pages branch into public"
   213  git worktree add -B gh-pages public upstream/gh-pages
   214  
   215  echo "Removing existing files"
   216  rm -rf public/*
   217  
   218  echo "Generating site"
   219  hugo
   220  
   221  echo "Updating gh-pages branch"
   222  cd public && git add --all && git commit -m "Publishing to gh-pages (publish.sh)"
   223  {{< /code >}}
   224  
   225  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.
   226  
   227  ### Deployment of Project Pages from Your `master` Branch
   228  
   229  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.
   230  
   231  You will also need to set `master` as your publishable branch from within the GitHub UI:
   232  
   233  1. Go to **Settings** &rarr; **GitHub Pages**
   234  2. From **Source**,  select "master branch" and then **Save**.
   235  
   236  ## Use a Custom Domain
   237  
   238  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.
   239  
   240  Refer to the [official documentation for custom domains][domains] for further information.
   241  
   242  [config]: /getting-started/configuration/
   243  [domains]: https://help.github.com/articles/using-a-custom-domain-with-github-pages/
   244  [ghorgs]: https://help.github.com/articles/user-organization-and-project-pages/#user--organization-pages
   245  [ghpfromdocs]: https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/#publishing-your-github-pages-site-from-a-docs-folder-on-your-master-branch
   246  [ghsignup]: https://github.com/join
   247  [GitHub Pages service]: https://help.github.com/articles/what-is-github-pages/
   248  [installgit]: https://git-scm.com/downloads
   249  [orphan branch]: https://git-scm.com/docs/git-checkout/#git-checkout---orphanltnewbranchgt
   250  [Quick Start]: /getting-started/quick-start/
   251  [submodule]: https://github.com/blog/2104-working-with-submodules
   252  [worktree feature]: https://git-scm.com/docs/git-worktree