# Conditions to check if PR is closed or merged

**URL:** <https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696>\
**Category:** Pipelines\
**Created:** [May 17, 2024, 4:44pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696 "2024-05-17T16:44:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [May 17, 2024, 4:44pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/1 "2024-05-17T16:44:11Z")

</div>

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](https://buildkite.com/docs/pipelines/conditionals) with `build.pull_request` but can’t quite hit the nail.

Can you please recommend something?

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [May 17, 2024, 6:55pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/2 "2024-05-17T18:55:01Z")

</div>

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](https://buildkite.com/docs/pipelines/conditionals#variable-and-syntax-reference-variables) if that would be helpful.

Cheers!

---

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [May 19, 2024, 3:21pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/3 "2024-05-19T15:21:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [May 20, 2024, 5:33am UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/4 "2024-05-20T05:33:29Z")

</div>

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.](https://github.com/buildkite/trigger-pipeline-action)

---

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [May 20, 2024, 9:13am UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/5 "2024-05-20T09:13:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [May 20, 2024, 12:47pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/6 "2024-05-20T12:47:40Z")

</div>

Guys, anyone who can assist here?

---

<div class="post-metadata">

**Author:** ![karen.sawrey](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/karen.sawrey/32/1188_2.png) [@karen.sawrey](https://forum.buildkite.community/u/karen.sawrey)\
**Post date:** [May 20, 2024, 1:25pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/7 "2024-05-20T13:25:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [May 20, 2024, 2:28pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/8 "2024-05-20T14:28:04Z")

</div>

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:

```auto
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):

```auto
- 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'"

```

---

<div class="post-metadata">

**Author:** ![athreya](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@athreya](https://forum.buildkite.community/u/athreya)\
**Post date:** [May 20, 2024, 2:39pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/9 "2024-05-20T14:39:42Z")

</div>

Hello @ismail !

It is possible to trigger a step by using [trigger-pipline-actions](https://github.com/buildkite/trigger-pipeline-action), but it needs some modifications in your pipline steps.

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

```auto
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](https://buildkite.com/docs/pipelines/conditionals#example-expressions) 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:

```auto
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:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/buildkite1/original/2X/b/b41bc86e0ef592c09ec40991f7f62d76aa0afdd8.png)

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 .

---

<div class="post-metadata">

**Author:** ![athreya](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@athreya](https://forum.buildkite.community/u/athreya)\
**Post date:** [May 20, 2024, 2:44pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/10 "2024-05-20T14:44:07Z")

</div>

> [@ismail](#):
>
> ```auto
> - 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'"
> 
> ```

Hey @ismail !

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

Cheers!  
Athreya

---

<div class="post-metadata">

**Author:** ![ismail](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@ismail](https://forum.buildkite.community/u/ismail)\
**Post date:** [May 29, 2024, 9:53am UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/11 "2024-05-29T09:53:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![athreya](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@athreya](https://forum.buildkite.community/u/athreya)\
**Post date:** [May 29, 2024, 11:09am UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/12 "2024-05-29T11:09:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/system/32/1321_2.png) [@system](https://forum.buildkite.community/u/system)\
**Post date:** [June 28, 2024, 9:09pm UTC](https://forum.buildkite.community/t/conditions-to-check-if-pr-is-closed-or-merged/3696/13 "2024-06-28T21:09:13Z")

</div>

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