Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pull request number within GitHub Actions workflow

I want to access the Pull Request number in a Github Actions workflow. I can access the GITHUB_REF environment variable that is available. Although on a Pull Request action it has the value: "refs/pull/125/merge". I need to extract just the "125".

I have found a similar post here that shows how to get the current branch using this variable. Although in this case, what I am parsing is different and I have been unable to isolate the Pull Request number.

I have tried using {GITHUB_REF##*/} which resolves to "merge" I have also tried {GITHUB_REF#*/} which resolves to "pull/125/merge"

I only need the Pull Request number (which in my example is 125)

like image 759
Colby Hill Avatar asked Nov 27 '19 19:11

Colby Hill


People also ask

How do I find the pull request number in GitHub Action?

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 .

How do you track Pull requests?

You can also find pull requests that you've been asked to review. At the top of any page, click Pull requests or Issues. Optionally, choose a filter or use the search bar to filter for more specific results.

What is pull request in GitHub Actions?

A GitHub action to create a pull request for changes to your repository in the actions workspace. Changes to a repository in the Actions workspace persist between steps in a workflow. This action is designed to be used in conjunction with other steps that modify or add files to your repository.


2 Answers

Although it is already answered, the easiest way I found is using the github context. The following example shows how to set it to an environment variable.

env:
  PR_NUMBER: ${{ github.event.number }}
like image 178
David Perfors Avatar answered Oct 01 '22 10:10

David Perfors


How about using awk to extract parts of GITHUB_REF instead of bash magick?

From awk manpage:

-F fs

--field-separator fs

Use fs for the input field separator (the value of the FS predefined variable).

As long you remember this, it's trivial to extract only part of variable you need. awk is available on all platforms, so step below will work everywhere:

- run:   echo ::set-env name=PULL_NUMBER::$(echo "$GITHUB_REF" | awk -F / '{print $3}')
  shell: bash
like image 41
Samira Avatar answered Oct 01 '22 12:10

Samira