https://evanwang.org/posts/feed.xml

使用Zola部署静态博客

2025-01-31

Install Zola

你可以用你喜欢的包管理器安装zola,因为我使用Arch Linux,所以我在这里使用pacman

pacman -S zola

其他发行版(操作系统)详细的安装指南请移步Zola 官方文档

Configuration Zola

初始化Zola

zola init my_site # 在当前目录下新建”my_site"文件夹,并在该文件夹内初始化
zola init #在当前文件夹init

运行init会有三个问题,默认即可,这些选择都可以在之后的config.toml中配置。

> What is the URL of your site? (https://example.com):
> Do you want to enable Sass compilation? [Y/n]:
> Do you want to enable syntax highlighting? [y/N]:
> Do you want to build a search index of the content? [y/N]:

这是默认的config.toml:

./config.toml
# The URL the site will be built for
base_url = "https://example.com"

# Whether to automatically compile all Sass files in the sass directory
compile_sass = true

# Whether to build a search index to be used later on by a JavaScript library
build_search_index = false

[markdown]
# Whether to do syntax highlighting
# Theme can be customised by setting the `highlight_theme` variable to a theme supported by Zola
highlight_code = false

[extra]
# Put all your custom variables here

构建网页

zola build

运行本地实施更新的服务器

zola serve

安装使用主题

这里我选择使用serene,这是在我浏览zola主题时发现的一款简洁、美观、功能丰富的zola主题。

安装serene主题,实际上就是把主题仓库clone到本地的themes目录下。

git submodule add -b latest https://github.com/isunjn/serene.git themes/serene

serene主题提供了config.toml示例,我们直接使用示例配置替换默认配置并根据注释和主题文档配置。

nv config.toml config.toml.bak
cp themes/serene/config.example.toml config.toml

之后请根据isunjn/serene的README配置主题。

推送到 GitHub1

添加远程存储库

git remote add origin <你的存储库地址>
git add .
git commit -m "init"
git push

部署Cloudflare Pages2

Note

以下内容假定你已经拥有并登录Cloudflare账号,且已将源文件push到远程仓库。

在 Account Home, 选择 Workers & Pages > Create application > Pages > Connect to Git.

选择你放置blog源文件的GitHub存储库, 在Set up builds and deployments 页面, 选择你要使用的branch,这里我们选择main。在Framework preset 选择zola。

Configuration optionValue
Production branchmain
Build commandzola build
Build directorypublic

在下面的Environment variables (advanced) 新建以下变量3

Variable nameValue
ZOLA_VERSION0.19.2
UNSTABLE_PRE_BUILDasdf plugin add zola <https://github.com/salasrod/asdf-zola> && asdf install zola 0.19.2 && asdf global zola 0.19.2

若不添加第二个变量会出现部署错误,value内的版本号可随zola更新调整。写这篇教程时zola的最新版本是0.19.2

点击Save and Deploy,开始你的第一次部署。在部署完成后,你可以在<你的项目名>.pags.dev 查看你的blog。

你可以在config.toml中将base_url 的值指向这个子域名

例如:

# The URL the site will be built for
base_url = "https://my-zola-project.pages.dev"

绑定自定义域名请参考Custom domains · Cloudflare Pages docs,之后将base_url指向你新绑定的域名即可。

部署到Github Pages

TODO

Reference