| 1 |
--- |
| 2 |
title: Host on GitHub Pages |
| 3 |
description: Host your project on GitHub Pages. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
aliases: [/hosting-and-deployment/hosting-on-github/] |
| 7 |
--- |
| 8 |
|
| 9 |
Use these instructions to enable continuous deployment from a GitHub repository to GitHub Pages. |
| 10 |
|
| 11 |
{{% include "/_common/gitignore-public.md" %}} |
| 12 |
|
| 13 |
## Types of sites |
| 14 |
|
| 15 |
There are three types of GitHub Pages sites: project, user, and organization. Project sites are connected to a specific project hosted on GitHub. User and organization sites are connected to a specific account on GitHub.com. |
| 16 |
|
| 17 |
> [!NOTE] |
| 18 |
> See the [GitHub Pages documentation][] to understand the requirements for repository ownership and naming. |
| 19 |
|
| 20 |
## Prerequisites |
| 21 |
|
| 22 |
Please complete the following tasks before continuing: |
| 23 |
|
| 24 |
1. [Create](https://github.com/signup) a GitHub account. |
| 25 |
1. [Log in](https://github.com/login) to your GitHub account. |
| 26 |
1. [Create](https://github.com/new) a GitHub repository for your project. |
| 27 |
1. [Create](https://git-scm.com/docs/git-init) a local Git repository for your project with a [remote][] reference to your GitHub repository. |
| 28 |
1. Create a Hugo project within your local Git repository and test it with the `hugo server` command. |
| 29 |
1. Commit the changes to your local Git repository and push to your GitHub repository. |
| 30 |
|
| 31 |
## Procedure |
| 32 |
|
| 33 |
Step 1 |
| 34 |
: Visit your GitHub repository. From the main menu choose **Settings** > **Pages**. In the center of your screen you will see this: |
| 35 |
|
| 36 |
 |
| 37 |
|
| 38 |
Change the **Source** to `GitHub Actions`. The change is immediate; you do not have to press a Save button. |
| 39 |
|
| 40 |
 |
| 41 |
|
| 42 |
Step 2 |
| 43 |
: Create a `hugo.yaml` file in the `.github/workflows` directory, adjusting the tool versions and time zone as needed. |
| 44 |
|
| 45 |
```yaml {file=".github/workflows/hugo.yaml" copy=true} |
| 46 |
name: Build and deploy |
| 47 |
on: |
| 48 |
push: |
| 49 |
branches: |
| 50 |
- main |
| 51 |
workflow_dispatch: |
| 52 |
permissions: |
| 53 |
contents: read |
| 54 |
pages: write |
| 55 |
id-token: write |
| 56 |
concurrency: |
| 57 |
group: pages |
| 58 |
cancel-in-progress: false |
| 59 |
defaults: |
| 60 |
run: |
| 61 |
shell: bash |
| 62 |
jobs: |
| 63 |
build: |
| 64 |
runs-on: ubuntu-latest |
| 65 |
env: |
| 66 |
# Define tool versions |
| 67 |
DART_SASS_VERSION: 1.102.0 |
| 68 |
GO_VERSION: 1.26.5 |
| 69 |
HUGO_VERSION: 0.165.0 |
| 70 |
NODE_VERSION: 24.19.0 |
| 71 |
|
| 72 |
# Set the build time zone |
| 73 |
TZ: Europe/Oslo |
| 74 |
steps: |
| 75 |
- name: Checkout |
| 76 |
uses: actions/checkout@v7 |
| 77 |
with: |
| 78 |
submodules: recursive |
| 79 |
fetch-depth: 0 |
| 80 |
lfs: false |
| 81 |
|
| 82 |
- name: Setup Pages |
| 83 |
id: pages |
| 84 |
uses: actions/configure-pages@v6 |
| 85 |
|
| 86 |
- name: Create a local tools directory |
| 87 |
run: | |
| 88 |
mkdir -p "${HOME}/.local" |
| 89 |
|
| 90 |
- name: Install Go |
| 91 |
if: hashFiles('go.mod') != '' |
| 92 |
uses: actions/setup-go@v7 |
| 93 |
with: |
| 94 |
go-version: ${{ env.GO_VERSION }} |
| 95 |
cache: false |
| 96 |
|
| 97 |
- name: Install Node.js |
| 98 |
if: hashFiles('package-lock.json') != '' |
| 99 |
uses: actions/setup-node@v7 |
| 100 |
with: |
| 101 |
node-version: ${{ env.NODE_VERSION }} |
| 102 |
|
| 103 |
- name: Install Dart Sass |
| 104 |
run: | |
| 105 |
echo "Installing Dart Sass ${DART_SASS_VERSION}..." |
| 106 |
curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 107 |
tar -C "${HOME}/.local" -xf "${{ runner.temp }}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 108 |
echo "${HOME}/.local/dart-sass" >> "${GITHUB_PATH}" |
| 109 |
|
| 110 |
- name: Install Hugo |
| 111 |
run: | |
| 112 |
echo "Installing Hugo ${HUGO_VERSION}..." |
| 113 |
curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 114 |
mkdir "${HOME}/.local/hugo" |
| 115 |
tar -C "${HOME}/.local/hugo" -xf "${{ runner.temp }}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 116 |
echo "${HOME}/.local/hugo" >> "${GITHUB_PATH}" |
| 117 |
|
| 118 |
- name: Log tool versions |
| 119 |
run: | |
| 120 |
echo "Logging tool versions..." |
| 121 |
command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed" |
| 122 |
command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed" |
| 123 |
command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed" |
| 124 |
command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed" |
| 125 |
|
| 126 |
- name: Configure Git |
| 127 |
run: | |
| 128 |
echo "Configuring Git..." |
| 129 |
git config --global core.quotepath false |
| 130 |
|
| 131 |
- name: Fetch full Git history |
| 132 |
run: | |
| 133 |
if [[ $(git rev-parse --is-shallow-repository) == true ]]; then |
| 134 |
echo "Fetching full Git history..." |
| 135 |
git fetch --unshallow |
| 136 |
fi |
| 137 |
|
| 138 |
- name: Initialize Git submodules |
| 139 |
run: | |
| 140 |
if [[ -f .gitmodules ]]; then |
| 141 |
echo "Initializing Git submodules..." |
| 142 |
git submodule update --init --recursive |
| 143 |
fi |
| 144 |
|
| 145 |
- name: Install Node.js dependencies |
| 146 |
run: | |
| 147 |
if [[ -f package-lock.json ]]; then |
| 148 |
echo "Installing Node.js dependencies..." |
| 149 |
npm ci |
| 150 |
fi |
| 151 |
|
| 152 |
- name: Cache restore |
| 153 |
id: cache-restore |
| 154 |
uses: actions/cache/restore@v6 |
| 155 |
with: |
| 156 |
path: ${{ runner.temp }}/.cache/hugo |
| 157 |
key: hugo-${{ github.run_id }} |
| 158 |
restore-keys: hugo- |
| 159 |
|
| 160 |
- name: Build |
| 161 |
run: | |
| 162 |
echo "Building the project..." |
| 163 |
hugo build \ |
| 164 |
--gc \ |
| 165 |
--minify \ |
| 166 |
--baseURL "${{ steps.pages.outputs.base_url }}/" \ |
| 167 |
--cacheDir "${{ runner.temp }}/.cache/hugo" |
| 168 |
|
| 169 |
- name: Cache save |
| 170 |
uses: actions/cache/save@v6 |
| 171 |
with: |
| 172 |
path: ${{ runner.temp }}/.cache/hugo |
| 173 |
key: ${{ steps.cache-restore.outputs.cache-primary-key }} |
| 174 |
|
| 175 |
- name: Upload artifact |
| 176 |
uses: actions/upload-pages-artifact@v5 |
| 177 |
with: |
| 178 |
include-hidden-files: false |
| 179 |
path: ./public |
| 180 |
deploy: |
| 181 |
runs-on: ubuntu-latest |
| 182 |
needs: build |
| 183 |
environment: |
| 184 |
name: github-pages |
| 185 |
url: ${{ steps.deployment.outputs.page_url }} |
| 186 |
steps: |
| 187 |
- name: Deploy to GitHub Pages |
| 188 |
id: deployment |
| 189 |
uses: actions/deploy-pages@v5 |
| 190 |
``` |
| 191 |
|
| 192 |
Step 3 |
| 193 |
: In your project configuration, change the location of the image cache to the [`cacheDir`][] as shown below: |
| 194 |
|
| 195 |
{{< code-toggle file=hugo copy=true >}} |
| 196 |
[caches.images] |
| 197 |
dir = ':cacheDir/images' |
| 198 |
{{< /code-toggle >}} |
| 199 |
|
| 200 |
See [configure file caches][] for more information. |
| 201 |
|
| 202 |
Step 4 |
| 203 |
: Commit the changes to your local Git repository and push to your GitHub repository. |
| 204 |
|
| 205 |
Step 5 |
| 206 |
: From GitHub's main menu, choose **Actions**. You will see something like this: |
| 207 |
|
| 208 |
 |
| 209 |
|
| 210 |
Step 6 |
| 211 |
: When GitHub has finished building and deploying your site, the color of the status indicator will change to green. |
| 212 |
|
| 213 |
 |
| 214 |
|
| 215 |
Step 7 |
| 216 |
: Click on the commit message as shown above. Under the deploy step, you will see a link to your live site. |
| 217 |
|
| 218 |
 |
| 219 |
|
| 220 |
In the future, whenever you push a change from your local Git repository, GitHub Pages will rebuild and deploy your site. |
| 221 |
|
| 222 |
## Other resources |
| 223 |
|
| 224 |
- [Learn more about GitHub Actions][] |
| 225 |
- [Caching dependencies to speed up workflows][] |
| 226 |
- [Manage a custom domain for your GitHub Pages site][] |
| 227 |
|
| 228 |
[Caching dependencies to speed up workflows]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows |
| 229 |
[GitHub Pages documentation]: https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#types-of-github-pages-sites |
| 230 |
[Learn more about GitHub Actions]: https://docs.github.com/en/actions |
| 231 |
[Manage a custom domain for your GitHub Pages site]: https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site/about-custom-domains-and-github-pages |
| 232 |
[`cacheDir`]: /configuration/all/#cachedir |
| 233 |
[configure file caches]: /configuration/caches/ |
| 234 |
[remote]: https://git-scm.com/docs/git-remote |