Deploying Static Sites using Cloudflare Pages
23 Apr 2024A few weeks ago, after switching to Hugo for my site, I was trying to create a service file to deploy the site even on a server restart. For the original Go server I created, I wrote a simple service and socket file to deploy the site:
# /etc/systemd/system/website.service
[Unit]
Description=website
Requires=network.target
After=multi-user.target
[Service]
Type=simple
WorkingDirectory=/home/<user>/website
ExecStart=/home/<user>/website/<server_executable>
User=root
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/website.socket
[Socket]
ListenStream=localhost:3000
[Install]
WantedBy=sockets.target
In my mind, it was as simple as replacing the path for the WorkingDirectory
and using hugo server
for ExecStart
. However, I ran into errors that I never encountered when trying to deploy the Go server. When troubleshooting these errors, I stumbled upon Cloudflare Pages. Cloudflare Pages allows you to deploy web apps to the Cloudflare network and supports many web frameworks including Hugo. Using Cloudflare Pages to deploy came with a huge benefit: the site is now serverless and will never be down (so long as the Cloudflare network remains online). Cloudflare pulls from the code that is hosted on GitHub. Using the code from GitHub allows me to push changes to the repo and see them update in (almost) realtime, giving me continuous integration and deployment.
Cloudflare Pages Build Log:
2024-04-18T21:37:53.30375Z Cloning repository...
...
2024-04-18T21:37:54.481735Z Success: Finished cloning repository files
...
2024-04-18T21:37:56.481275Z Executing user command: hugo
...
2024-04-18T21:37:56.783674Z | EN
2024-04-18T21:37:56.783785Z -------------------+-----
2024-04-18T21:37:56.783926Z Pages | 9
2024-04-18T21:37:56.784037Z Paginator pages | 0
2024-04-18T21:37:56.784143Z Non-page files | 0
2024-04-18T21:37:56.784237Z Static files | 6
2024-04-18T21:37:56.784335Z Processed images | 0
2024-04-18T21:37:56.784431Z Aliases | 0
2024-04-18T21:37:56.78457Z Sitemaps | 1
2024-04-18T21:37:56.784668Z Cleaned | 0
2024-04-18T21:37:56.784824Z
2024-04-18T21:37:56.784927Z Total in 36 ms
2024-04-18T21:37:56.797559Z Finished
2024-04-18T21:37:56.798018Z Note: No functions dir at /functions found. Skipping.
2024-04-18T21:37:56.798129Z Validating asset output directory
2024-04-18T21:37:57.416252Z Deploying your site to Cloudflare's global network...
2024-04-18T21:37:59.573849Z Uploading... (16/16)
2024-04-18T21:37:59.574676Z ✨ Success! Uploaded 0 files (16 already uploaded) (0.50 sec)
2024-04-18T21:37:59.574874Z
2024-04-18T21:38:00.025079Z ✨ Upload complete!
2024-04-18T21:38:02.373655Z Success: Assets published!
2024-04-18T21:38:03.639944Z Success: Your site was deployed!