blob: 546c3b4bef274c3a238d370bc7ac43b89526e1ca [file] [log] [blame]
Andrew Z Allen7d1dae32015-06-25 07:45:32 +00001#! /usr/bin/env bash
2#
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Andrew Z Allen7d1dae32015-06-25 07:45:32 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
Thomas Broyer5bae07e2015-06-29 11:18:14 +000018# This script looks at the files changed in git against origin/master
19# (actually a common ancestor of origin/master and the current commit) and
Andrew Z Allen7d1dae32015-06-25 07:45:32 +000020# queries for all build and test targets associated with those files.
21#
22# Running this script on a CI server should allow you to only test the targets
23# that have changed since the last time your merged or fast forwarded.
24#
25# This script can be used to recreate the benefits that TAP provides to
26# Google's developers as describe by Mike Bland on his article on Google's
27# infrastructure.
28# https://mike-bland.com/2012/10/01/tools.html#tools-tap-sponge
29#
30# "Every single change submitted to Google’s Perforce depot is built and
31# tested, and only those targets affected by a particular change are
32# built and tested"
33#
Thomas Broyer5bae07e2015-06-29 11:18:14 +000034# When this script is triggered by Gerrit's patchset-updated hook (for example)
Ivan Vucicac3ca4d02016-07-11 15:01:15 +000035# you can replace origin/master in the COMMIT_RANGE variable initialization
Thomas Broyer5bae07e2015-06-29 11:18:14 +000036# with the branch passed as argument to the hook. When using Jenkins with the
37# Gerrit Trigger Plugin, use $GERRIT_BRANCH instead. This would make it
38# possible to have the Verified label on Gerrit patchsets populated as fast
39# as possible.
40# For a ref-updated event, use "${GERRIT_OLDREV}..${GERRIT_NEWREV}" as the
41# value for COMMIT_RANGE.
42# When running in Travis-CI, you can directly use the $TRAVIS_COMMIT_RANGE
43# environment variable.
Andrew Z Allen7d1dae32015-06-25 07:45:32 +000044
Ivan Vucica633e48a2016-07-11 16:17:59 +000045COMMIT_RANGE=${COMMIT_RANGE:-$(git merge-base origin/master HEAD)".."}
Andrew Z Allen7d1dae32015-06-25 07:45:32 +000046
47# Go to the root of the repo
48cd "$(git rev-parse --show-toplevel)"
49
50# Get a list of the current files in package form by querying Bazel.
51files=()
Thomas Broyer5bae07e2015-06-29 11:18:14 +000052for file in $(git diff --name-only ${COMMIT_RANGE} ); do
Androbincfb2ec02017-06-27 13:47:43 +020053 IFS=$'\n' read -r -a files <<< "$(bazel query $file)"
54 bazel query $file
Andrew Z Allen7d1dae32015-06-25 07:45:32 +000055done
56
57# Query for the associated buildables
Erik Kueflercc076342015-08-17 08:41:54 +000058buildables=$(bazel query \
59 --keep_going \
60 --noshow_progress \
Androbincfb2ec02017-06-27 13:47:43 +020061 "kind(.*_binary, rdeps(//..., set(${files[*]})))")
Andrew Z Allen7d1dae32015-06-25 07:45:32 +000062# Run the tests if there were results
63if [[ ! -z $buildables ]]; then
64 echo "Building binaries"
65 bazel build $buildables
66fi
67
Erik Kueflercc076342015-08-17 08:41:54 +000068tests=$(bazel query \
69 --keep_going \
70 --noshow_progress \
Androbincfb2ec02017-06-27 13:47:43 +020071 "kind(test, rdeps(//..., set(${files[*]}))) except attr('tags', 'manual', //...)")
Andrew Z Allen7d1dae32015-06-25 07:45:32 +000072# Run the tests if there were results
73if [[ ! -z $tests ]]; then
74 echo "Running tests"
75 bazel test $tests
76fi