A relatively new product from Cloudflare - Cloudflare Pages - allows you to easily deploy static sites. With this new product, Cloudflare joins other popular services, such as GitHub Pages and Netlify. These services allow you to host static sites (no server-side).
The free plan of Cloudflare Pages is more than enough for most users, and allows them, as for this moment, to host unlimited sites and enjoy unlimited bandwidth.
In this guide, we’ll cover the deployment process of a new Hugo website (if you’re new to Hugo, check out How to build a static site with Hugo). However, Cloudflare has organized and detailed documentation that includes relevant guides for other popular static site generators.
Create a project and deploy
- Log in to your Cloudflare account.
- On the Cloudflare homepage, click the “Pages” tab in the left menu.
- Click “Create a project”.
- Select the GitHub repository of your Hugo site. If you don’t have a Git repository for your site, you can read more here about integrating Git with a Hugo project. Cloudflare Pages will integrate directly with the GitHub repository to automatically deploy your site with any changes.
- Fill in the details and configure the deployment process.
mainas your “production branch”.
Hugoas the “Framework preset”.
- Make sure the “Build command” is set to
- Make sure the “Build output directory” is set to
- Important: Click on the “Environment variables” tab under the “Settings” screen. Add a new variable: Set
HUGO_VERSIONas the “Variable name”. Enter the Hugo version that you use for your site as the “Value”, for example:
0.92.0. If you’re not sure which version you’re using, navigate (
cd) to your site folder via the terminal and type
hugo version. Adding this variable tells Cloudflare Pages builder the exact Hugo version to use when building your site. Using the same version as the one that you use locally to build your site will prevent various errors. Before I added this variable I got an error during the deployment process and the deployment failed. Setting the Hugo version variable immediately solved the issue.
- It is also recommended to set another variable:
productionas its value, since there are various features, depending on your theme and site configuration, that will be enabled in the production environment only.
- Click “Save and Deploy”. Cloudflare will start building your site and will show you detailed logs about the deployment process.
- If it all went successfully, you should see a confirmation message after the deployment is done. Your site will be available under a unique subdomain that Cloudflare created for it:
Set up a custom domain (optional)
- Under your site’s project in Cloudflare pages, click the “Custom domains” tab.
- Click the “Set up a custom domain” button.
- Type in your domain and follow the instructions to configure the DNS settings.