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][] 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 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.101.0
68 GO_VERSION: 1.26.4
69 HUGO_VERSION: 0.163.3
70 NODE_VERSION: 24.16.0
71
72 # Set the build time zone
73 TZ: Europe/Oslo
74 steps:
75 - name: Checkout
76 uses: actions/checkout@v6
77 with:
78 submodules: recursive
79 fetch-depth: 0
80
81 - name: Setup Pages
82 id: pages
83 uses: actions/configure-pages@v6
84
85 - name: Create a local tools directory
86 run: |
87 mkdir -p "${HOME}/.local"
88
89 - name: Install Go
90 if: hashFiles('go.mod') != ''
91 uses: actions/setup-go@v6
92 with:
93 go-version: ${{ env.GO_VERSION }}
94 cache: false
95
96 - name: Install Node.js
97 if: hashFiles('package-lock.json') != ''
98 uses: actions/setup-node@v6
99 with:
100 node-version: ${{ env.NODE_VERSION }}
101
102 - name: Install Dart Sass
103 run: |
104 echo "Installing Dart Sass ${DART_SASS_VERSION}..."
105 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"
106 tar -C "${HOME}/.local" -xf "${{ runner.temp }}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
107 echo "${HOME}/.local/dart-sass" >> "${GITHUB_PATH}"
108
109 - name: Install Hugo
110 run: |
111 echo "Installing Hugo ${HUGO_VERSION}..."
112 curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz"
113 mkdir "${HOME}/.local/hugo"
114 tar -C "${HOME}/.local/hugo" -xf "${{ runner.temp }}/hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz"
115 echo "${HOME}/.local/hugo" >> "${GITHUB_PATH}"
116
117 - name: Log tool versions
118 run: |
119 echo "Logging tool versions..."
120 command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed"
121 command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed"
122 command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed"
123 command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed"
124
125 - name: Configure Git
126 run: |
127 echo "Configuring Git..."
128 git config --global core.quotepath false
129
130 - name: Fetch full Git history
131 run: |
132 if [[ $(git rev-parse --is-shallow-repository) == true ]]; then
133 echo "Fetching full Git history..."
134 git fetch --unshallow
135 fi
136
137 - name: Initialize Git submodules
138 run: |
139 if [[ -f .gitmodules ]]; then
140 echo "Initializing Git submodules..."
141 git submodule update --init --recursive
142 fi
143
144 - name: Install Node.js dependencies
145 run: |
146 if [[ -f package-lock.json ]]; then
147 echo "Installing Node.js dependencies..."
148 npm ci
149 fi
150
151 - name: Cache restore
152 id: cache-restore
153 uses: actions/cache/restore@v5
154 with:
155 path: ${{ runner.temp }}/hugo_cache
156 key: hugo-${{ github.run_id }}
157 restore-keys: hugo-
158
159 - name: Build
160 run: |
161 echo "Building the project..."
162 hugo build \
163 --gc \
164 --minify \
165 --baseURL "${{ steps.pages.outputs.base_url }}/" \
166 --cacheDir "${{ runner.temp }}/hugo_cache"
167
168 - name: Cache save
169 uses: actions/cache/save@v5
170 with:
171 path: ${{ runner.temp }}/hugo_cache
172 key: ${{ steps.cache-restore.outputs.cache-primary-key }}
173
174 - name: Upload artifact
175 uses: actions/upload-pages-artifact@v5
176 with:
177 include-hidden-files: false
178 path: ./public
179 deploy:
180 runs-on: ubuntu-latest
181 needs: build
182 environment:
183 name: github-pages
184 url: ${{ steps.deployment.outputs.page_url }}
185 steps:
186 - name: Deploy to GitHub Pages
187 uses: actions/deploy-pages@v5
188 ```
189
190 Step 3
191 : In your project configuration, change the location of the image cache to the [`cacheDir`][] as shown below:
192
193 {{< code-toggle file=hugo copy=true >}}
194 [caches.images]
195 dir = ':cacheDir/images'
196 {{< /code-toggle >}}
197
198 See [configure file caches][] for more information.
199
200 Step 4
201 : Commit the changes to your local Git repository and push to your GitHub repository.
202
203 Step 5
204 : From GitHub's main menu, choose **Actions**. You will see something like this:
205
206 ![screen capture](gh-pages-03.png)
207
208 Step 6
209 : When GitHub has finished building and deploying your site, the color of the status indicator will change to green.
210
211 ![screen capture](gh-pages-04.png)
212
213 Step 7
214 : Click on the commit message as shown above. Under the deploy step, you will see a link to your live site.
215
216 ![screen capture](gh-pages-05.png)
217
218 In the future, whenever you push a change from your local Git repository, GitHub Pages will rebuild and deploy your site.
219
220 ## Other resources
221
222 - [Learn more about GitHub Actions][]
223 - [Caching dependencies to speed up workflows][]
224 - [Manage a custom domain for your GitHub Pages site][]
225
226 [Caching dependencies to speed up workflows]: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows
227 [GitHub Pages documentation]: https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#types-of-github-pages-sites
228 [Learn more about GitHub Actions]: https://docs.github.com/en/actions
229 [Log in]: https://github.com/login
230 [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
231 [`cacheDir`]: /configuration/all/#cachedir
232 [configure file caches]: /configuration/caches/
233 [remote]: https://git-scm.com/docs/git-remote