|
| 1 | +--- |
| 2 | +title: Host on Cloudflare |
| 3 | +description: Host your site 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 if you are using GitLab for version control. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Please complete the following tasks before continuing: |
| 13 | + |
| 14 | +1. [Create](https://dash.cloudflare.com/sign-up) a Cloudflare account |
| 15 | +1. [Log in](https://dash.cloudflare.com/login) to your Cloudflare account |
| 16 | +1. [Create](https://github.com/signup) a GitHub account |
| 17 | +1. [Log in](https://github.com/login) to your GitHub account |
| 18 | +1. [Create](https://github.com/new) a GitHub repository for your project |
| 19 | +1. [Create](https://git-scm.com/docs/git-init) a local Git repository for your project with a [remote](https://git-scm.com/docs/git-remote) reference to your GitHub repository |
| 20 | +1. Create a Hugo site within your local Git repository and test it with the `hugo server` command |
| 21 | + |
| 22 | +## Procedure |
| 23 | + |
| 24 | +### Step 1 |
| 25 | + |
| 26 | +Create a `wrangler.toml` file in the root of your project. |
| 27 | + |
| 28 | +```toml {file="wrangler.toml" copy=true} |
| 29 | +# Configure Cloudflare Worker |
| 30 | + |
| 31 | +name = 'hosting-cloudflare-worker' |
| 32 | +compatibility_date = '2025-07-31' |
| 33 | + |
| 34 | +[build] |
| 35 | +command = './build.sh' |
| 36 | + |
| 37 | +[assets] |
| 38 | +directory = './public' |
| 39 | +not_found_handling = '404-page' |
| 40 | +``` |
| 41 | + |
| 42 | +### Step 2 |
| 43 | + |
| 44 | +Create a `build.sh` file in the root of your project. |
| 45 | + |
| 46 | +```sh {file="build.sh" copy=true} |
| 47 | +#!/usr/bin/env bash |
| 48 | + |
| 49 | +#------------------------------------------------------------------------------ |
| 50 | +# @file |
| 51 | +# Builds a Hugo site hosted on a Cloudflare Worker. |
| 52 | +# |
| 53 | +# The Cloudflare Worker automatically installs Node.js dependencies. |
| 54 | +#------------------------------------------------------------------------------ |
| 55 | + |
| 56 | +main() { |
| 57 | + |
| 58 | + DART_SASS_VERSION=1.90.0 |
| 59 | + GO_VERSION=1.24.5 |
| 60 | + HUGO_VERSION=0.148.2 |
| 61 | + NODE_VERSION=22.18.0 |
| 62 | + |
| 63 | + export TZ=Europe/Oslo |
| 64 | + |
| 65 | + # Install Dart Sass |
| 66 | + echo "Installing Dart Sass ${DART_SASS_VERSION}..." |
| 67 | + curl -sLJO "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 68 | + tar -C "${HOME}/.local" -xf "dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 69 | + rm "dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 70 | + export PATH="${HOME}/.local/dart-sass:${PATH}" |
| 71 | + |
| 72 | + # Install Go |
| 73 | + echo "Installing Go ${GO_VERSION}..." |
| 74 | + curl -sLJO "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" |
| 75 | + tar -C "${HOME}/.local" -xf "go${GO_VERSION}.linux-amd64.tar.gz" |
| 76 | + rm "go${GO_VERSION}.linux-amd64.tar.gz" |
| 77 | + export PATH="${HOME}/.local/go/bin:${PATH}" |
| 78 | + |
| 79 | + # Install Hugo |
| 80 | + echo "Installing Hugo ${HUGO_VERSION}..." |
| 81 | + curl -sLJO "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 82 | + mkdir "${HOME}/.local/hugo" |
| 83 | + tar -C "${HOME}/.local/hugo" -xf "hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 84 | + rm "hugo_extended_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 85 | + export PATH="${HOME}/.local/hugo:${PATH}" |
| 86 | + |
| 87 | + # Install Node.js |
| 88 | + echo "Installing Node.js ${NODE_VERSION}..." |
| 89 | + curl -sLJO "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" |
| 90 | + tar -C "${HOME}/.local" -xf "node-v${NODE_VERSION}-linux-x64.tar.xz" |
| 91 | + rm "node-v${NODE_VERSION}-linux-x64.tar.xz" |
| 92 | + export PATH="${HOME}/.local/node-v${NODE_VERSION}-linux-x64/bin:${PATH}" |
| 93 | + |
| 94 | + # Verify installations |
| 95 | + echo "Verifying installations..." |
| 96 | + echo Dart Sass: "$(sass --version)" |
| 97 | + echo Go: "$(go version)" |
| 98 | + echo Hugo: "$(hugo version)" |
| 99 | + echo Node.js: "$(node --version)" |
| 100 | + |
| 101 | + # Configure Git |
| 102 | + echo "Configuring Git..." |
| 103 | + git config core.quotepath false |
| 104 | + if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then |
| 105 | + git fetch --unshallow |
| 106 | + fi |
| 107 | + |
| 108 | + # Build the site |
| 109 | + echo "Building the site..." |
| 110 | + hugo --gc --minify |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +set -euo pipefail |
| 115 | +main "$@" |
| 116 | +``` |
| 117 | + |
| 118 | +### Step 3 |
| 119 | + |
| 120 | +Commit the changes to your local Git repository and push to your GitHub repository. |
| 121 | + |
| 122 | +### Step 4 |
| 123 | + |
| 124 | +In the upper right corner of the Cloudflare [dashboard](https://dash.cloudflare.com/), press the **Add** button and select "Workers" from the drop down menu. |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +### Step 5 |
| 129 | + |
| 130 | +On the "Workers" tab, press the **Get started** button to the right of the "Import a repository" item. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +### Step 6 |
| 135 | + |
| 136 | +Connect to GitHub. |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +### Step 7 |
| 141 | + |
| 142 | +Select the GitHub account where you want to install the Cloudflare Workers and Pages application. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +### Step 8 |
| 147 | + |
| 148 | +Authorize the Cloudflare Workers and Pages application to access all repositories or only select repositories, then press the **Install & Authorize** button. |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +Your browser will be redirected to the Cloudflare dashboard. |
| 153 | + |
| 154 | +### Step 9 |
| 155 | + |
| 156 | +On the "Workers" tab, press the **Get started** button to the right of the "Import a repository" item. |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +### Step 10 |
| 161 | + |
| 162 | +Select the repository to import. |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | +### Step 11 |
| 167 | + |
| 168 | +On the "Set up your application" screen, provide a project name, leave the build command blank, then press the **Create and deploy" button. |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +### Step 12 |
| 173 | + |
| 174 | +Wait for the site to build and deploy, then visit your site. |
| 175 | + |
| 176 | + |
0 commit comments