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?
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.
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.
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.
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.
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.
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.
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.
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.
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 }}
This is a single workflow, which runs different jobs depending on whether it was triggered by a push or a tag creation event.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With