soerface
soerface

Reputation: 6833

Which properties does `github.event` in a GitHub Workflow have?

When using GitHub Actions, it is possible to access contexts in an expression. One of the contexts is the github context. It has a property github.event, which is an object.

What properties does the github.event object have? How can I distinguish between e.g. a push event and a tag creation event?

Upvotes: 21

Views: 23708

Answers (1)

soerface
soerface

Reputation: 6833

To distinguish the different events, you can always check for github.event_name:

jobs:
  test:
    runs-on: ubuntu-18.04
    if: github.event_name == 'push'

The properties of github.event depend on what kind of event was triggered. They are documented in the "Event Types & Payload" section of the REST API v3 documentation. The section "Webhook events" of the "Events that trigger workflows" documentation contains links to every object in the "Webhook event payload" column.

Example

You have a create event, therefore github.event_name == 'create'. You can access the following properties in your workflow.yml (as described in Event Types & Payload / CreateEvent)

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

Complete workflow example

This is a single workflow, which runs different jobs depending on whether it was triggered by a push or a tag creation event.

  • Runs tests on pushes and tag creations
  • Packages the application on any tag
  • Deploys the app when the tag starting with 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>

Note that github.ref and github.event.ref differs:

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

Upvotes: 21

Related Questions