Interview Prep/GitHub Actions/Triggers & Events

How do you trigger a workflow on pushes to `main` and on PRs that target `main`?

BeginnerConceptTriggers & Events
Answer
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

For pull_request, branches filters the base branch, not the head. Without filters, the workflow runs on every push to every branch and every PR, which doubles runs when a PR branch is pushed (one push, one pull_request).

All GitHub Actions questions Roadmap: GitHub Actions

More on Triggers & Events