Debian Codegraph @

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

raw
1 ---
2 title: Host on Vercel
3 description: Host your project on Vercel.
4 categories: []
5 keywords: []
6 ---
7
8 Use these instructions to enable continuous deployment from a GitHub repository. The same general steps apply for other Git providers such as GitLab or Bitbucket.
9
10 {{% include "/_common/gitignore-public.md" %}}
11
12 ## Prerequisites
13
14 Please complete the following tasks before continuing:
15
16 1. [Create](https://vercel.com/signup) a Vercel account.
17 1. [Log in](https://vercel.com/login) to your Vercel account.
18 1. [Create](https://github.com/signup) a GitHub account.
19 1. [Log in](https://github.com/login) to your GitHub account.
20 1. [Create](https://github.com/new) a GitHub repository for your project.
21 1. [Create](https://git-scm.com/docs/git-init) a local Git repository for your project with a [remote][] reference to your GitHub repository.
22 1. Create a Hugo project within your local Git repository and test it with the `hugo server` command.
23 1. Commit the changes to your local Git repository and push to your GitHub repository.
24
25 ## Procedure
26
27 Step 1
28 : Create a `vercel.json` file in the root of your project.
29
30 ```json {file="vercel.json" copy=true}
31 {
32 "$schema": "https://openapi.vercel.sh/vercel.json",
33 "installCommand": "",
34 "buildCommand": "chmod a+x build.sh && ./build.sh",
35 "outputDirectory": "public"
36 }
37 ```
38
39 Step 2
40 : Create a `build.sh` file in the root of your project, adjusting the tool versions and time zone as needed.
41
42 ```sh {file="build.sh" copy=true}
43 #!/usr/bin/env bash
44
45 #------------------------------------------------------------------------------
46 # @file
47 # Builds a Hugo project hosted on Vercel.
48 #------------------------------------------------------------------------------
49
50 # Exit on error, undefined variables, or pipe failures
51 set -euo pipefail
52
53 # Define tool versions
54 DART_SASS_VERSION=1.102.0
55 GO_VERSION=1.26.5
56 HUGO_VERSION=0.165.0
57 NODE_VERSION=24.19.0
58
59 # Set the build time zone
60 TZ=Europe/Oslo
61
62 # Set the build cache directory
63 HUGO_CACHEDIR="${PWD}/.vercel/cache/hugo"
64
65 # Perform cleanup
66 cleanup() {
67 if [[ -n "${build_temp_dir:-}" && -d "${build_temp_dir}" ]]; then
68 rm -rf "${build_temp_dir}"
69 fi
70 }
71
72 # Register the cleanup trap
73 trap cleanup EXIT SIGINT SIGTERM
74
75 main() {
76 # Export the build time zone
77 export TZ
78
79 # Export the build cache directory
80 export HUGO_CACHEDIR
81
82 # Create a temporary directory for downloads
83 build_temp_dir=$(mktemp -d)
84
85 # Create a local tools directory
86 mkdir -p "${HOME}/.local"
87
88 # Install Dart Sass
89 echo "Installing Dart Sass ${DART_SASS_VERSION}..."
90 curl -sfL --output-dir "${build_temp_dir}" -O "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
91 tar -C "${HOME}/.local" -xf "${build_temp_dir}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
92 export PATH="${HOME}/.local/dart-sass:${PATH}"
93
94 # Install Go
95 if [[ -f "go.mod" ]]; then
96 echo "Installing Go ${GO_VERSION}..."
97 curl -sfL --output-dir "${build_temp_dir}" -O "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz"
98 tar -C "${HOME}/.local" -xf "${build_temp_dir}/go${GO_VERSION}.linux-amd64.tar.gz"
99 export PATH="${HOME}/.local/go/bin:${PATH}"
100 fi
101
102 # Install Hugo
103 echo "Installing Hugo ${HUGO_VERSION}..."
104 curl -sfL --output-dir "${build_temp_dir}" -O "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz"
105 mkdir -p "${HOME}/.local/hugo"
106 tar -C "${HOME}/.local/hugo" -xf "${build_temp_dir}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz"
107 export PATH="${HOME}/.local/hugo:${PATH}"
108
109 # Install Node.js
110 if [[ -f "package-lock.json" ]]; then
111 echo "Installing Node.js ${NODE_VERSION}..."
112 curl -sfL --output-dir "${build_temp_dir}" -O "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz"
113 tar -C "${HOME}/.local" -xf "${build_temp_dir}/node-v${NODE_VERSION}-linux-x64.tar.gz"
114 export PATH="${HOME}/.local/node-v${NODE_VERSION}-linux-x64/bin:${PATH}"
115 fi
116
117 # Log tool versions
118 echo "Logging tool versions..."
119 command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed"
120 command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed"
121 command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed"
122 command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed"
123
124 # Configure Git
125 echo "Configuring Git..."
126 git config --global core.quotepath false
127
128 # Fetch full Git history
129 if [[ $(git rev-parse --is-shallow-repository) == true ]]; then
130 echo "Fetching full Git history..."
131 git fetch --unshallow
132 fi
133
134 # Initialize Git submodules
135 if [[ -f .gitmodules ]]; then
136 echo "Initializing Git submodules..."
137 git submodule update --init --recursive
138 fi
139
140 # Install Node.js dependencies
141 if [[ -f package-lock.json ]]; then
142 echo "Installing Node.js dependencies..."
143 npm ci
144 fi
145
146 # Build the project
147 echo "Building the project..."
148 hugo build --gc --minify
149 }
150
151 main "$@"
152 ```
153
154 Step 3
155 : In your project configuration, change the location of the image cache to the [`cacheDir`][] as shown below:
156
157 {{< code-toggle file=hugo copy=true >}}
158 [caches.images]
159 dir = ':cacheDir/images'
160 {{< /code-toggle >}}
161
162 See [configure file caches][] for more information.
163
164 Step 4
165 : Commit the changes to your local Git repository and push to your GitHub repository.
166
167 Step 5
168 : In the upper right corner of the Vercel dashboard, press the **Add New** button and select "Project" from the drop down menu.
169
170 ![screen capture](vercel-01.png)
171
172 Step 6
173 : Press the "Continue with GitHub" button.
174
175 ![screen capture](vercel-02.png)
176
177 Step 7
178 : Press the **Authorize Vercel** button to allow the Vercel application to access your GitHub account.
179
180 ![screen capture](vercel-03.png)
181
182 Step 8
183 : Press the **Install** button to install the Vercel application.
184
185 ![screen capture](vercel-04.png)
186
187 Step 9
188 : Select the GitHub account where you want to install the Vercel application.
189
190 ![screen capture](vercel-05.png)
191
192 Step 10
193 : Authorize the Vercel application to access all repositories or only select repositories, then press the **Install** button.
194
195 ![screen capture](vercel-06.png)
196
197 Your browser will be redirected to the Cloudflare dashboard.
198
199 Step 11
200 : Press the **Import** button to the right of the name of your GitHub repository.
201
202 ![screen capture](vercel-07.png)
203
204 Step 12
205 : On the "New Project" page, leave the settings at their default values and press the **Deploy** button.
206
207 ![screen capture](vercel-08.png)
208
209 Step 13
210 : When the deployment completes, press the **Continue to Dashboard" button at the bottom of the page.
211
212 ![screen capture](vercel-09.png)
213
214 Step 14
215 : On the "Production Deployment" page, click on the link to your published site.
216
217 ![screen capture](vercel-10.png)
218
219 In the future, whenever you push a change from your local Git repository, Vercel will rebuild and deploy your site.
220
221 [`cacheDir`]: /configuration/all/#cachedir
222 [configure file caches]: /configuration/caches/
223 [remote]: https://git-scm.com/docs/git-remote