乐闻世界logo
搜索文章和话题

How to get the type of a trigger event as a variable In GitHub Actions

1个答案

1

In GitHub Actions, you can use the github context to retrieve the type of the triggering event for the current workflow. The github context contains detailed information about the workflow and triggering events. Specifically, the github.event_name property can be used to retrieve the name of the event that triggered the current workflow.

For example, if you want to perform different actions based on different triggering events, you can use this variable to perform conditional checks within the workflow steps. Here is a practical example:

yaml
name: Example Workflow on: push: branches: - master pull_request: branches: - master jobs: build: runs-on: ubuntu-latest steps: - name: Check out repository uses: actions/checkout@v2 - name: Print event name run: echo "The event that triggered this workflow is ${{ github.event_name }}" - name: Execute command based on event run: | if [ "${{ github.event_name }}" = "push" ]; then echo "This was triggered by a push event" elif [ "${{ github.event_name }}" = "pull_request" ]; then echo "This was triggered by a pull_request event" else echo "This was triggered by some other events" fi

In this example:

  • The workflow is triggered by push and pull_request events.
  • One step uses github.event_name to print the type of the triggering event.
  • The subsequent steps execute different commands based on the type of the triggering event.

By doing this, you can flexibly handle different events in GitHub Actions and execute specific workflow logic based on the event type.

2024年6月29日 12:07 回复

你的答案