【问题标题】:How do I get the event type that triggered a GitHub action?如何获取触发 GitHub 操作的事件类型?
【发布时间】:2021-08-05 09:19:55
【问题描述】:

我正在用 JavaScript 编写自定义操作。

在我的 GitHub 操作中,我想根据触发它的事件类型做出决定。例如,它是推送作业还是 cron 作业。

如何使用 GitHub Actions Toolkit 访问这些信息?

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    问题的标题和问题的内容相互矛盾。您在标题中提到了事件 type,但问题询问了其他内容:

    是推送还是定时任务

    这不是事件类型,这是事件名称name 可以通过github.event_name 变量访问,type 可以从github.event.action 读取。 (注意下划线和点的区别。)

    是的,命名相当混乱。在工作流 yaml 中的 on 下仅一层,您指定事件 name。您在关键字types 之后指定的更深一层也称为“动作”。这是一个例子:

    name: Foo
    on:
      pull_request:
        types: ["ready_for_review", "converted_to_draft"]
    
    jobs:
      bar:
        runs-on: ["ubuntu-latest"]
        steps:
          - run: echo "event name is:" ${{ github.event_name }} 
          - run: echo "event type is:" ${{ github.event.action }} 
    

    这个输出:

    event name is: pull_request
    event type is: converted_to_draft
    

    【讨论】:

      【解决方案2】:

      从此page,您可以使用GITHUB_EVENT_NAME 环境变量。

      触发工作流的 webhook 事件的名称。

      如果您使用的是@actions/github 包,它可以让您访问context 对象,该对象允许您获取事件名称,如下所示:

      import {context} from '@actions/github';
      
      console.log(context.eventName);
      

      这两种方法都会得到相同的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-28
        • 2022-08-13
        • 2021-07-31
        • 2014-04-26
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多