抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Github Actions 是 Github 在 2018 年 10 月推出的持续集成服务

准备工作

  • 在 Github 上分别新建用于存放 hexo 源文件和部署 gh-pages 的仓库
  • 为了更方便地更新主题,可以将主题添加为子模块
  • 申请 Token 或者将私钥添加到仓库的 Secrets
  • 熟悉 Github Actions 基本操作

Github Actions 基本用法

本文仅列举一些 Github Actions 的基本用法,若想了解更高阶的用法可以看官方文档传送门

基本概念

  1. workflow:持续集成一次运行的过程,就是一个 workflow

  2. job:一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个 jobs

  3. step:每个 job 由多个 step 构成,一步步完成

  4. action:每个 step 可以依次执行一个或多个 action

Workflow 文件结构

Workflow 文件是 Github Actions 的配置文件,位于仓库的 .github/workflows 目录中

Workflow 文件的格式为 YAML 格式,文件后缀名为 yml

Workflow文件的配置字段
name
1
name: learn-github-actions

指定将出现在 GitHub 仓库的 Actions 选项卡中的工作流程名称

如果省略该字段,默认为当前 workflow 的文件名

on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
on: push

on: [push, fork]

on:
push:
# Sequence of patterns matched against refs/heads
branches:
- main
- 'mona/octocat'
- 'releases/**'
# Sequence of patterns matched against refs/tags
tags:
- v2
- v1.*
label:
types:
- created
pull_request:
branches:
- 'releases/**'
- '!releases/**-alpha'
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 5,17 * * *'
watch:
types: [started]
workflow_dispatch:

指定了触发 Workflow 的条件

  • push:推送到特定的分支或标签时触发
  • pull_request:在创建特定 pr 时
  • schedule:定时执行,使用 cron 表达式确定时间(UTC)
  • watch:发生特殊事件时触发,如仓库被 star
  • workflow_dispatch:允许在 Actions 界面被手动触发
jobs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2
with:
ref: main
submodules: true

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14.x'
job1:
runs-on: ubunu-latest
needs: build
if: ${{ always() }}
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
steps:
- name: blablabla
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
  • <job_id>:为 job 命名(必须以字母或数字开头,并且只能包含字母数字字符)
  • run-on:指定该 job 的工作环境,如 windows-latest ubuntu-latest macos-latest
  • needs:在执行该 job 时需要完成的(如不指定默认情况下 job 为并行关系)
  • if:在 ${{ }} 表达式写条件,Github 会自动为表达式求值
  • env:为环境变量赋值
  • steps:描述 job 的具体执行流程
  • steps.name:为 step 命名
  • steps.uses:可以使用他人已经写好的代码库 {owner}/{repo}@{ref}
  • steps.run:运行指定命令,可用 | 输入多行命令
  • steps.uses:可以输入参数作为环境变量来使用

自动部署实例

自动更新 Readme Stats

.github/workflows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
name: Auto Deploy
on:
schedule:
- cron: '0 10,22 * * *' # 北京时间上午和下午的的6点自动执行
# every 12 hours: 0 */12 * * *
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
profile-readme:
# https://github.com/marketplace/actions/profile-readme
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Create README.md
uses: actions-js/profile-readme@master
with:
username: zryyyy
github_token: ${{ secrets.TOKEN }}
- name: Commit & Push changes
uses: actions-js/push@master
with:
github_token: ${{ secrets.TOKEN }}
profile-readme-development-stats:
# https://github.com/marketplace/actions/profile-readme-development-stats
needs: profile-readme
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Profile Readme Development Stats
uses: anmol098/waka-readme-stats@master
with:
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
GH_TOKEN: ${{ secrets.TOKEN }}
SHOW_PROFILE_VIEWS: "False" # 项目浏览量(标签)
SHOW_UPDATED_DATE: "False" # 最后一次更新的时间
SHOW_LINES_OF_CODE: "False" # 代码行数(标签)
SHOW_OS: "True"
SHOW_PROJECTS: "True"
SHOW_LOC_CHART: "False" # 在图表中区分不同年份
SHOW_EDITORS: "True"
SHOW_TIMEZONE: "True"
SHOW_COMMIT: "True"
SHOW_LANGUAGE: "True"
SHOW_LANGUAGE_PER_REPO: "True"
SHOW_DAYS_OF_WEEK: "True"
SHOW_SHORT_INFO: "False" # 短个人信息
LOCALE: "en"
COMMIT_BY_ME: "False" # 用自己的邮箱提交
COMMIT_MESSAGE: "Updated with Dev Metrics" # 提交摘要
SHOW_TOTAL_CODE_TIME: "False" # 写代码总时间(标签)

自动生成并部署 Hexo

.github/workflows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
name: Auto Deploy Main
on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Source
uses: actions/checkout@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js
uses: actions/setup-node@main
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: "24"
- name: Change Config
run: |
sed -i 's/hexo-test/zryyyy.github.io/g' _config.yml
- name: Setup Deploy Key
env:
DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "1931860504@qq.com"
git config --global user.name "zryyyy"
- name: Install Dependencies
run: |
npm install
- name: Generate & Deploy
run: |
npm run clean
npm run generate
npm run deploy
delete-workflow:
runs-on: ubuntu-latest
steps:
- name: Delete workflow runs
uses: Mattraks/delete-workflow-runs@main
with:
# token: ${{ github.token }}
token: ${{ secrets.DELETE_WORKFLOW_TOKEN }}
repository: ${{ github.repository }}
retain_days: 7 # keep runs for 7 days
keep_minimum_runs: 3 # keep at least 3 runs



Vi Veri Veniversum Vivus Vici