Debian Codegraph @

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

raw
1 ---
2 title: Host on Cloudflare
3 description: Host your project on Cloudflare.
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://dash.cloudflare.com/sign-up) a Cloudflare account.
17 1. [Log in](https://dash.cloudflare.com/login) to your Cloudflare 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 `wrangler.jsonc` file in the root of your project.
29
30 ```jsonc {file="wrangler.jsonc" copy=true}
31 {
32 // Set this to the name of your project.
33 "name": "test",
34 // Set this to today's date in YYYY-MM-DD format.
35 "compatibility_date": "2026-06-19",
36 "build": {
37 "command": "chmod a+x build.sh && ./build.sh"
38 },
39 "assets": {
40 "directory": "./public",
41 "not_found_handling": "404-page"
42 }
43 }
44 ```
45
46 Step 2
47 : Create a `build.sh` file in the root of your project, adjusting the tool versions and time zone as needed.
48
49 ```sh {file="build.sh" copy=true}
50 #!/usr/bin/env bash
51
52 #------------------------------------------------------------------------------
53 # @file
54 # Builds a Hugo project hosted on a Cloudflare Worker.
55 #------------------------------------------------------------------------------
56
57 # Exit on error, undefined variables, or pipe failures
58 set -euo pipefail
59
60 # Define tool versions
61 DART_SASS_VERSION=1.101.0
62 GO_VERSION=1.26.4
63 HUGO_VERSION=0.163.3
64 NODE_VERSION=24.16.0
65
66 # Set the build time zone
67 TZ=Europe/Oslo
68
69 # Set the build cache directory
70 HUGO_CACHEDIR="${PWD}/.cache/hugo"
71
72 # Perform cleanup
73 cleanup() {
74 if [[ -n "${build_temp_dir:-}" && -d "${build_temp_dir}" ]]; then
75 rm -rf "${build_temp_dir}"
76 fi
77 }
78
79 # Register the cleanup trap
80 trap cleanup EXIT SIGINT SIGTERM
81
82 main() {
83 # Export the build time zone
84 export TZ
85
86 # Export the build cache directory
87 export HUGO_CACHEDIR
88
89 # Create a temporary directory for downloads
90 build_temp_dir=$(mktemp -d)
91
92 # Create a local tools directory
93 mkdir -p "${HOME}/.local"
94
95 # Install Dart Sass
96 echo "Installing Dart Sass ${DART_SASS_VERSION}..."
97 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"
98 tar -C "${HOME}/.local" -xf "${build_temp_dir}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz"
99 export PATH="${HOME}/.local/dart-sass:${PATH}"
100
101 # Install Go
102 if [[ -f "go.mod" ]]; then
103 echo "Installing Go ${GO_VERSION}..."
104 curl -sfL --output-dir "${build_temp_dir}" -O "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz"
105 tar -C "${HOME}/.local" -xf "${build_temp_dir}/go${GO_VERSION}.linux-amd64.tar.gz"
106 export PATH="${HOME}/.local/go/bin:${PATH}"
107 fi
108
109 # Install Hugo
110 echo "Installing Hugo ${HUGO_VERSION}..."
111 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"
112 mkdir -p "${HOME}/.local/hugo"
113 tar -C "${HOME}/.local/hugo" -xf "${build_temp_dir}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz"
114 export PATH="${HOME}/.local/hugo:${PATH}"
115
116 # Install Node.js
117 if [[ -f "package-lock.json" ]]; then
118 echo "Installing Node.js ${NODE_VERSION}..."
119 curl -sfL --output-dir "${build_temp_dir}" -O "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz"
120 tar -C "${HOME}/.local" -xf "${build_temp_dir}/node-v${NODE_VERSION}-linux-x64.tar.gz"
121 export PATH="${HOME}/.local/node-v${NODE_VERSION}-linux-x64/bin:${PATH}"
122 fi
123
124 # Log tool versions
125 echo "Logging tool versions..."
126 command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed"
127 command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed"
128 command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed"
129 command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed"
130
131 # Configure Git
132 echo "Configuring Git..."
133 git config --global core.quotepath false
134
135 # Fetch full Git history
136 if [[ $(git rev-parse --is-shallow-repository) == true ]]; then
137 echo "Fetching full Git history..."
138 git fetch --unshallow
139 fi
140
141 # Initialize Git submodules
142 if [[ -f .gitmodules ]]; then
143 echo "Initializing Git submodules..."
144 git submodule update --init --recursive
145 fi
146
147 # Install Node.js dependencies
148 if [[ -f package-lock.json ]]; then
149 echo "Installing Node.js dependencies..."
150 npm ci
151 fi
152
153 # Build the project
154 echo "Building the project..."
155 hugo build --gc --minify
156 }
157
158 main "$@"
159 ```
160
161 Step 3
162 : In your project configuration, change the location of the image cache to the [`cacheDir`][] as shown below:
163
164 {{< code-toggle file=hugo copy=true >}}
165 [caches.images]
166 dir = ':cacheDir/images'
167 {{< /code-toggle >}}
168
169 See [configure file caches][] for more information.
170
171 Step 4
172 : Commit the changes to your local Git repository and push to your GitHub repository.
173
174 Step 5
175 : In the upper right corner of the Cloudflare [dashboard][], press the **Add** button and select "Workers" from the drop down menu.
176
177 ![screen capture](cloudflare-01.png)
178
179 Step 6
180 : Verify your account if prompted.
181
182 ![screen capture](cloudflare-02.png)
183
184 Step 7
185 : On the "Create a Worker" page, under the "Ship something new" heading, press the **Connect GitHub** button.
186
187 ![screen capture](cloudflare-03.png)
188
189 Step 8
190 : Select the GitHub account where you want to install the Cloudflare Workers and Pages application.
191
192 ![screen capture](cloudflare-04.png)
193
194 Step 9
195 : Authorize the Cloudflare Workers and Pages application to access all repositories or only select repositories, then press the **Install & Authorize** button.
196
197 ![screen capture](cloudflare-05.png)
198
199 Step 10
200 : On the "Create a Worker" page, under the "Select a repository" heading, select the repository to deploy, then press the **Next** button.
201
202 ![screen capture](cloudflare-06.png)
203
204 Step 11
205 : On the "Create a Worker" page, under the "Set up your application" heading, perform the following steps:
206
207 1. Provide a **Project name**.
208 1. Leave the **Build command** blank and ensure the **Deploy command** is `npx wrangler deploy`.
209 1. Expand the **Advanced settings** panel.
210 1. In the **Variable name** field, enter `SKIP_DEPENDENCY_INSTALL`.
211 1. In the **Variable value** field, enter `true`.
212 1. Press the **Deploy** button.
213
214 Step 12
215 : Wait for the site to build and deploy, then press the **Visit** button in the upper left corner of your screen.
216
217 ![screen capture](cloudflare-07.png)
218
219 In the future, whenever you push a change from your local Git repository, Cloudflare will rebuild and deploy your site.
220
221 ## Build cache
222
223 The build script shown in [Step 2](#step-2) sets Hugo's [`cacheDir`][] to the path required by Cloudflare's build cache, which is disabled by default. To enable the Cloudflare build cache, you must complete two steps.
224
225 First, your project must have both a `package.json` and `package-lock.json` file in the project root. If you have only a package.json file, run `npm install` to create the corresponding `package-lock.json` file. If your project does not require any Node.js packages, create both files by running `npm init -y && npm install`.
226
227 Second, you must enable the build cache in your project dashboard.
228
229 1. Navigate to Workers & Pages Overview on the [dashboard][].
230 1. Find your Workers project.
231 1. Go to **Settings**&nbsp;>&nbsp;**Build**&nbsp;>&nbsp;**Build cache**.
232 1. Press the **Enable** button.
233
234 ## Scheduled builds
235
236 If your site uses [`resources.GetRemote`][] to fetch external data at build time, that data is embedded in the static HTML when the site is built. Without a scheduled build, the data only refreshes when someone commits code to the repository. To keep content current, you can trigger a rebuild on a schedule by creating a Cloudflare deploy hook and calling it from a GitHub Actions workflow.
237
238 Step 1
239 : In the Cloudflare [dashboard][], go to **Workers & Pages**. Select your project, then navigate to **Settings**&nbsp;>&nbsp;**Builds**&nbsp;>&nbsp;**Deploy Hooks**. Press **Create deploy hook**, provide a name (e.g., `github-cron`), and copy the generated URL.
240
241 Step 2
242 : In your GitHub repository, go to **Settings**&nbsp;>&nbsp;**Secrets and variables**&nbsp;>&nbsp;**Actions**. Press **New repository secret**, name it `CLOUDFLARE_DEPLOY_HOOK`, paste the deploy hook URL as the value, and save.
243
244 Step 3
245 : Create a GitHub Actions workflow file in your repository.
246
247 ```yaml {file=".github/workflows/scheduled-cloudflare-deploy.yaml" copy=true}
248 name: github-cron
249 on:
250 schedule:
251 - cron: "42 7 * * *"
252 timezone: Etc/UTC
253
254 jobs:
255 deploy:
256 runs-on: ubuntu-latest
257 steps:
258 - name: Trigger Cloudflare deploy hook
259 run: curl -X POST "${{ secrets.CLOUDFLARE_DEPLOY_HOOK }}"
260 ```
261
262 Adjust the [`cron`][] expression to set your desired build schedule. In the example above, the job is scheduled to run every day at 7:42 AM UTC.
263
264 Step 4
265 : Commit the changes to your local Git repository and push to your GitHub repository.
266
267 > [!NOTE]
268 > The schedule event can be delayed during periods of high loads of GitHub Actions workflow runs. High load times include the start of every hour. If the load is sufficiently high enough, some queued jobs may be dropped. To decrease the chance of delay, schedule your workflow to run at a different time of the hour, or use a dedicated third-party scheduling service such as [Google Cloud Scheduler][] or [cron-job.org][].
269
270 [`cacheDir`]: /configuration/all/#cachedir
271 [`cron`]: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#schedule
272 [`resources.GetRemote`]: /functions/resources/getremote/
273 [configure file caches]: /configuration/caches/
274 [cron-job.org]: https://cron-job.org/en/
275 [dashboard]: https://dash.cloudflare.com/
276 [remote]: https://git-scm.com/docs/git-remote
277 [Google Cloud Scheduler]: https://docs.cloud.google.com/scheduler/docs/overview