Debian Codegraph @

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

raw
1 ---
2 title: Host on GitLab Pages
3 description: Host your project on GitLab Pages.
4 categories: []
5 keywords: []
6 aliases: [/hosting-and-deployment/hosting-on-gitlab/]
7 ---
8
9 Use these instructions to enable continuous deployment from a GitLab repository to GitLab Pages.
10
11 {{% include "/_common/gitignore-public.md" %}}
12
13 ## Prerequisites
14
15 Please complete the following tasks before continuing:
16
17 1. [Create](https://gitlab.com/users/sign_up) a GitLab account.
18 1. [Log in](https://gitlab.com/users/sign_in) to your GitLab account.
19 1. [Create](https://gitlab.com/projects/new) a GitLab repository for your project.
20 1. [Create](https://git-scm.com/docs/git-init) a local Git repository for your project with a [remote][] reference to your GitLab repository.
21 1. Create a Hugo project within your local Git repository and test it with the `hugo server` command.
22 1. Commit the changes to your local Git repository and push to your GitLab repository.
23
24 ## BaseURL
25
26 The [`baseURL`][] in your project configuration must reflect the full URL of your GitLab Pages repository if you are using the default GitLab Pages URL (e.g., `https://<YourUsername>.gitlab.io/<your-hugo-site>/`) and not a custom domain.
27
28 ## Procedure
29
30 Step 1
31 : Create a `.gitlab-ci.yml` file in the root of your project, adjusting the tool versions and time zone as needed.
32
33 ```yaml {file=".gitlab-ci.yml" copy=true}
34 variables:
35 # Define tool versions
36 DART_SASS_VERSION: 1.101.0
37 GO_VERSION: 1.26.4
38 HUGO_VERSION: 0.163.3
39 NODE_VERSION: 24.16.0
40
41 # Set the build timezone
42 TZ: Europe/Oslo
43
44 # Set the build cache directory
45 HUGO_CACHEDIR: ${CI_PROJECT_DIR}/.cache/hugo
46
47 # Set the repository clone and fetch strategy
48 GIT_DEPTH: 0
49 GIT_STRATEGY: clone
50 GIT_SUBMODULE_STRATEGY: recursive
51 cache:
52 key: ${CI_COMMIT_REF_SLUG}
53 fallback_keys:
54 - ${CI_DEFAULT_BRANCH}
55 paths:
56 - .cache/hugo
57 image:
58 name: buildpack-deps:bookworm
59 pages:
60 stage: deploy
61 script:
62 - chmod a+x build.sh && ./build.sh
63 artifacts:
64 paths:
65 - public
66 rules:
67 - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
68 ```
69
70 Step 2
71 : Create a `build.sh` file in the root of your project.
72
73 ```sh {file="build.sh" copy=true}
74 #!/usr/bin/env bash
75
76 #------------------------------------------------------------------------------
77 # @file
78 # Builds a Hugo project hosted on GitLab Pages.
79 #------------------------------------------------------------------------------
80
81 # Exit on error, undefined variables, or pipe failures
82 set -euo pipefail
83
84 # Perform cleanup
85 cleanup() {
86 if [[ -n "${build_temp_dir:-}" && -d "${build_temp_dir}" ]]; then
87 rm -rf "${build_temp_dir}"
88 fi
89 }
90
91 # Register the cleanup trap
92 trap cleanup EXIT SIGINT SIGTERM
93
94 main() {
95 # Create a temporary directory for downloads
96 build_temp_dir=$(mktemp -d)
97
98 # Create a local tools directory
99 mkdir -p "${HOME}/.local"
100
101 # Install utilities
102 echo "Installing utilities..."
103 apt-get update > /dev/null
104 apt-get install -y brotli > /dev/null
105
106 # Install Dart Sass
107 echo "Installing Dart Sass ${DART_SASS_VERSION}..."
108 curl -sfLO --output-dir "${build_temp_dir}" "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
109 tar -C "${HOME}/.local" -xf "${build_temp_dir}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
110 export PATH="${HOME}/.local/dart-sass:${PATH}"
111
112 # Install Go
113 if [[ -f "${CI_PROJECT_DIR}/go.mod" ]]; then
114 echo "Installing Go ${GO_VERSION}..."
115 curl -sfLO --output-dir "${build_temp_dir}" "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz"
116 tar -C "${HOME}/.local" -xf "${build_temp_dir}/go${GO_VERSION}.linux-amd64.tar.gz"
117 export PATH="${HOME}/.local/go/bin:${PATH}"
118 fi
119
120 # Install Hugo
121 echo "Installing Hugo ${HUGO_VERSION}..."
122 curl -sfLO --output-dir "${build_temp_dir}" "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz"
123 mkdir -p "${HOME}/.local/hugo"
124 tar -C "${HOME}/.local/hugo" -xf "${build_temp_dir}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz"
125 export PATH="${HOME}/.local/hugo:${PATH}"
126
127 # Install Node.js
128 if [[ -f "${CI_PROJECT_DIR}/package-lock.json" ]]; then
129 echo "Installing Node.js ${NODE_VERSION}..."
130 curl -sfLO --output-dir "${build_temp_dir}" "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz"
131 tar -C "${HOME}/.local" -xf "${build_temp_dir}/node-v${NODE_VERSION}-linux-x64.tar.gz"
132 export PATH="${HOME}/.local/node-v${NODE_VERSION}-linux-x64/bin:${PATH}"
133 fi
134
135 # Log tool versions
136 echo "Logging tool versions..."
137 command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed"
138 command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed"
139 command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed"
140 command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed"
141
142 # Configure Git
143 echo "Configuring Git..."
144 git config --global core.quotepath false
145
146 # Fetch full Git history
147 if [[ $(git rev-parse --is-shallow-repository) == true ]]; then
148 echo "Fetching full Git history..."
149 git fetch --unshallow
150 fi
151
152 # Initialize Git submodules
153 if [[ -f .gitmodules ]]; then
154 echo "Initializing Git submodules..."
155 git submodule update --init --recursive
156 fi
157
158 # Install Node.js dependencies
159 if [[ -f package-lock.json ]]; then
160 echo "Installing Node.js dependencies..."
161 npm ci
162 fi
163
164 # Build the project
165 echo "Building the project..."
166 hugo build --gc --minify
167
168 # Compress published files
169 echo "Compressing published files..."
170 find public/ -type f -regextype posix-extended -regex '.+\.(cjs|css|html|js|json|mjs|svg|txt|xml)$' -print0 > "${build_temp_dir}/files.txt"
171 xargs --null --max-procs=0 --max-args=1 brotli --quality=10 --force --keep < "${build_temp_dir}/files.txt"
172 xargs --null --max-procs=0 --max-args=1 gzip -9 --force --keep < "${build_temp_dir}/files.txt"
173 }
174
175 main "$@"
176 ```
177
178 Step 3
179 : In your project configuration, change the location of the image cache to the [`cacheDir`][] as shown below:
180
181 {{< code-toggle file=hugo copy=true >}}
182 [caches.images]
183 dir = ':cacheDir/images'
184 {{< /code-toggle >}}
185
186 See [configure file caches][] for more information.
187
188 Step 4
189 : Commit the changes to your local Git repository and push to your GitLab repository.
190
191 Step 5
192 : From your GitLab repository, navigate to **Build**&nbsp;>&nbsp;**Pipelines** to follow the CI pipeline building your page.
193
194 Step 6
195 : When the pipeline has passed, your new website is available at `https://<YourUsername>.gitlab.io/<your-hugo-site>/`.
196
197 In the future, whenever you push a change from your local Git repository, GitLab Pages will rebuild and deploy your site.
198
199 [`baseURL`]: /configuration/all/#baseurl
200 [`cacheDir`]: /configuration/all/#cachedir
201 [configure file caches]: /configuration/caches/
202 [remote]: https://git-scm.com/docs/git-remote