# Fix GitHub Actions "github.event.pull_request is null"

## TL;DR
The workflow ran on an event that has no pull request payload: push, schedule, or manual dispatch. Guard PR-only steps with a check on the event name, or stop dereferencing PR fields on those events. Each event type ships a different payload shape, and only PR events carry the pull_request object.

```text
GitHub Actions "github.event.pull_request is null": context debugging
```

## Steps

1. **Check the triggers.** Read the workflow's `on:` block and look for push, schedule, or workflow_dispatch alongside pull_request.
   Expected: you find at least one non-PR trigger, which explains the null.

2. **Guard PR-only steps.** Add a condition like `if: github.event_name == 'pull_request'` (or include pull_request_target) to steps that touch PR fields.
   Expected: those steps skip cleanly on push and schedule runs instead of erroring.

3. **Stop dereferencing blindly.** Before using fields like the PR number or head sha, make sure the object exists in that event.
   Expected: no more null dereference failures in expressions.

4. **If you need PR data on push events, fetch it.** Use the list pull requests API filtered by branch, or restrict the workflow to PR triggers only.
   Expected: you stop relying on a context object that is not there.

5. **For scheduled runs that need a PR, pass it explicitly.** Accept the PR number as a workflow_dispatch input and reference the input instead of the event.
   Expected: the run has what it needs regardless of trigger.

## Use this when
- Expressions on github.event.pull_request evaluate to null
- A workflow with multiple triggers fails only on push or schedule runs
- Steps referencing PR numbers or head SHAs break intermittently

## Not for this skill when
- You are deciding between pull_request and pull_request_target for checkout security (different question)
- The API returns 404 for a PR number (that is the API call, not the context)
- Some other context object is null (same technique applies, different object)

## Variant phrasings
- pull_request null github actions
- event.pull_request undefined
- gha context null
- github event pull request does not exist

## Why it happens
GitHub builds the event payload per trigger type. Push payloads describe commits, schedule payloads describe the timer, and neither includes a pull_request key. Any expression touching github.event.pull_request on those runs evaluates to null, and dereferencing deeper throws.

## Edge cases
- pull_request_target does carry the PR object but runs in the base repository context with different permissions. Do not treat it as interchangeable.
- Merged PRs still fire pull_request events with the closed action. The object exists; the state field tells you it merged.
- Reusable workflow callers must forward anything the callee branches on. The callee sees the caller's event, not a fresh one.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_THZiCmHr6Wcrny6WnEedOw
