使用GitHub Pages Jekyll工作流配置使用非默认分支

个人知识库

Author: 刘杰文, Date: 2023-06-04 20:20:00 +0800, Categories: GitHubGitHub PagesGitHub Actions, Tags: GitHubGitHub PagesGitHub ActionsGitHub Pages Jekyllbranchesexamples

使用GitHub Pages Jekyll工作流配置使用非默认分支

本文将以我的项目FetchRPlayerdocs_generation分支进行举例说明。

在创建workflow的时候是可以编辑的,这个时候我们对给出的默认配置做出如下修改:

原文:

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

改后:

on:
  # Runs on pushes targeting the default branch
  push:
    branches: 
      - docs_generation

原文:

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

改后:

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with: 
          ref: 'docs_generation'

(添加了with和输入ref键)

做完这些工作,我们还要配置一下环境。本来如果我们在选择部署方式的时候如果选择了“Deploy from a branch”,然后在页面上选择好分支并保存,那么这个环境配置的工作网站就帮我们做了。现在是我们自己配的,那就要我们自己设置了。

Pages一样,都在设置(Settings)中,我们点击左侧Code and automation分区中的Environments,添加对应分支的“准许”规则:

image-20230604203521835

我们看到右上角的Selected branches:

image-20230604203615148

以及上面一行灰色语句:

Limit which branches can deploy to this environment based on rules or naming patterns.

两者很明确地表明,如果我们不设置,按照项目的默认配置我们将不能成功部署网站。后果就像是这样:

我们Rerun部署(deploy)工作(job),同时勾选调试输出,我们会得到这样一行信息:

Branch “docs_generation” is not allowed to deploy to github-pages due to environment protection rules.

这里的environment protection rules很明确地指向了前面提到的环境配置。


参考的链接:

  1. https://github.com/actions/checkout/discussions/277
  2. (未参考但确实有用的链接)https://github.com/withastro/docs/issues/1376