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.
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.
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 ...
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]"
Using a block or Input step, to require input and set meta-data e.g.,