github.com/m3db/m3@v1.5.0/.buildkite/scripts/pr_build.sh (about)

     1  #!/bin/bash
     2  
     3  set -exo pipefail
     4  
     5  # Script to run adhoc buildkite builds for the given branch. Useful for running
     6  # CI tests against branches that may not yet be ready for a PR. See
     7  # https://buildkite.com/docs/apis/rest-api#authentication for more info on
     8  # buildkite tokens. To take full advantage of this script your token will need
     9  # `read_builds` and `write_builds` scopes. The script expects a working
    10  # installation of jq.
    11  #
    12  # You'll also need a Github token with `public_repo` permissions.
    13  
    14  function usage() {
    15    echo "build.sh \$PR_NUMBER"
    16    exit 1
    17  }
    18  
    19  if [[ -z "$GITHUB_PR_TOKEN" ]]; then
    20    echo "must set GITHUB_PR_TOKEN"
    21    exit 1
    22  fi
    23  
    24  if [[ -z "$BUILDKITE_CLI_TOKEN" ]]; then
    25    echo "must set BUILDKITE_CLI_TOKEN"
    26    exit 1
    27  fi
    28  
    29  if ! command -v jq; then
    30    echo "cannot find jq in PATH"
    31    exit 1
    32  fi
    33  
    34  RESP=$(mktemp)
    35  
    36  function cleanup() {
    37    rm -f "$RESP"
    38  }
    39  
    40  trap cleanup EXIT
    41  
    42  curl -H "Authorization: token $GITHUB_PR_TOKEN" -sSf -o "$RESP" "https://api.github.com/repos/m3db/m3/pulls/$1"
    43  
    44  function jq_field() {
    45    <"$RESP" jq -r "$1"
    46  }
    47  
    48  function build() {
    49    local pr=$1
    50    local url="https://api.buildkite.com/v2/organizations/uberopensource/pipelines/m3-monorepo-ci"
    51    local auth="Authorization: Bearer $BUILDKITE_CLI_TOKEN"
    52    local repo; repo=$(jq_field '.head.repo.clone_url')
    53    local sha; sha=$(jq_field '.head.sha')
    54    local branch; branch=$(jq_field '.head.ref')
    55    local message; message=$(jq_field '.title')
    56  
    57    local data
    58    data=$(cat <<EOF
    59      {
    60        "branch": "$branch",
    61        "message": "$message",
    62        "commit": "$sha",
    63        "ignore_pipeline_branch_filters": true,
    64        "clean_checkout": true,
    65        "pull_request_base_branch": "master",
    66        "pull_request_id": $pr,
    67        "pull_request_repository": "$repo"
    68      }
    69  EOF
    70    )
    71  
    72    curl -sSf -X POST -H "$auth" "${url}/builds" -d "$data" | jq -r .web_url
    73  }
    74  
    75  build "$*"