Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

GithubActions

2021/11/08 小白文
Word count: 3,708 | Reading time: 16min

GitHub Actions 是 GitHub 的持续集成服务,于2018年10月推出

GitHub Actions 是什么?

大家知道,持续集成由很多操作组成,比如抓取代码、运行测试、登录远程服务器,发布到第三方服务等等。GitHub 把这些操作就称为 actions。

很多操作在不同项目里面是类似的,完全可以共享。GitHub 注意到了这一点,想出了一个很妙的点子,允许开发者把每个操作写成独立的脚本文件,存放到代码仓库,使得其他开发者可以引用。

如果你需要某个 action,不必自己写复杂的脚本,直接引用他人写好的 action 即可,整个持续集成过程,就变成了一个 actions 的组合。这就是 GitHub Actions 最特别的地方。

GitHub 做了一个官方市场,可以搜索到他人提交的 actions。另外,还有一个 awesome actions 的仓库,也可以找到不少 action。

基础名词概念

GitHub Actions 有一些自己的术语。
(1)workflow (工作流程):持续集成一次运行的过程,就是一个 workflow。
(2)job (任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。
(3)step(步骤):每个 job 由多个 step 构成,一步步完成。每个 step可以定义自己的 nameid,通过 uses 可以声明使用一个具体的 action,通过run 声明需要执行哪些指令。
(4)action (动作):每个 step 可以依次执行一个或多个命令(action)。

GitHub Actions阮一峰

具体内容说明:

  • name自动构建的名字

  • on触发条件

    • watch监视

      • type:started 类型:点击了星标
    • 也可以是数组: [push, pull_request]

    • 1
      2
      3
      4
      5
      6
      push: # push触发
      branches: [ main ]
      workflow_dispatch: # 手动触发
      schedule: # 计划任务触发
      #- cron: '*/30 20-23,0-15 * * *' # cron表达式,Actions时区是UTC时间,所以要往前推8个小时(4-23)
      - cron: '0 */1 * * *'
  • env环境变量

  • jobs任务

  • build工作的id

  • run-on工作运行的环境平台:可用的虚拟机类型如下:

    1
    2
    3
    ubuntu-latest,ubuntu-18.04 或 ubuntu-16.04
    windows-latest,windows-2019 或 windows-2016
    macOS-latest 或 macOS-10.14
  • if工作运行的判断

  • steps 包含一系列任务步骤

    • name:子任务名
    • uses :使用官方的一些action库完成一些操作
    • run: 运行脚本
    • id: 运行step的id——用来设置环境变量所用:jobs.<job_id>.steps.envsteps.<step id>.outputs,再如:run: echo "The time was $",此处的hello就是上一步id为hello的step

    注:每个step后的路径是不相关的,定义每个step时默认的工作目录都是根目录

  • strategy:GitHub Actions 会将 matrix 中的每个参数排列组合,产生一个新的运行实例。

workflow 文件

GitHub Actions 的配置文件叫做 workflow 文件,存放在代码仓库的.github/workflows目录。

workflow 文件采用 YAML 格式,文件名可以任意取,但是后缀名统一为.yml,比如foo.yml。一个库可以有多个 workflow 文件。GitHub 只要发现.github/workflows目录里面有.yml文件,就会自动运行该文件。

综上, Github actions配合GithubPages会非常方便

什么是 GitHub Pages?

打开 GitHub Pages 的首页我们会看到如下图所示:
他可以当做你或者你的项目的 Websites,那么我们可以知道 GitHub Pages 有两种最基本的用法:

  1. 作为你自己(或者组织)的网站(访问地址示例:http://username.github.io
  2. 作为你某一个项目的网站(访问地址示例:http://username.github.io/projectname
  3. 由于性能不太好, 因此最主要的用途是作为部署静态网页, 给demo展示: 做 demo 展示不同于做项目开发, 我们需要的是快速轻便的开发和部署, 而不是完备的一整套开发流程.

Github pages 的好处

  1. 使用零成本: github pages 集成在 github 中, 直接和代码管理绑定在一起, 随着代码更新自动重新部署, 使用非常方便.
  2. 免费: 免费提供 username.github.io 的域名, 免费的静态网站服务器.
  3. 无数量限制: github pages 没有使用的数量限制, 每一个 github repository 都可以部署为一个静态网站.

workflowDemo

官方demo

默认的生成模板: 会在仓库的./github/workflows目录下创建一个.yml的文件

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
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the main branch
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"——任务名
build:
# The type of runner that the job will run on
# To set the operating system for your job, specify the operating system using runs-on: ubuntu-latest, ubuntu-18.04, or ubuntu-16.04、windows-latest or windows-2019、macos-latest or macos-10.15
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
# 检出, 将代码从代码库中拉下来, 然后复制到本地的虚拟机上
- uses: actions/checkout@v2

# Runs a single command using the runners shell
# 给操作——run, 起个操作名——name
- name: Run a one-line script
run: echo Hello, world!

# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.

BiliBiliTool的workflow

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
name: bilibili-daily-task


on:

push: # push触发
branches: [ main ]
workflow_dispatch: # 手动触发
schedule: # 计划任务触发
- cron: '10 6 * * *' # cron表达式,Actions时区是UTC时间,所以要往前推8个小时,如上表示每天14点10分


jobs:
run-bilibili-tool:

runs-on: ubuntu-latest

steps:

# 检出
- name: Checkout
uses: actions/checkout@v2

# 设置服务器时区为东八区
- name: Set time zone
run: sudo timedatectl set-timezone 'Asia/Shanghai'

# .Net Core 环境
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301

# 安装依赖
- name: Install dependencies
run: dotnet restore

# 构建应用
- name: Build
run: dotnet build --configuration Release --no-restore

# 运行
- name: Run
# env是从仓库settings的secrets中取值
env:
userId: ${{secrets.USERID}}
sessData: ${{secrets.SESSDATA}}
biliJct: ${{secrets.BILIJCT}}
otherConfigs: ${{secrets.OTHERCONFIGS}}
run: dotnet run -p ./src/Ray.BiliBiliTool.Console -userId=${userId} -sessData=${sessData} -biliJct=${biliJct} -closeConsoleWhenEnd=1 ${otherConfigs}

联通Workflow

https://github.com/simo8102/88-AutoSignMachine/blob/main/.github/workflows/main.yml#L11

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  
name: 联通挂机任务积分脚本


on:
push: # push触发
branches: [ main ]
workflow_dispatch: # 手动触发
schedule: # 计划任务触发
#- cron: '*/30 20-23,0-15 * * *' # cron表达式,Actions时区是UTC时间,所以要往前推8个小时(4-23)
- cron: '0 */1 * * *'


jobs:
daily-task:
if: github.event.repository.owner.id == github.event.sender.id
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12]
node-registry: ['https://registry.npmjs.org']

steps:
# 检出
- name: Checkout
uses: actions/checkout@v2

# 设置服务器时区为东八区
- name: Set time zone
run: sudo timedatectl set-timezone 'Asia/Shanghai'

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
registry-url: ${{ matrix.node-registry }}

- name: Get npm cache directory
id: npm-cache
run: |
echo "::set-output name=dir::$(npm config get cache)"
- name: restore npm cache
uses: actions/cache@v2
id: use-npm-cache
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

- name: restore npm dependencies
uses: actions/cache@v2
id: use-npm-depends
with:
path: |
node_modules
*/*/node_modules
key: ${{ runner.os }}-depends-${{ hashFiles('**/package-lock.json') }}

- name: restore task cache
uses: actions/cache@v2
id: use-task-cache
with:
path: |
/home/runner/.AutoSignMachine
key: ${{ runner.os }}-task-${{ hashFiles('**/package-lock.json') }}

- name: Install dependencies
if: steps.use-npm-cache.outputs.cache-hit != 'true' || steps.use-npm-depends.outputs.cache-hit != 'true'
run: npm install

# 运行 unicom 签到任务
- name: Run unicom daily task

env:
user: ${{secrets.unicom_user}}
password: ${{secrets.unicom_password}}
appid: ${{secrets.unicom_appid}}
notify_sctkey: ${{secrets.NOTIFY_SCKEY}}
run: node index.js unicom --user=${user} --password=${password} --appid=${appid}
  • 压缩打包的action:archive-action
  • 复制文件到另一个仓库:copy_file_to_another_repo_action

附录:

好用的Github Action库

构建暂存区

  • 上传文件到Github Action工作区

  • 根据上传的tag将工作区文件发布成release:softprops/action-gh-release

    在网页端上传release的时候,也会有tag选择,tag被用来指明和区分软件开发中的关键时期,比如版本号更新的时候可以创建tag来区分

    • 创建本地tag:git tag -a v2.0 -m "msg"
    • 上传tag: git push origin v2.0
    • 删除本地tag:git tag -d v2.0
    • 删除远程tag:git push origin :refs/tags/v2.0

    这个动作的典型用法是在被推送到git标签上的Commit的构建过程中添加一个步骤,该步骤将会把指定的文件发布成release

    • 某个commit想要发布时,需要打tag,整体操作如下:

      1
      2
      3
      4
      5
      6
      7
      8
      # 提交、标记和推送操作到 GitHub
      # 从您的终端,提交 action.yml、entrypoint.sh、Dockerfile 和 README.md 文件。
      # 最佳做法是同时为操作版本添加版本标记。 有关对操作进行版本管理的详细信息,请参阅“关于操作”。 https://docs.github.com/cn/actions/creating-actions/creating-a-docker-container-action
      Shell
      git add action.yml entrypoint.sh Dockerfile README.md
      git commit -m "My first action is ready"
      git tag -a -m "My first action release" v1
      git push --follow-tags # 等价于git push 与 git push origin v1 合体

上述两者可以配合使用,即在作业之间共享数据,所谓的工作区是Github在workflow中提供给构建存储的空间:在作业之间共享数据

工作流程命令

您可以在Github Action中工作流程或操作代码中运行 shell 命令时,使用工作流程命令。

关于工作流程命令:操作可以与运行器机器进行通信,以设置环境变量,其他操作使用的输出值,将调试消息添加到输出日志和其他任务。大多数工作流程命令使用特定格式的 echo 命令,而其他工作流程则通过写入文件被调用。 更多信息请参阅“环境文件”。

设置输出参数

::set-output name={name}::{value}设置操作的输出参数,如echo "::set-output name=action_fruit::strawberry"

具体的例子:脚本需要获取当前时间并将其设置为(作业中稍后运行的操作可以使用的输出变量)。 为便于 GitHub 识别输出变量, 您必须以特定语法使用工作流程命令: echo "::set-output name=<output name>::<value>"

在作业之间共享数据

1
2
3
4
5
6
7
8
9
10
11
12
13
# 例如,您可以创建一个文件,然后将其作为构件上传。
jobs:
example-job:
name: Save output
steps:
- shell: bash
run: |
expr 1 + 1 > output.log
- name: Upload output file
uses: actions/upload-artifact@v2
with:
name: output-log-file
path: output.log
1
2
3
4
5
6
7
8
# 要从单独的工作流程运行下载构件,您可以使用 `actions/download-artifact` 操作。 例如,您可以下载名为 `output-log-file` 的构件。
jobs:
example-job:
steps:
- name: Download a single artifact
uses: actions/download-artifact@v2
with:
name: output-log-file

10 个你该了解的 GitHub Actions 进阶技巧

术语介绍

  • action : workflow最小执行单元
  • Artifact : workflow运行,产生的中间文件,包括日志、测试结果等
  • Continuous integration (CI):自动构建、测试
  • Continuous deployment (CD): 自动部署
  • Event: 触发workflow
  • GitHub-hosted runner:githut平台的虚拟机
  • Job: workflow的分解,可串行存在依赖;可并行
  • Runner: 运行环境
  • Self-hosted runner: 开发者自己的运新环境
  • Step: job的分解

Github Action 触发工作流程

以下步骤将触发工作流程运行:

  1. 仓库中发生事件,生成的事件具有关联的提交 SHA 和 Git ref。

  2. 在仓库的 .github/workflow 目录中关联的提交 SHA 或 Git ref 处搜索工作流程文件。 工作流程文件必须存在于该提交 SHA 或 Git ref 中才会被考虑。

    例如,如果事件发生在特定仓库分支上,则工作流程文件必须存在于该分支的仓库中。

  3. 检查该提交 SHA 和 Git ref 的工作流程文件, 并且对其 on: 值与触发事件匹配的任何工作流程触发新的工作流程。

    工作流程在触发事件的相同提交 SHA 和 Git ref 上的仓库代码中运行。 当工作流程运行时,GitHub 会在运行器环境中设置 GITHUB_SHA(提交 SHA)和 GITHUB_REF(Git 引用)环境变量。 更多信息请参阅“使用环境变量”。

采坑记录

  • 每个step后的路径是不相关的,定义每个step时默认的工作目录都是根目录

  • pyinstaller打包的程序不能跨平台执行,如果需要同时生成linux和windows的可以yml上加上strategy:matrix(GitHub Actions 会将 matrix 中的每个参数排列组合,产生一个新的运行实例。),大佬示例:build.yml

  • 有跨平台的打包action:archive-action

  • entrypoint.sh需要执行权限

  • 1
    - `if: github.event.repository.owner.id == github.event.sender.id`,仓库所有者ID == 事件触发者ID,`if: ${{ github.event_name == 'workflow_dispatch' && !github.event.repository.private}}`检查是否为手动触发、仓库是否是私有的

Author: Mrli

Link: https://nymrli.top/2020/11/12/GithubAction/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
JupyterHub搭建
NextPost >
南邮校园网CSDN等部分网站图片无法加载解决方案
CATALOG
  1. 1. GitHub Actions 是什么?
  2. 2. 基础名词概念
  3. 3. workflow 文件
  4. 4. workflowDemo
    1. 4.1. 官方demo
    2. 4.2. BiliBiliTool的workflow
    3. 4.3. 联通Workflow
  5. 5. 附录:
    1. 5.1. 好用的Github Action库
      1. 5.1.1. 构建暂存区
    2. 5.2. 工作流程命令
    3. 5.3. 在作业之间共享数据
    4. 5.4. 10 个你该了解的 GitHub Actions 进阶技巧
    5. 5.5. 术语介绍
    6. 5.6. Github Action 触发工作流程
  6. 6. 采坑记录