Github Actions E03触发

前言

知道手机能够打电话,还得知道怎么把电话号码拨出去,这样才能实现通话的功能;而CICD的场景会更为复杂,如果一个任务在你不想让它运行的时候运行,会浪费自己资源,影响产出物。

关于触发工作流-workflow,github actions的原文如下

Events that trigger workflows

You can configure your workflows to run when specific activity on GitHub happens, at a scheduled time, or when an event outside of GitHub occurs.


根据上面描述,大致有3类事件(错了的话欢迎指正),官网文档更详细的在这里,不是在那里

最好看英文的,中文似乎没有翻译完,少了一个 Triggering new workflows using a personal access token 方法 😢

一、Github的特定事件 (Specific activity on GitHub happens)

算是最常见的一种方式,比如:当小明更新了master分支代码,推送到仓库后触发这个CICD流程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 单一事件 Trigger on push
on: push
#------------------分割线----------------
# 一个事件列表也可以,Trigger the workflow on push or pull request
on: [push, pull_request]
#------------------分割线----------------
# 不同类型的事件的组合
on:
# Trigger the workflow on push or pull request,
# but only for the master branch
push:
branches:
- master
pull_request:
branches:
- master
# Also trigger on page_build, as well as release created events
page_build:
release:
types: # This configuration does not affect the page_build event above
- created

Github 的 webhooks event有很多,每个event又包含很多类型。查看所有的event

顺带熟悉下 github webhook 的概念

官方介绍:Webhooks provide a way for notifications to be delivered to an external web server whenever certain actions occur on a repository or organization.原文点这里

通俗版:可以将 github Webhook 理解为一个勤勤恳恳的“卧底”,它被安插到github的一个仓库或某个组织中,一旦发生了之前定义好的事情,它会第一时间把以某种约定好的方式告知给 github acionts。
简单(不严谨地)而言: 当有行为触发时,Github 会发送 POST 请求至指定的 URL。

请看🌰

1
2
3
on:
issue_comment:
types: [created, deleted]

默认情况下,一个事件的所有活动类型都会触发工作流程运行。也可以使用types关键字将工作流程运行限制为特定的活动类型,精确使用。

延伸一下: github webhook -> 广义的webhooks、 github哪些events

二、定时事件(Scheduled events)

类似crontab的用法,使用POSIX cron 语法在指定的时间(UTC 时间)运行。

在workflow中用 date命令也可以看到输出的是UTC时间,如:Thu Mar 19 12:08:10 UTC 2020。使用中要根据自己的时区计算出UTC时间,然后配置上。

1
2
3
4
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '*/15 * * * *'

注: GitHub 操作 不支持非标准语法 @yearly、@monthly、@weekly、@daily、@hourly 和 @reboot。这样的语法,如 @midnight 在jenkins中是支持的,需要区别开来。

三、外部事件(External events)

  • 通过Github API 触发叫做 repository_dispatch 的事件,以此来触发workflow.
1
on: repository_dispatch
  • 使用个人访问令牌触发新工作流程(Triggering new workflows using a personal access token)

原文引用:
For example, if a workflow run pushes code using the repository’s GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.

If you would like to trigger a workflow from a workflow run, you can trigger the event using a personal access token. You’ll need to create a personal access token and store it as a secret in your repository. To minimize your GitHub Actions usage costs, ensure that you don’t create recursive or unintended workflow runs.

上面提及的2中方法,都属于用外部方法来触发 workflow。 而上一段英文也提及 workflow 触发workflow的情况,有需要可详细研究。


More Ref:

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~