有没有办法通过提交信息的值触发特定的 Github action 工作流程?

7

有没有办法通过提交信息的值来触发特定的Github action workflow?例如,如果我使用提交信息“smoke_test”推送,那么只有来自一组工作流中的(一个特定工作流程)smoke test工作流会被触发。请帮忙解答,谢谢!


https://docs.github.com/en/actions/using-jobs/using-conditions-to-control-job-execution - Sebastián Palma
https://docs.github.com/en/actions/learn-github-actions/expressions#startswith - Sebastián Palma
https://docs.github.com/en/actions/learn-github-actions/environment-variables#using-contexts-to-access-environment-variable-values - Sebastián Palma
3个回答

12

有的。如果提交说明包含特定关键词,您可以检查触发器事件。

这里是一个示例:

name: Smoke tests
on: push

jobs:
 test:
   runs-on: [ ubuntu-latest ]
   steps:
     - name: Run smoke tests
       if: contains(github.event.head_commit.message, 'smoke_test')
       run: |
         echo 'Running smoke tests!'

这已经足够好了,但实际上它会创建一个工作流运行,然后跳过该步骤。 - Jemshit Iskenderov

5
是的,有一种方法。我没有在此链接中找到官方文档,其中包含所使用脚本语言的CFG描述。
我进行的实验表明,您可以使用if关键字来条件化Job中的所有步骤。请参见下面的示例。
使用条件控制作业执行相比,这更加美观,因为如果所有步骤都没有执行,则在GitHub CI中将构建标记为灰色。
示例:
name: My Build.

# The name for workflow runs generated from the workflow, which will appear in the list of workflow.

run-name: ${{ github.actor }} has triggered Build and Launch Unittests for the project 

# Specify the trigger for this workflow.
on: [push]


jobs:
  Build-my-project-for-Linux:
    runs-on: ubuntu-latest
    if: contains(github.event.head_commit.message, 'please build')
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo " The ${{ github.repository }} repository has been cloned to the runner into " ${{ github.workspace }}
      - run: |
         export CC=/usr/bin/gcc-10
         export CXX=/usr/bin/g++-10
         echo "Etc. for first step"
      - run: |
         echo "Etc. for second step"

附录

官方文档链接

1 https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions

[2] https://docs.github.com/en/actions/using-workflows/about-workflows

GitHub工作流程中使用的CI术语

工作流程 - 是一个可配置的自动化过程,将运行一个或多个作业。

事件 - 是触发工作流程运行的存储库中的特定活动。

运行器 - 是在触发工作流程时运行工作流程的服务器。每个运行器一次只能运行一个作业。GitHub提供Ubuntu Linux、Microsoft Windows和macOS运行器。

作业 - 是在工作流程中执行相同运行器上的一组步骤。

步骤 - 步骤可以是一个将被执行的Shell脚本,也可以是一个将被运行的动作。
动作 - 是GitHub Actions平台上执行复杂但经常重复的任务的自定义应用程序。

2

可以通过使用paths:检查特定文件在提交中是否已更改,而不是检查提交消息内容,来替代在工作流启动后检查提交消息以跳过步骤/作业的方法(参见此处)。这样,您的工作流永远不会开始运行,与其他方法不同:

on:
  push:
    paths:
      - '**trigger_file.txt'

您可以结合这两种方法,使触发器变得更加明确。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接