Github Actions 是 Github 在 2018 年 10 月推出的持续集成服务
准备工作
在 Github 上分别新建用于存放 hexo 源文件和部署 gh-pages 的仓库
为了更方便地更新主题,可以将主题添加为子模块
申请 Token 或者将私钥添加到仓库的 Secrets
熟悉 Github Actions 基本操作
Github Actions 基本用法 本文仅列举了一些 Github Actions 的基本用法,若要了解更高阶的用法可以去看官方文档传送门
基本概念
workflow :持续集成一次运行的过程,就是一个 workflow
job :一个 workflow
由一个或多个 jobs
构成,含义是一次持续集成的运行,可以完成多个 jobs
step :每个 job
由多个 step
构成,一步步完成
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: branches: - main - 'mona/octocat' - 'releases/**' tags: - v2 - v1.* label: types: - created pull_request: branches: - 'releases/**' - '!releases/**-alpha' schedule: - 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 * * *' workflow_dispatch: jobs: 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: 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 54 55 56 name: Auto deploy Hexo on: watch: types: [started ] workflow_dispatch: 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' - name: Setup Hexo env: ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$ACTION_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" mv test.yml _config.yml -b npm ci npm i npm install hexo-cli -g npm install hexo-deployer-git --save npm i hexo-generator-search hexo-generator-json-content npm i hexo-renderer-stylus npm i -S hexo-related-popular-posts npm i --save hexo-wordcount npm i hexo-helper-qrcode npm install hexo-generator-feed --save npm install hexo-generator-sitemap --save - name: Generate & Deploy run: | hexo clean hexo generate hexo deploy