GitHub 页面

在本教程中,我们使用 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.

  1. 创建一个名为 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.

  2. 将 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 main branch 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 .gitignore file contains public/ line. The folder structure should be roughly similar to this repo.

  1. 使用 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)

  2. 在你的 GitHub repo 的设置中,导航到设置 > 页面 > 源。将源更改为 GitHub Actions 并保存。

    ¥In your GitHub repo’s setting, navigate to Settings > Pages > Source. Change the source to GitHub Actions and save.

  3. 在你的存储库中使用以下内容创建 .github/workflows/pages.yml(将 20 替换为你在上一步中记下的 Node.js 主版本):

    ¥Create .github/workflows/pages.yml in your repo with the following contents (substituting 20 to the major version of Node.js that you noted in previous step):

.github/workflows/pages.yml
name: Pages

on:
push:
branches:
- main # default branch

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
# Examples: 20, 18.19, >=16.20.2, lts/Iron, lts/Hydrogen, *, latest, current, node
# Ref: https://github.com/actions/setup-node#supported-version-syntax
node-version: "20"
- name: Cache NPM dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
deploy:
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
  1. 部署完成后,请查看 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:

  1. 导航到 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.

  2. 编辑你的 _config.yml,将 url: 值更改为 https://username.github.io/repository

    ¥Edit your _config.yml, change the url: value to https://username.github.io/repository.

  3. 在你的 GitHub repo 的设置中,导航到设置 > 页面 > 源。将源更改为 GitHub Actions 并保存。

    ¥In your GitHub repo’s setting, navigate to Settings > Pages > Source. Change the source to GitHub Actions and save.

  4. 提交并推送到默认分支。

    ¥Commit and push to the default branch.

  5. 部署完成后,请查看 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.

  1. 安装 hexo-deployer-git

    ¥Install hexo-deployer-git.

  2. 将以下配置添加到 _config.yml,(如果有,请删除现有行)。

    ¥Add the following configurations to _config.yml, (remove existing lines if any).

deploy:
type: git
repo: https://github.com/<username>/<project>
# example, https://github.com/hexojs/hexojs.github.io
branch: gh-pages
  1. 运行 hexo clean && hexo deploy

    ¥Run hexo clean && hexo deploy.

  2. 检查 username.github.io 上的网页。

    ¥Check the webpage at username.github.io.

有用的链接

¥Useful links