Checkout: Get pull request number from action

Created on 23 Oct 2019  路  17Comments  路  Source: actions/checkout

I'm trying to create an action that is triggered on a PR and tags a specified reviewer. For that I'm creating an action which uses the GitHub API to create a review request. To create this request I need the pull request number. The action is triggered by a PR, but I can't find the pull request number in the action logs.

Most helpful comment

@kirosc I wrote a small post on retrieving the pull number based on an issue event.

If you use ${{github.event.number}} you will get the PR number on pull request event.
If you use ${{github.event.issue.number}} you will get the PR number on any issue event e.g. updates.

Not sure if that helps

All 17 comments

You can extract the PR number from the environment variable GITHUB_REF. For on: pull_request workflows GITHUB_REF takes the format refs/pull/:prNumber/merge.

ref: https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request

Thanks! I also found this:
pull_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")

Javascript version

"use strict";

const parsePullRequestId = githubRef => {
  const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef);
  if (!result) throw new Error("Reference not found.");
  const [, pullRequestId] = result;
  return pullRequestId;
};

const main = async () => {
  const pullRequestId = parsePullRequestId(process.env.GITHUB_REF);
};

main().catch(err => {
  console.error(err);
  process.exitCode = 1;
});

Today I found that the PR number will be missing if you re-run the job for on: pull_request workflow. The only reliable way to get it is through $GITHUB_EVENT_PATH.

const fs = require('fs')
const ev = JSON.parse(
  fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')
)
const prNum = ev.pull_request.number

I think it is kind of a bug of GitHub Actions. 馃槶

I also think this is an important feature and it is a shame that this issue is closed "since you can hack your way around it". :(

In case this is still useful to anybody, a simple shell solution:

PR_NUMBER=$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; { print $3 }')

Did anyone attempt to get the $GITHUB_REF but have a return with no PR number in the output? For example, my output is something like refs/head/master but has no pull number. Would love to see if anyone had a solution for this.

Did anyone attempt to get the $GITHUB_REF but have a return with no PR number in the output? For example, my output is something like refs/head/master but has no pull number. Would love to see if anyone had a solution for this.

Replying to myself, I didn't run actions/checkout before ${{github.event.issue.number}}!

Is this problem fixed?

As I understood from the above, because there are several workarounds it's closed with no official solution.

PR=$(IFS='/' read -r -a REF <<< "$GITHUB_REF" && echo ${REF[2]})

Did anyone attempt to get the $GITHUB_REF but have a return with no PR number in the output? For example, my output is something like refs/head/master but has no pull number. Would love to see if anyone had a solution for this.

Because it's not the first pull request. Instead, it's a push event.
GitHub should add the PR number to the environment variable.

@kirosc I wrote a small post on retrieving the pull number based on an issue event.

If you use ${{github.event.number}} you will get the PR number on pull request event.
If you use ${{github.event.issue.number}} you will get the PR number on any issue event e.g. updates.

Not sure if that helps

@kirosc I wrote a small post on retrieving the pull number based on an issue event.

If you use ${{github.event.number}} you will get the PR number on pull request event.
If you use ${{github.event.issue.number}} you will get the PR number on any issue event e.g. updates.

Not sure if that helps

Where is the documentation for these nested environment variables?

It's not documented that well in the Github Actions/Workflow docs, but you'll find the github.event.number cited quite a bit around the web. I'm not sure where that's documented on official docs, unless github.event is vague and depends on context what properties it has.

They do have official docs on pull request event which github.event.pull_request.number provides.

Issue event is similar, so github.event.number is a more generic approach (since on Github issues and PRs share the same incrementing number ID source for new issues/PRs).

@polarathene thank you! best answer here and the only one that also works on pull_request close!

Took me a serious amount of time to get here and see github.event.number,,,
Official documentation should be made ASAP.

Was this page helpful?
0 / 5 - 0 ratings