GitHub 工作流中的 `github.event` 有哪些属性?

soe*_*ace 6 continuous-integration webhooks github-actions

使用 GitHub 操作时,可以访问表达式中的上下文。上下文之一是github上下文。它有一个属性github.event,它是一个对象。

github.event对象有什么属性?我如何区分例如推送事件和标签创建事件?

soe*_*ace 10

为了区分不同的事件,您可以随时检查github.event_name

jobs:
  test:
    runs-on: ubuntu-18.04
    if: github.event_name == 'push'
Run Code Online (Sandbox Code Playgroud)

的属性github.event取决于触发的事件类型。它们记录在REST API v3 文档的“事件类型和有效负载”部分。“触发工作流的事件”文档的“Webhook 事件”部分包含指向“Webhook 事件负载”列中每个对象的链接。

例子

您有一个创建事件,因此github.event_name == 'create'。您可以访问以下属性workflow.yml(如Event Types & Payload / CreateEvent 中所述

  • ${{ github.event.ref_type }}
  • ${{ github.event.ref }}
  • ${{ github.event.master_branch }}
  • ${{ github.event.description }}

完整的工作流程示例

这是一个单一的工作流,它运行不同的作业,具体取决于它是由推送还是标签创建事件触发的。

  • 对推送和标签创建运行测试
  • 在任何标签上打包应用程序
  • 当标签以 v
name: CI

on:
  push:
    branches:
      - master
  create:
    tags:

jobs:

  test:
    runs-on: ubuntu-18.04

    steps:
      - <YOUR TESTSTEPS HERE>

  dist:
    runs-on: ubuntu-18.04
    if: github.event_name == 'create'

    steps:
      - <YOUR BUILDSTEPS HERE>
      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: mypackage
          path: dist

  deploy:
    needs: dist
    runs-on: ubuntu-18.04
    if: github.event_name == 'create' && startsWith(github.ref, 'refs/tags/v')
    # This does the same:
    # if: github.event_name == 'create' && github.event.ref_type == 'tag' && startsWith(github.event.ref, 'v')

    steps:
      - name: Download artifact
        uses: actions/download-artifact@v1
        with:
          name: mypackage
      - <YOUR DEPLOY STEPS HERE>
Run Code Online (Sandbox Code Playgroud)

请注意,github.refgithub.event.ref不同之处:

  • github.ref == 'refs/tags/v1.2.5'
  • github.event.ref == 'v1.2.5'