Get Pull Request Number with GitHub Actions: Complete Guide

Logan Jul 13, 2026

In the realm of continuous integration and deployment, GitHub Actions offers a powerful, inline software engineering tool. One of its many strengths lies in its ability to interact with pull requests, an essential part of collaborative coding. A common need is to obtain the pull request number within the context of GitHub Actions. Let's delve into how you can achieve this.

How to Add Screenshots and Animation to GitHub Pull Requests - Make Tech Easier
How to Add Screenshots and Animation to GitHub Pull Requests - Make Tech Easier

Before we explore the methods, let's understand why fetching the pull request number is crucial. Your GitHub Actions workflows might require specific actions based on the pull request. For instance, they could create a release branch pattern or generate a changelog based on the pull request number. Knowing the pull request number also helps in maintaining a structured commit history.

Google lanceert Gemini CLI GitHub Actions als gratis AI‑codegenoot
Google lanceert Gemini CLI GitHub Actions als gratis AI‑codegenoot

The GitHub Context

The GitHub context provides the necessary information for the current workflow run. It comes with predefined context objects, including the 'pull_request' object when a workflow is triggered by a pull request event.

an image of a computer screen with text
an image of a computer screen with text

However, when using other triggers like 'push', the 'pull_request' context object might not be available. In such cases, you can use the 'github' API to fetch the necessary information.

Direct Access to 'pull_request' Context Object

the text changes looked out to be an important request
the text changes looked out to be an important request

When a workflow is triggered by a pull request event, you can access the 'pull_request' context object directly. The 'number' property of this object holds the pull request number.

Here's a simple example using the 'actions-ts' library in a TypeScript workflow file: ```typescript import { context, getOctokit } from '@actions/github'; const octokit = getOctokit(process.env.GITHUB_TOKEN); const prNumber = context.issue.number; console.log(`Pull Request Number: ${prNumber}`); ```

Using 'github' API

Git
Git

When the workflow is triggered by a 'push' event, the 'pull_request' context object is not available. Instead, you can use the 'github' API to fetch the required information. To do this, you'll need to install the '@octocat/rest' package and use the 'pagination' to retrieve the pull request details.

Here's a basic example: ```typescript import { Octokit } from "@octocat/rest"; const token = process.env.GITHUB_TOKEN; const octokit = new Octokit(token); async function getPullRequestNumber() { const pulls = await octokit.pulls.list({ owner: context.repo.owner, repo: context.repo.repo, head: context.ref, }); const pr = pulls.data.find(p => p.head.sha === context.sha); return pr ? pr.number : null; } getPullRequestNumber().then(prNumber => { if (prNumber) { console.log(`Pull Request Number: ${prNumber}`); } else { console.log("Pull request not found"); } }); ```

Using Environmental Variables

number = phone number📞
number = phone number📞

Once you've fetched the pull request number, you can store it in an environmental variable and use it throughout your workflow. This is particularly useful when you're running complex workflows that span multiple jobs.

Here's how you can set an environmental variable using core.ts: ```typescript import { setenv } from '@actions/core'; setenv('PULL_REQUEST_NUMBER', prNumber.toString()); ```

the screenshot of an email address on a cell phone
the screenshot of an email address on a cell phone
an iphone screen with the text, phone number and email address
an iphone screen with the text, phone number and email address
two different types of text and numbers are shown in the table below it is an image of
two different types of text and numbers are shown in the table below it is an image of
an iphone screen showing the settings for private numbers and other things to do on it
an iphone screen showing the settings for private numbers and other things to do on it
the sync account data button is highlighted in this screenshot, which shows you can delete your phone
the sync account data button is highlighted in this screenshot, which shows you can delete your phone
the phone numbers are displayed in different colors and sizes, with hearts coming out of them
the phone numbers are displayed in different colors and sizes, with hearts coming out of them
Spiritueel
Spiritueel
the sign in screen shows that you can log into your email address or phone number
the sign in screen shows that you can log into your email address or phone number
an iphone with the text i've lost my phone number, can i have yours?
an iphone with the text i've lost my phone number, can i have yours?
an iphone screen showing the number of people who are missing their cell phone numbers and how to find them
an iphone screen showing the number of people who are missing their cell phone numbers and how to find them
コーイチ on X
コーイチ on X
the 99 % sign is displayed on a black background with red light coming from it
the 99 % sign is displayed on a black background with red light coming from it
Find out who’s behind any phone number with this free lookup tool
Find out who’s behind any phone number with this free lookup tool
an iphone screen with two texts on it and the texting section highlighted in the bottom right corner
an iphone screen with two texts on it and the texting section highlighted in the bottom right corner
a phone screen with the text welcome and an image of a telephone number on it
a phone screen with the text welcome and an image of a telephone number on it
a green check mark on a white background
a green check mark on a white background
an iphone screen showing the factory data rest button and other email notifications on it
an iphone screen showing the factory data rest button and other email notifications on it
the words london are written in black and white
the words london are written in black and white
the sign up screen for zangi's account number and how to use it
the sign up screen for zangi's account number and how to use it
a blue and white sign that says i don't give my phone number to anyone anywhere
a blue and white sign that says i don't give my phone number to anyone anywhere

Extracting Pull Request Number from the Workflow File Path

In some cases, you might have structured your workflow files to contain the pull request number in their path. For instance, you could have a workflow file named 'pr-{pr_number}.yml'. In such scenarios, you can extract the pull request number from the workflow file path using regular expressions.

Here's a simple example: ```typescript import { context } from '@actions/github'; const regex = /pr-(\d+)\./; const match = context.payload.workflow_run.path.match(regex); const prNumber = match ? parseInt(match[1]) : null; if (prNumber) { console.log(`Pull Request Number: ${prNumber}`); } else { console.log("Unable to extract pull request number from workflow file path"); } ```

Remember, always ensure you're handling the case where the pull request number might not be available or might not match the expected format. This can prevent your workflows from failing unexpectedly.

In conclusion, GitHub Actions provides several ways to fetch the pull request number, catering to different workflow triggers and structures. By understanding and utilizing these methods, you can enhance your workflows' capabilities and streamline your continuous integration and deployment processes.