GitHub Actions 快速入门与进阶技巧
ericwxy

GitHub Actions 是一个强大的自动化工具,它允许你在 GitHub 上直接构建、测试和部署你的项目。通过编写自定义的工作流程,你可以自动化各种任务,从而提高开发效率和代码质量。本文将为你提供一个快速入门指南,并分享一些进阶技巧,帮助你更好地利用 GitHub Actions。

GitHub Actions 简介

GitHub Actions 基于事件驱动,你可以定义特定的触发事件(如 push、pull request、schedule 等)来启动一个工作流程(workflow)。每个工作流程由一个或多个作业(job)组成,作业可以运行在不同的操作系统上,并且可以并行或顺序执行。

基本操作流程

  1. 创建工作流程文件:在项目的 .github/workflows 目录下创建一个 .yml 文件。
  2. 定义触发器:在文件中使用 on 关键字定义何时触发工作流程。
  3. 编写作业:使用 jobs 关键字定义一个或多个作业,每个作业包含一系列的步骤(steps)。
  4. 执行步骤:在步骤中,你可以使用 run 来执行命令,或者使用 uses 来引用其他 Actions。

快速入门案例

以下是一个简单的工作流程示例,它在每次推送到主分支时运行一个简单的 shell 脚本。

1
2
3
4
5
6
7
8
9
10
11
12
# .github/workflows/hello-world.yml

name: Hello World

on: push

jobs:
greet:
runs-on: ubuntu-latest
steps:
- name: Greet
run: echo "Hello, World!"

进阶技巧

1. 传入参数

在执行 workflow 时,允许传入参数,以控制执行逻辑。

1
2
3
4
5
6
7
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'info'

案例:在持续部署时,你可能需要根据不同的日志级别来执行不同的部署策略。

.eg

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
name: Deploy with Custom LogLevel

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level for deployment'
required: true
default: 'info'

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Echo LogLevel
run: echo "Deploying with log level: ${{ github.event.inputs.logLevel }}"
- name: Deploy Application
run: |
# Deployment commands based on logLevel
if [ "${{ github.event.inputs.logLevel }}" == "debug" ]; then
echo "Running debug deployment..."
# Debug deployment commands
elif [ "${{ github.event.inputs.logLevel }}" == "info" ]; then
echo "Running info deployment..."
# Info deployment commands
else
echo "Running warning deployment..."
# Warning deployment commands
fi

2. 控制执行顺序

使用 needs 参数管理 job 之间的依赖。

1
2
3
4
jobs:
job1:
needs: []
# ...

案例:你可能需要在代码推送后,先运行单元测试,然后运行集成测试,最后部署。

.eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
name: Test and Deploy

on: push

jobs:
unit_tests:
runs-on: ubuntu-latest
steps:
- name: Run Unit Tests
run: echo "Running unit tests..."

integration_tests:
needs: unit_tests
runs-on: ubuntu-latest
steps:
- name: Run Integration Tests
run: echo "Running integration tests..."

deploy:
needs: integration_tests
runs-on: ubuntu-latest
steps:
- name: Deploy Application
run: echo "Deploying application..."

3. 项目管理

使用 Actions 实现项目管理,如自动添加标签、管理 Issues 和 PR。

1
2
3
uses: actions/labeler@main
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

案例:自动为修改特定目录的PR添加标签。

.eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
name: Auto Label PR

on:
pull_request:
paths:
- 'docs/**'

jobs:
label_docs_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@main
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

4. 在线调试

使用特定的 Action 进行在线调试。

1
uses: shaowenchen/debugger-action@v2

案例:在Runner上进行调试。

.eg

1
2
3
4
5
6
7
8
9
10
11
12
13
name: Debug Runner

on: workflow_dispatch

jobs:
debug:
runs-on: ubuntu-latest
steps:
- uses: shaowenchen/debugger-action@v2
with:
timeout-minutes: 30
continue-on-error: true
ngrok_token: ${{ secrets.NGROK_TOKEN }}

5. 设置缓存

使用缓存加快构建速度。

1
uses: actions/cache@v2

案例:为Node.js项目设置缓存,以加快依赖安装速度。

.eg

1
2
3
4
5
6
7
8
9
10
11
12
13
name: Node.js Cache

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-modules-

6. 检测问题链接

检测文档中的 Broken 链接。

1
uses: gaurav-nelson/github-action-markdown-link-check@v1

案例:检测Markdown文档中的Broken链接。

.eg

1
2
3
4
5
6
7
8
9
10
11
12
13
name: Check Markdown Links

on: push

jobs:
markdown_link_check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: gaurav-nelson/github-action-markdown-link-check@v1
with:
use-quiet-mode: 'yes'
config-file: '.github/workflows/checklink_config.json'

7. 参数化执行

通过参数化的方式批量执行或编排流程。

1
2
3
4
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [14, 16]

案例:在不同的操作系统和Node.js版本上运行测试。

.eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
name: Test on Multiple OS and Node Versions

on: push

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [14, 16]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test

8. 显示状态

在 README 中显示 workflow 的执行状态。

在GitHub Actions页面,找到你的workflow,点击“Create status badge”,复制提供的Markdown链接,并将其添加到README文件中。

9. 精准 Hook

精准 Hook GitHub 上的行为,如 fork、star 等。

1
2
on:
fork:

案例:当有人fork仓库时,自动发送通知。

.eg

1
2
3
4
5
6
7
8
9
10
11
name: Fork Event Notification

on:
fork:

jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Send Notification
run: echo "Someone forked the repository!"

结语

GitHub Actions 提供了一个强大的自动化平台,无论是简单的构建和测试,还是复杂的项目管理,都能通过编写工作流程来实现。希望本文能帮助你快速上手 GitHub Actions,并利用这些进阶技巧来提升你的工作流程。记得,自动化的旅程永无止境,不断探索和实践,你将发现更多的可能性。

 评论
评论插件加载失败
正在加载评论插件