Skip to content

Instantly share code, notes, and snippets.

@minillinim
Created May 10, 2017 05:43
Show Gist options
  • Save minillinim/c927aab6236de1f9ff02b31b20d741dd to your computer and use it in GitHub Desktop.
Save minillinim/c927aab6236de1f9ff02b31b20d741dd to your computer and use it in GitHub Desktop.
Buildkite + monorepo + hooks

If you use a monorepo you can place a .buildkite/pipeline.yml in each of the sub repos/folders and set buildkite to look there using a pipeline command as your first step. Such as:

[[ -f "<SUB_FOLDER>/.buildkite/pipeline.yml" ]] && buildkite-agent pipeline upload <SUB_FOLDER>/.buildkite/pipeline.yml || true

Where <SUB_FOLDER> is the name of your sub repo / folder.

Unfortunately, if you try to use pre- and post-command hooks this way buildkite won't pick them up, it only cares about the ones in the root .buildkite folder.

Assuming your monorepo folder structure looks like this:

.
├── .buildkite
│   ├── hooks
│   │   ├── post-command        //
│   │   └── pre-command         // Use the templates below
│   └── utils.sh                //
|
├── FIRST_SUB_REPO/FOLDER
│   ├── .buildkite
│   │   ├── hooks
│   │   │   ├── post-command     // Put anything sub repo related here
│   │   │   └── pre-command
│   │   └── pipeline.yml         // Pipeline for your sub repo / folder
│   └── ... Filez
|
├── ANOTHER_SUB_REPO/FOLDER
│   ├── .buildkite
│   │   ├── hooks
│   │   │   ├── post-command
│   │   │   └── pre-command
│   │   └── pipeline.yml
│   └── ... Filez
...

You can use these files a template. Note: For each sub repo / folder you weant to use you need to add a section to the case statement in utils.sh.

Contents of BASE pre-command:

#!/usr/bin/env bash

set -eu

# load common utils
. .buildkite/utils.sh

run_pre_command_for "${BUILDKITE_PIPELINE_SLUG}"

Contents of BASE post-command:

#!/usr/bin/env bash

set -eu

# load common utils
. .buildkite/utils.sh

run_post_command_for "${BUILDKITE_PIPELINE_SLUG}"

Contents of utils.sh:

run_command_for () {

  run_folder=""

  case "${2}" in

    "<VALUE OF BUILDKITE_PIPELINE_SLUG>") run_folder="<THE SUB REPO/FOLDER>"
    ;;

    *) run_folder=""
    ;;
  esac

  if [[ ! -z  "${run_folder}" ]]; then
    cmd_file="$(pwd)/${run_folder}/.buildkite/hooks/${1}-command"
    [[ -e "${cmd_file}" ]] && echo "Running ${1}-command hook in ${run_folder} folder" && echo $cmd_file | sh
  fi
}

run_pre_command_for () {
  run_command_for "pre" "${1}"
}

run_post_command_for () {
  run_command_for "post" "${1}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment