Andrew Z Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 1 | #! /usr/bin/env bash |
| 2 | # |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Andrew Z Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 4 | # |
| 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 Broyer | 5bae07e | 2015-06-29 11:18:14 +0000 | [diff] [blame] | 18 | # 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 Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 20 | # 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 Broyer | 5bae07e | 2015-06-29 11:18:14 +0000 | [diff] [blame] | 34 | # When this script is triggered by Gerrit's patchset-updated hook (for example) |
Ivan Vucica | c3ca4d0 | 2016-07-11 15:01:15 +0000 | [diff] [blame] | 35 | # you can replace origin/master in the COMMIT_RANGE variable initialization |
Thomas Broyer | 5bae07e | 2015-06-29 11:18:14 +0000 | [diff] [blame] | 36 | # 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 Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 44 | |
Ivan Vucica | 633e48a | 2016-07-11 16:17:59 +0000 | [diff] [blame] | 45 | COMMIT_RANGE=${COMMIT_RANGE:-$(git merge-base origin/master HEAD)".."} |
Andrew Z Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 46 | |
| 47 | # Go to the root of the repo |
| 48 | cd "$(git rev-parse --show-toplevel)" |
| 49 | |
| 50 | # Get a list of the current files in package form by querying Bazel. |
| 51 | files=() |
Thomas Broyer | 5bae07e | 2015-06-29 11:18:14 +0000 | [diff] [blame] | 52 | for file in $(git diff --name-only ${COMMIT_RANGE} ); do |
Androbin | cfb2ec0 | 2017-06-27 13:47:43 +0200 | [diff] [blame] | 53 | IFS=$'\n' read -r -a files <<< "$(bazel query $file)" |
| 54 | bazel query $file |
Andrew Z Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 55 | done |
| 56 | |
| 57 | # Query for the associated buildables |
Erik Kuefler | cc07634 | 2015-08-17 08:41:54 +0000 | [diff] [blame] | 58 | buildables=$(bazel query \ |
| 59 | --keep_going \ |
| 60 | --noshow_progress \ |
Androbin | cfb2ec0 | 2017-06-27 13:47:43 +0200 | [diff] [blame] | 61 | "kind(.*_binary, rdeps(//..., set(${files[*]})))") |
Andrew Z Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 62 | # Run the tests if there were results |
| 63 | if [[ ! -z $buildables ]]; then |
| 64 | echo "Building binaries" |
| 65 | bazel build $buildables |
| 66 | fi |
| 67 | |
Erik Kuefler | cc07634 | 2015-08-17 08:41:54 +0000 | [diff] [blame] | 68 | tests=$(bazel query \ |
| 69 | --keep_going \ |
| 70 | --noshow_progress \ |
Androbin | cfb2ec0 | 2017-06-27 13:47:43 +0200 | [diff] [blame] | 71 | "kind(test, rdeps(//..., set(${files[*]}))) except attr('tags', 'manual', //...)") |
Andrew Z Allen | 7d1dae3 | 2015-06-25 07:45:32 +0000 | [diff] [blame] | 72 | # Run the tests if there were results |
| 73 | if [[ ! -z $tests ]]; then |
| 74 | echo "Running tests" |
| 75 | bazel test $tests |
| 76 | fi |