github-actions特殊用法
# 由来
Github Actions是github被微软收购以后推出的一项功能强大的便利服务,入门教程可以看看阮一峰的教程。 本文主要列一些官方的文档做备份,比如多个actions怎么顺序执行,怎么互相调用
# 入门教程
# 官方文档
# 最简单的工作流
文章将以这个简单的工作流不断添加内容
name: quickStart
on: push
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
2
3
4
5
6
7
8
9
10
11
12
13
14
# 同一个yml中顺序执行
name: quickStart
on: push
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
job2:
name: '示例2'
nedds: job1
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
job3:
name: '示例3'
nedds: [job1,job2]
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
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
上面的示例中可以看到有个“nedds”字段,这个字段表示执行顺序的依赖管理。
job2依赖job1执行成功,job3依赖job1和job3全部执行成功。
所以job的执行顺序为:job1 -> job2 -> job3。
但是这样也有一个问题,只有job1执行成功才能执行job2,如果执行失败也继续执行job2,就需要下面的写法。
name: quickStart
on: push
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
job2:
name: '示例2'
nedds: job1
if: ${{ always() }} # 注意这里的用法
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
job3:
name: '示例3'
nedds: [job1,job2]
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
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
# 多个yml顺序执行
文件1.yml
name: quickStart-1
on: push
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
2
3
4
5
6
7
8
9
10
11
12
13
14
文件2.yml
name: quickStart-2
on: push
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
2
3
4
5
6
7
8
9
10
11
12
13
14
此时我需要在文件1.yml的工作流执行完成或者失败以后执行文件2.yml怎么办? 这里可以采用on搭配workflow_run。
When using the workflow_run event, you can specify what branches the triggering workflow must run on in order to trigger your workflow.
The branches and branches-ignore filters accept glob patterns that use characters like *, **, +, ?, ! and others to match more than one branch name. If a name contains any of these characters and you want a literal match, you need to escape each of these special characters with . 有关 glob 模式的更多信息,请参阅“过滤器模式备忘清单”。
For example, a workflow with the following trigger will only run when the workflow named Build runs on a branch whose name starts with releases/:
文件1.yml
name: quickStart-1
on: push
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
steps:
- name: '输出版本'
run: |
echo $VERSION
2
3
4
5
6
7
8
9
10
11
12
13
14
文件2.yml
name: quickStart-2
on:
workflow_run:
workflows: quickStart-1 # 需要监听的流程名字
types: completed
env:
VERSION: 20220304
jobs:
job1:
name: '示例'
runs-on: ubuntu-latest
# 只有指定的工作流成功运行,本工作流才能被执行
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: '输出版本'
run: |
echo $VERSION
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19