Disclaimer: I am not affiliated with Cloudflare nor they sponsor me. This article is for sharing information that I find useful.
🧑💻 This post assumes you have intermediate experience with the command line and Git front-ends like GitHub.
When looking for a hosting solution for a static website that has a free tier, there are many options including Github Pages, Neocities, and Codeberg Pages
Cloudflare Pages has a free tier that is pretty permissive, at the time of writing they offer:
- 1 build at the time
- 500 builds per month
- 100 custom domains per project
- Unlimited sites
- Unlimited static requests
- Unlimited bandwidth
💡 You can read about its limits on this site
Let’s start
Photo by Clément Hélardot on Unsplash
First, go ahead and create an account on Cloudflare if you don’t have one.
We are gonna use GitHub, to take advantage of their CI/CD platform.
Let’s create a new repository:
You can name it whatever you want! You are free to choose private or public repo too.
Go ahead and copy the clone URL that is shown on top of the screen.
Open your favorite terminal emulator, and clone it:
🪟 For the Windows operative system, please continue on WSL. If you don’t have it, go ahead and install it and configure it.
Now we have to log in to Cloudflare or create an account by using their command line tool.
For this, you will need Node installed on your computer.
This will show you an URL that you have to copy and paste into your web browser to continue the login, there you can create your Cloudflare account if you don’t have one.
Writing the code
We are going to create an example website to deploy, additionally, we are gonna use SCSS to show off in our automatic deployment.
✏️ Let’s create our starting files:
Edit the index.html
file with the following contents:
💅 Let’s now give it a stylesheet, edit the file style.scss
💡 This code defines a simple personal website that is about a fake person. You can replace the text if you want. The website uses a layout known as “hero” which is a large image at the top of the page. The image is responsive and will adapt to the size of the screen.
Let’s now compile the stylesheet, and run the following command:
You can also use the --watch
option to automatically recompile the stylesheet when you edit the file.
For being able to preview the website, we will use Python’s built-in HTTP server.
You can now open the website at http://localhost:8000
It should look like this:
Taking care of the deployment
🧰 Let’s create a simple script that takes care of building the website.
Edit the file scripts/build.sh
:
For making git ignore any built files, edit the file .gitignore
:
We will commit the changes to git.
Let’s create a new Cloudflare pages project.
Now we only need to tell Cloudflare Pages to run this script when deploying the website.
For this let’s create our GitHub Actions workflow.
Edit the file .github/workflows/build.yml
:
⚠️ Don’t forget to replace
my-website
with the name of your website, this is shown in your Cloudflare Pages dashboard.
Before we can deploy the website, we need to create a secret that contains the API token for Cloudflare Pages.
Go to the Cloudflare API Tokens page and create a new token with the following permissions:
- Account: Cloudflare Pages: Edit
- Account: Account Settings: Read
- User: Member Permissions: Read
- User: User Details: Read
In the section called Account Resources, select the account that you want to deploy to.
Now that you have created the token, go to the Github secrets page:
- Navigate to the repository settings, then to the secrets page and click on
New repository secret
Name it CLOUDFLARE_API_TOKEN
and paste the token you created earlier.
Click the Add secret
button.
Now we will push the code to GitHub and let GitHub Actions do the rest.
Conclusion
Open GitHub Actions to see the workflow running, it should take a few minutes to deploy the website.
💡 To avoid hitting the free build limit, you can make changes to the website in a new branch and then merge it to the main branch when you are done developing those changes.
🎉 Congratulations, you have successfully deployed your website to Cloudflare Pages!