In GitHub Actions, how to get the type of a trigger event as a variable?

Viewed 5666

I’m trying to set up a GitHub action for when pull requests are opened or closed and I’d like to get the type of the trigger to add it to the message. The YAML is as follows:

on:
    pull_requeste
        types: [opened, closed, reopened] #I’d like to get which one has been triggered

For instance :

User X has opened a pull request

Someone suggested ${{env.GITHUB_EVENT_NAME}} but it’s empty. ${{github.event}} seems to be a good place but it returns an object with the webhook payload and I don’t know if "types" is in it or how to get it.

1 Answers

${{github.event.action}} should give you the action for the pull request.

Example:

on:
  pull_request:
    types: [opened, closed, reopened]
        
jobs:
  prJob:    
    name: Print info
    runs-on: ubuntu-latest
    steps:
      - name: Print GitHub event action
        run: |
          echo "${{ github.event.action }}"

The github context is documented at https://docs.github.com/en/actions/learn-github-actions/contexts#github-context. And the complete documentation for the event can be found here: https://developer.github.com/webhooks/event-payloads/#pull_request

Related