How to get pull request number within GitHub Actions workflow

While the answer by @Samira worked correctly. I found out that there is a new way to do this and wanted to share it with anyone who might stumble upon this.

The solution is to add a stage at the beginning of your workflow which gets the PR number from the Github Token (event) and then set it as an environment variable for easy use throughout the rest of the workflow. Here is the code:

  - name: Test
    uses: actions/[email protected]
    with:
      github-token: ${{github.token}}
      script: |
        const core = require('@actions/core')
        const prNumber = context.payload.number;
        core.exportVariable('PULL_NUMBER', prNumber);

Now in any later stage, you can simply use $PULL_NUMBER to access the environment variable set before.


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 }}