AI Hints
CI/CD with GitHub Actions · Lesson 30 of 138

Push and Pull Request Triggers

Which events, when.

⏱ 2 min read▶ Video at 33:46🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Most CI workflows listen for two events. Push, for commits landing on main. And pull request, for changes that want to be merged. Each tells the workflow something different, and you can act on that.

Push and Pull Request Triggers
From the video: Push and Pull Request Triggers
  • Two main events
  • push and pull_request
  • Act on which one it was

Knowing the event

This workflow runs on pushes to main, and on pull requests. The first step prints which event started it, and the branch, using expressions in double curly braces. The second step has an if condition, so it only runs for pull requests.

.github/workflows/events.ymlyaml
name: Events
on:
  push:
    branches: [main]
  pull_request:
jobs:
  info:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Started by ${{ github.event_name }} on ${{ github.ref_name }}"
      - name: Only for pull requests
        if: github.event_name == 'pull_request'
        run: echo "Checking a pull request"

Two different runs

First, as a push to main. The event is push, and the pull request step is skipped. Then, with dash dash event, we pretend a pull request opened from a feature branch. Now the event is pull request, and the extra step runs. One workflow, two behaviours.

Terminal
run-steps .github/workflows/events.yml
Workflow: Events  (event: push, branch: main)
Job: info
  ✓ Run echo "Started by push on main"
      Started by push on main
  - Only for pull requests  (skipped)
  Job succeeded
run-steps .github/workflows/events.yml --event pull_request --ref add-free-shipping
Workflow: Events  (event: pull_request, branch: add-free-shipping)
Job: info
  ✓ Run echo "Started by pull_request on add-free-shipping"
      Started by pull_request on add-free-shipping
  ✓ Only for pull requests
      Checking a pull request
  Job succeeded

Summary

Push and pull request are the core CI events. The github dot event name expression tells you which one started the run. Next, filtering by branch and by changed files.

Key takeaways
  • push: branches
  • pull_request
  • github.event_name
Next up: Branch and Path Filters — run only when needed