Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 617
soerface Avatar asked Dec 16 '19 02:12

soerface


People also ask

What is GitHub event?

GitHub events provide a handy way to receive automated status updates from your GitHub repos concerning everything from code commits to new users joining a project. And because they are accessible via a Web API as GET requests, it's easy to integrate them into the notification system of your choosing.

What are workflows in GitHub Actions?

Workflows. A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule.

What is the syntax of writing event triggers in GitHub Actions?

GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored as a separate YAML file in your code repository, in a directory named . github/workflows . You can create an example workflow in your repository that automatically triggers a series of commands whenever code is pushed.

What are the properties of the GitHub Event Context?

The properties in the github.event context depend on the type of event that triggered the workflow. For example, a workflow triggered when an issue is labeled would have information about the issue and label. Reference the webhook event documentation for common properties and example payloads.

What events can be triggered by GitHub workflow?

These events can be: Events that occur outside of GitHub and trigger a repository_dispatch event on GitHub For example, you can configure your workflow to run when a push is made to the default branch of your repository, when a release is created, or when an issue is opened.

How do I find a workflow in GitHub?

GitHub searches the .github/workflows directory in your repository for workflow files that are present in the associated commit SHA or Git ref of the event. A workflow run is triggered for any workflows that have on: values that match the triggering event.

What is the difference between a workflow event and workflow run?

The event has an associated commit SHA and Git ref. GitHub searches the .github/workflows directory in your repository for workflow files that are present in the associated commit SHA or Git ref of the event. A workflow run is triggered for any workflows that have on: values that match the triggering event.


1 Answers

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'
like image 182
soerface Avatar answered Oct 17 '22 06:10

soerface