Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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_request:
        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.

like image 435
Tumulte Avatar asked May 19 '20 08:05

Tumulte


People also ask

What is Workflow_dispatch in GitHub Actions?

This action triggers another GitHub Actions workflow, using the workflow_dispatch event. The workflow must be configured for this event type e.g. on: [workflow_dispatch] This allows you to chain workflows, the classic use case is have a CI build workflow, trigger a CD release/deploy workflow when it completes.

What is Pull_request_target?

pull_request_target runs in the context of the target repository of the PR, rather than in the merge commit. This means the standard checkout action uses the target repository to prevent accidental usage of the user supplied code.


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

like image 108
riQQ Avatar answered Oct 30 '22 12:10

riQQ