# Phabricator

[Phabricator](https://phacility.com/phabricator/) can trigger Buildkite builds on new revisions through Harbormaster.

Phabricator and Buildkite integrate using webhooks. Phabricator triggers builds in Buildkite with webhooks, then Buildkite reports the status back to Phabricator also using webhooks.

## Before you start

Check that your repository is activated in your Phabricator instance and
Harbormaster is installed. Configure a pipeline for that repository in Buildkite. You'll also need to create a Buildkite API access token.

>📘
> Admin access in both Phabricator and your Buildkite organization will be required.

## Step 1: New Phabricator build step

Create a new Build Plan in Harbormaster.

Inside Harbormaster open **Manage Build Plans**. Click **Create Build Plan** located in the upper right.

<div style="max-width: 950px"><div class="responsive-image-container"><img alt="Creating a new Build Plan in Phabricator" src="/docs/assets/phabricator_create_build_plan-iwCxs2YI.png" /></div></div>

Provide a name then click **Create Build Plan**.

Next, add a step to the Build Plan. A Phabricator **Build Step** creates a build on one Buildkite **pipeline**. If you need to create builds on multiple pipelines, create multiple Build Steps.

<div style="max-width: 944px"><div class="responsive-image-container"><img alt="Selecting Buildkite for this Build Step" src="/docs/assets/phabricator_run_in_buildkite-BW1G6LRX.png" /></div></div>

Click **Add Build Step** on the next screen. Then select **Build with Buildkite**.

<div style="max-width: 795px"><div class="responsive-image-container"><img alt="Incomplete Build Step" src="/docs/assets/phabricator_blank_build_step-Dt838yUB.png" /></div></div>

Keep this screen open while you configure Buildkite.

## Step 2: Configure Buildkite notification webhook

Create a [new API access token](https://buildkite.com/user/api-access-tokens/new) in your Buildkite Personal Settings.

Provide a description, then select your organization. Select the `read_builds` and `write_builds` scopes. Create the token, then copy the token from the subsequent screen.

<div style="max-width: 1176px"><div class="responsive-image-container"><img alt="Creating a new Buildkite API access token" src="/docs/assets/buildkite_new_api_access_token-BjROjdEn.png" /></div></div>

Next, add a **Webhook Notification** in your Buildkite organization's Notification Services.

From Phabricator's **Webhook Configuration**, copy the **Webhook URL** (it should end in `/harbormaster/hook/buildkite/`) and paste it into the **Webhook URL** field in Buildkite. Take a copy of the autogenerated value in the **Token** field in Buildkite. Select the `build.finished` Event, then save the notification settings.

<div style="max-width: 601px"><div class="responsive-image-container"><img alt="Buildkite Notification Webhook settings" src="/docs/assets/buildkite_notification_webhook_settings-Bo-PBq_M.png" /></div></div>

## Step 3: Complete Phabricator build step

In Phabricator, click the **Add New Credential** button next to **API Token**. Provide a name and use the Buildkite API access token that you created earlier for the **Token** field.

<div style="max-width: 882px"><div class="responsive-image-container"><img alt="Creating a new Credential in Phabricator" src="/docs/assets/phabricator_new_credential-BC1eBTDG.png" /></div></div>

Fill in the Buildkite **Organization Name** and **Pipeline Name**. Use the Token
from Buildkite's Notification Webhook for **Webhook Token** in Phabricator.

<div style="max-width: 810px"><div class="responsive-image-container"><img alt="Completing the Phabricator Build Step" src="/docs/assets/phabricator_build_step-DVLRmvUB.png" /></div></div>

Finally, click **Create Build Step**.

## Step 4: Test with a manual build

Click **Run Plan Manually** on the next screen. Provide a revision ID from the repository inside Phabricator.

<div style="max-width: 920px"><div class="responsive-image-container"><img alt="Run a manual build in Phabricator" src="/docs/assets/phabricator_manual_build_trigger-B9NPBVnz.png" /></div></div>

Your Buildkite pipeline should run, then report back to Phabricator.

<div style="max-width: 634px"><div class="responsive-image-container"><img alt="Build status in Phabricator" src="/docs/assets/phabricator_build_status-BHX3k5Rt.png" /></div></div>
<div style="max-width: 740px"><div class="responsive-image-container"><img alt="Build status in Buildkite" src="/docs/assets/buildkite_pipeline-sw1llqJM.png" /></div></div>

## Step 5: Configure builds on new commits

The Herald application acts as a "trigger" inside Phabricator. You can create a Herald rule that runs your Build Step on new commits.

Open Herald, and click **Create Herald Rule**. Select **Commits** on the next
screen, then select **Object** on the screen after that. This allows you to connect a rule to a specific repository which you'll configure on the next screen.

Fill in the repository name. It should start with `r`, so this example is using `rDEMO`.

<div style="max-width: 942px"><div class="responsive-image-container"><img alt="Create a Herald Rule" src="/docs/assets/phabricator_create_rule-foErRKMz.png" /></div></div>

Finally, configure an action associated with this rule. Adding conditions is optional, but you could use this to, for example, limit builds to specific branches.

Select **Run Build Plan**, then provide the Build Plan created in the previous step. Finally, press **Save Rule**.

<div style="max-width: 940px"><div class="responsive-image-container"><img alt="Configure Rule Action" src="/docs/assets/phabricator_configure_action-DWr0YFUP.png" /></div></div>

Now that your setup is complete, every time there is a new commit in your repository a build will be run on your Buildkite pipeline and the status will appear in Phabricator.

[phabricator]: https://phacility.com/phabricator/
