Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the event type that triggered a GitHub action?

I'm writing a custom action in JavaScript.

In my GitHub action, I want to make a decision based on the type of the event that triggered it. For example was it a push or a cron job.

How can I access this information with the GitHub Actions Toolkit?

like image 395
K.. Avatar asked Nov 26 '19 13:11

K..


People also ask

How do I view GitHub Action logs?

Searching logs On GitHub.com, navigate to the main page of the repository. Under your repository name, click Actions. In the left sidebar, click the workflow you want to see. From the list of workflow runs, click the name of the run to see the workflow run summary.

Can you manually trigger a GitHub Action?

Earlier this week, GitHub announced a new feature which allows developers to trigger workflows manually from within the “Actions” tab.


2 Answers

From this page, you can use GITHUB_EVENT_NAME environment variable.

The name of the webhook event that triggered the workflow.

If you are using the @actions/github package, it gives you access to a context object which allows you to get the event name like so:

import {context} from '@actions/github';

console.log(context.eventName);

Both methods will give you the same result.

like image 20
smac89 Avatar answered Sep 21 '22 17:09

smac89


The title of the question and the content of it contradict to each other. You mention event type in the title but the question asks about something else:

was it a push or a cron job

This is not the event type, this is the event name. The name can be accessed via the github.event_name variable and the type can be read from github.event.action. (Note the difference between the underscore and the dot.)

Yes, the naming is quite confusing. Just one level under the on in a workflow yaml, you specify the event name. What you specify even one level deeper after they keyword types is also called "action". Here is an example:

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 }} 

This outputs:

event name is: pull_request
event type is: converted_to_draft
like image 73
Csongor Halmai Avatar answered Sep 17 '22 17:09

Csongor Halmai