Conditions to check if PR is closed or merged

Hi Team,

I want to set up a condition for a job that should trigger only if PR is closed or merged. Have tried a few combinations here:Using conditionals | Buildkite Documentation with build.pull_request but can’t quite hit the nail.

Can you please recommend something?

Hey Ismail,

Welcome to the Community!

The BUILDKITE_PULL_REQUEST environment variable only indicates whether a build is associated with a pull request. It doesn’t differentiate between a closed PR and a merged PR.

When a PR is just closed, no build is triggered. However, a PR merge, which is a push event, does trigger a build if you have this setting enabled. Unfortunately, the environment variable doesn’t provide details on the specific event that triggered the build. It would also return true for a push to a branch in an open PR. So, achieving this use case natively isn’t feasible.

Alternatively, we could set up a job conditional based on a specific PR label and other supported variables Using conditionals | Buildkite Documentation if that would be helpful.

Cheers!

Hi Stephanie,

Is this feature something we can expect in the future?

I don’t have a use case, where we use labels, so it may not be a feasible solution.

Do you have any alternate approach/way to achieve this?

Regards,
Ismail

Hey @ismail I’m not sure on whether we’ll support this in the future. It would be quite a major change to how things operate as the PR close/merge event can often mean that the git ref is no longer available for the build to use to pull code.

However, if you wanted to handle these cases yourself (ie perhaps skipping the checkout) you could try our GitHub action that allows you to trigger Buildkite builds from any GitHub event supported in their workflows. You can check it out at: GitHub - buildkite/trigger-pipeline-action: A GitHub Action for triggering a build on a Buildkite pipeline.

Hi @paula ,

I don’t want to trigger a complete pipeline, just a step within the pipeline. The above example seems to be for the complete pipeline. Is there a way to achieve that?

Guys, anyone who can assist here?

Hello, @ismail! We’re still looking into this but I believe we might be able to provide you with a workaround. Stay tuned!

Best
Karen

Thanks for letting me know, @karen.sawrey.

In the meantime, I’ve put together a solution of my own. Although it hasn’t been tested yet, here’s what I have so far:

All the steps are executed as webhook triggered in the pipeline except for the last one which is to be triggered as api via GitHub Actions:

name: Trigger Buildkite Build on PR Close

on:
  pull_request:
    types: [closed]

jobs:
  trigger-buildkite-on-pr-close:
    runs-on: ubuntu-latest

    steps:
      - name: Trigger Buildkite Build
        env:
          BUILDKITE_API_TOKEN: ${{ secrets.BUILDKITE_API_TOKEN }}
        run: |
          if [ "${{ github.event.pull_request.merged }}" = "true" ]; then
            echo "Triggering Buildkite build for merged pull request..."
            curl -X POST "https://api.buildkite.com/v2/organizations/<my-org>/pipelines/<my-pipeline>/builds" \
              -H "Authorization: Bearer $BUILDKITE_API_TOKEN" \
              -H "Content-Type: application/json" \
              -d '{
                "commit": "${{ github.sha }}",
                "branch": "${{ github.ref_name }}",
                "message": "Build triggered by GitHub Action on PR merge"
              }'
          else
            echo "Pull request closed without merge, no build triggered."
          fi

Additionally, I made the following change in the if condition within the Buildkite pipeline (last line):

- label: "Build and push docker image :docker:"
  key: "build-push"
  command: ./scripts/build.sh
  depends_on:
    - "ci"
  if: "build.branch == 'master'"

- label: "Deleting Preview Environment"
  command: ./scripts/cleanup.sh
  if: "build.source == 'api'"

1 Like

Hello @ismail !

It is possible to trigger a step by using trigger-pipline-actions, but it needs some modifications in your pipline steps.

Here’s an example workflow that basically triggers a build if PR is closed:

name: Trigger Buildkite Build if a PR is closed
on:
  pull_request:
    types: [closed]

jobs:
  triggerBuildkite:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Trigger Buildkite Pipeline
      run: |
        curl -X POST "https://api.buildkite.com/v2/organizations/<your-org-slug>/pipelines/<your pipleine slug>/builds" \
        -H "Authorization: Bearer ${{ secrets.BUILDKITE_API_TOKEN }}" \
        -d '{
          "commit": "HEAD",
          "branch": "${{ github.head_ref }}",
          "message": "Triggered by PR closing",

        }'

The intent behind this file is to simply trigger a build if a PR is closed.

In your buildkite pipeline , you can levarge the use of conditionals and set a condition that would restrict the step to be trigered / cause the step to be triggered. In the following example pipeline, I’ve used the build.message conditional atribute to play around with my pipline:

steps:
- label: "Run on PR close and Merge"
  command: echo "Close and merge step is running"
  if: build.message =~ /Triggered / || build.message =~ /Merge /


- label: "Run on Merge only"
  command: echo "regular step is running"
  if: build.message =~ /Merge /

And my pipeline settings are as follows:

In the above case, When you create a branch, a pipeline is uploaded, and as long as you ensure that you do not have a “Merge” or a “Triggered” keyword in your build message/commit message , no successive steps will run. And once you close a PR , the github actions will trigger a build with a message “Triggered by PR closing”. On buildkite side, this evaluates “if: build.message =~ /Triggered /” to true, and the respective step would be triggered.

When merged, build.message =~ /Merge / on both the steps will evaluate to true and will run both the steps.

Feel free to reach out if you require any further assistance, we’re here to help!

Cheers,
Athreya .

Hey @ismail !

This approach looks better too. Glad that you figured out a workaround : )

Cheers!
Athreya

My solution worked and the trigger is happening via API. I would still suggest adding this feature in BuildKite as acting on merge is something many would need

Hi Ismail,

Thank you for the suggestion! We are still unsure if we will be able to support this in the future. It would require a significant change in the way things currently work, especially since the PR close/merge event can mostly make the git reference unavailable for the build to access the code.

Best regards,
Athreya.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.