在本教程中,我们使用 GitHub Actions 部署 GitHub Pages。它适用于公共和私有存储库。如果你不想将源文件夹上传到 GitHub,请跳到 一键部署部署 部分。
¥In this tutorial, we use GitHub Actions to deploy GitHub Pages. It works in both public and private repositories. Skip to the One-command deployment section if you prefer not to upload your source folder to GitHub.
创建一个名为 username.github.io 的 repo,其中 username 是你在 GitHub 上的用户名。如果你已经上传到另一个 repo,请重命名该 repo。
¥Create a repo named username.github.io, where username is your username on GitHub. If you have already uploaded to another repo, rename the repo instead.
将 Hexo 文件夹的文件推送到存储库的默认分支。默认分支通常为 main,较旧的仓库可能使用 master 分支。
¥Push the files of your Hexo folder to the default branch of your repository. The default branch is usually main, older repositories may use master branch.
要将
main分支推送到 GitHub:¥To push
mainbranch to GitHub:$ git push -u origin main
默认情况下不会(也不应该)上传
public/文件夹,请确保.gitignore文件包含public/行。文件夹结构应该与 此 repo 大致相似。¥The
public/folder is not (and should not be) uploaded by default, make sure the.gitignorefile containspublic/line. The folder structure should be roughly similar to this repo.
使用
node --version检查你在本地机器上使用的 Node.js 版本。记下主要版本(例如v20.y.z)¥Check what version of Node.js you are using on your local machine with
node --version. Make a note of the major version (e.g.,v20.y.z)在你的 GitHub repo 的设置中,导航到设置 > 页面 > 源。将源更改为 GitHub Actions 并保存。
¥In your GitHub repo’s setting, navigate to Settings > Pages > Source. Change the source to GitHub Actions and save.
在你的存储库中使用以下内容创建
.github/workflows/pages.yml(将20替换为你在上一步中记下的 Node.js 主版本):¥Create
.github/workflows/pages.ymlin your repo with the following contents (substituting20to the major version of Node.js that you noted in previous step):
name: Pages |
部署完成后,请查看 username.github.io 上的网页。
¥Once the deployment is finished, check the webpage at username.github.io.
注意 - 如果使用 CNAME 指定自定义域名,则需要将 CNAME 文件添加到 source/ 文件夹。更多信息。
¥Note - if you specify a custom domain name with a CNAME, you need to add the CNAME file to the source/ folder. More info.
项目页面
¥Project page
如果你希望在 GitHub 上有一个项目页面:
¥If you prefer to have a project page on GitHub:
导航到 GitHub 上的存储库。转到设置选项卡。更改存储库名称,以便你的博客可在 username.github.io/repository 上访问,存储库可以是任何名称,如 blog 或 hexo。
¥Navigate to your repo on GitHub. Go to the Settings tab. Change the Repository name so your blog is available at username.github.io/repository, repository can be any name, like blog or hexo.
编辑你的 _config.yml,将
url:值更改为 https://username.github.io/repository。¥Edit your _config.yml, change the
url:value to https://username.github.io/repository.在你的 GitHub repo 的设置中,导航到设置 > 页面 > 源。将源更改为 GitHub Actions 并保存。
¥In your GitHub repo’s setting, navigate to Settings > Pages > Source. Change the source to GitHub Actions and save.
提交并推送到默认分支。
¥Commit and push to the default branch.
部署完成后,请查看 username.github.io/repository 上的网页。
¥Once the deployment is finished, check the webpage at username.github.io/repository.
一键部署部署
¥One-command deployment
以下说明改编自 一命令部署 页面。
¥The following instruction is adapted from one-command deployment page.
-
¥Install hexo-deployer-git.
将以下配置添加到 _config.yml,(如果有,请删除现有行)。
¥Add the following configurations to _config.yml, (remove existing lines if any).
deploy: |
运行
hexo clean && hexo deploy。¥Run
hexo clean && hexo deploy.检查 username.github.io 上的网页。
¥Check the webpage at username.github.io.
有用的链接
¥Useful links