Debian Codegraph @

docs/content/en/host-and-deploy/host-on-github-pages/index.md

raw
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 ![screen capture](gh-pages-01.png)
37
38 Change the **Source** to `GitHub Actions`. The change is immediate; you do not have to press a Save button.
39
40 ![screen capture](gh-pages-02.png)
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 env:
48 # Define tool versions
49 DART_SASS_VERSION: 1.104.0
50 GO_VERSION: 1.27.0
51 HUGO_VERSION: 0.166.0
52 NODE_VERSION: 24.20.0
53
54 # Set the build time zone
55 TZ: Europe/Oslo
56 on:
57 push:
58 branches:
59 - main
60 workflow_dispatch:
61 permissions:
62 contents: read
63 pages: write
64 id-token: write
65 concurrency:
66 group: pages
67 cancel-in-progress: false
68 defaults:
69 run:
70 shell: bash
71 jobs:
72 build:
73 runs-on: ubuntu-latest
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 the project configuration file in the root of your local Git repository, set 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 ![screen capture](gh-pages-03.png)
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 ![screen capture](gh-pages-04.png)
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 ![screen capture](gh-pages-05.png)
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 ## Related resources
223
224 For more information on hosting and managing your site with GitHub Pages, consult the official documentation:
225
226 - [General documentation][]
227 - [Custom domain setup][]
228
229 [Custom domain setup]: https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site
230 [General documentation]: https://docs.github.com/en/pages
231 [GitHub Pages documentation]: https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#types-of-github-pages-sites
232 [`cacheDir`]: /configuration/all/#cachedir
233 [configure file caches]: /configuration/caches/
234 [remote]: https://git-scm.com/docs/git-remote