Skip to content

Commit

Permalink
Merge pull request #4 from zsims/mode
Browse files Browse the repository at this point in the history
Adds a 'trigger' mode to kick off another build
  • Loading branch information
zsims authored Jun 13, 2018
2 parents 9298097 + a03cbdf commit 5cb3a18
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# GitHub Pull Request Plugin

A [Buildkite plugin](https://buildkite.com/docs/agent/v3/plugins) to checkout the GitHub post-merge check PR ref (`refs/pull/123/merge`) rather than building the branch `HEAD`.
A [Buildkite plugin](https://buildkite.com/docs/agent/v3/plugins) to help build the GitHub post-merge check PR ref (`refs/pull/123/merge`).

This ensures tests run against the "merged" branch, helping catch bad merges.
This plugin helps you run CI run against the "merged" branch, helping catch bad merges.

The plugin has two modes:
- `mode: trigger` to async trigger another build (of the current pipeline); and
- `mode: checkout` to checkout the PR merged ref (`refs/pull/123/merge`) rather than the head (`refs/pull/123/head`)

## Example

```yml
steps:
- plugins:
zsims/github-merged-pr#v0.0.4: ~
zsims/github-merged-pr#v0.0.5:
mode: checkout
```
```yml
steps:
- plugins:
zsims/github-merged-pr#v0.0.5:
mode: trigger
```
Ensure `Skip pull request builds for existing commits` is set to `false` in your Pipeline settings, as BuildKite will build the branch and skip the PR build.
Expand Down
33 changes: 32 additions & 1 deletion hooks/pre-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

set -euo pipefail

if [[ -n "${BUILDKITE_PULL_REQUEST}" && "${BUILDKITE_PULL_REQUEST}" != "false" ]] ; then
mode="checkout"
if [[ -v BUILDKITE_PLUGIN_GITHUB_MERGED_PR_MODE ]]; then
mode="${BUILDKITE_PLUGIN_GITHUB_MERGED_PR_MODE}"
fi

function trigger_merged_build() {
local slug="${BUILDKITE_PIPELINE_SLUG}"
local pr="${BUILDKITE_PULL_REQUEST}"
local ref="refs/pull/${pr}/merge"
echo "--- Triggering merged build of ${slug} for PR ${pr} (ref ${ref})"
cat <<EOL | buildkite-agent pipeline upload
- label: "Merge build"
trigger: "${slug}"
async: true
build:
message: "Merge build for Pull Request ${pr}"
branch: "${ref}"
EOL
}

function update_ref_to_merged() {
# Setting BUILDKITE_REFSPEC will avoid other checkout behavior: https://github.com/buildkite/agent/blob/3b979f044ec7321470a8b1d742eee23ad4f31bce/templates/bootstrap.sh#L288
export BUILDKITE_REFSPEC="refs/pull/${BUILDKITE_PULL_REQUEST}/merge"
echo "--- Setting BUILDKITE_REFSPEC to ${BUILDKITE_REFSPEC}"
}

if [[ -n "${BUILDKITE_PULL_REQUEST}" && "${BUILDKITE_PULL_REQUEST}" != "false" ]] ; then
if [[ "$mode" == "checkout" ]]; then
update_ref_to_merged
elif [[ "$mode" == "trigger" ]]; then
trigger_merged_build
else
echo "--- Invalid mode: $mode"
exit 1
fi
else
echo "--- No BUILDKITE_PULL_REQUEST variable set, using default checkout behavior"
fi
16 changes: 16 additions & 0 deletions tests/pre-checkout.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ pre_checkout_hook="$PWD/hooks/pre-checkout"
assert_output --partial "Setting BUILDKITE_REFSPEC to refs/pull/123/merge"
}

@test "Triggers a build if PR number set and mode is trigger" {
export BUILDKITE_PULL_REQUEST=123
export BUILDKITE_PLUGIN_GITHUB_MERGED_PR_MODE="trigger"
export BUILDKITE_PIPELINE_SLUG="my-pipeline"

stub buildkite-agent \
"pipeline upload : echo UPLOADED_PIPELINE"

run "$pre_checkout_hook"

assert_success
assert_output --partial "Triggering merged build of my-pipeline for PR 123"
assert_output --partial "UPLOADED_PIPELINE"
unstub buildkite-agent
}

@test "Does nothing if no PR number set" {
export BUILDKITE_PULL_REQUEST=""

Expand Down

0 comments on commit 5cb3a18

Please sign in to comment.