Hold the value of BUILDKITE_PULL_REQUEST

Hi Team,

When I merge or close a PR, the value of the BUILDKITE_PULL_REQUEST variable becomes false. I’ve seen the documentation confirming this behavior. However, I need a way to retrieve the PR number associated with the BUILDKITE_PULL_REQUEST variable during the last run, specifically when the script is running from the main branch after the merge.

There are certain operations I need to perform upon closing or merging a PR, and the false value is disrupting the logic.

Could you please assist with this issue?

Thanks and regards,
Ismail

Hey @ismail

Thanks for using the Buildkite Community Forums!

There are a few ways that the Pull Request Number can be retrieved after the merging of a Pull Request, will list a few of them but aware that some might not be appropriate depending on your workflows.

  1. Retrieving it from BUILDKITE_MESSAGE environment variable, if as a part of your commit message the Pull Request Number is included, as seen as a part of some GitHub Merge Commits e.g., Merge pull request #4 from ...

  2. Using buildkite-agent meta-data to store the Pull Request Number in a Build e.g.,

- label: "set PR Number in Meta-Data"
  command: "buildkite-agent meta-data set pull-request-number $$BUILDKITE_PULL_REQUEST"

and then retrieve it in another, but this requires the Build ID or Job ID from where the meta-data is set e.g.,

- label: "get PR Number from Meta-Data"
  command: "buildkite-agent meta-data get pull-request-number --build [Build ID from another Build]"
  1. Using a block or Input step, to require input and set meta-data e.g.,
- block: "Previous PR Number"
  fields:
      - text: "Pull Request Number"
        key: "pull-request-number"

which can be retrieved later in your Build e.g.,

buildkite-agent meta-data get "pull-request-number"

Let us know if any of these options helps.

Regards,

Tom