travis-ci: record and skip successfully built trees
authorSZEDER Gábor <szeder.dev@gmail.com>
Sun, 31 Dec 2017 10:12:05 +0000 (11:12 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 2 Jan 2018 19:25:58 +0000 (11:25 -0800)
Travis CI dutifully builds and tests each new branch tip, even if its
tree has previously been successfully built and tested. This happens
often enough in contributors' workflows, when a work-in-progress
branch is rebased changing e.g. only commit messages or the order or
number of commits while leaving the resulting code intact, and is then
pushed to a Travis CI-enabled GitHub fork.

This is wasting Travis CI's resources and is sometimes scary-annoying
when the new tip commit with a tree identical to the previous,
successfully tested one is suddenly reported in red, because one of
the OSX build jobs happened to exceed the time limit yet again.

So extend our Travis CI build scripts to skip building commits whose
trees have previously been successfully built and tested. Use the
Travis CI cache feature to keep a record of the object names of trees
that tested successfully, in a plain and simple flat text file, one
line per tree object name. Append the current tree's object name at
the end of every successful build job to this file, along with a bit
of additional info about the build job (commit object name, Travis CI
job number and id). Limit the size of this file to 1000 records, to
prevent it from growing too large for git/git's forever living
integration branches. Check, using a simple grep invocation, in each
build job whether the current commit's tree is already in there, and
skip the build if it is. Include a message in the skipped build job's
trace log, containing the URL to the build job successfully testing
that tree for the first time and instructions on how to force a
re-build. Catch the case when a build job, which successfully built
and tested a particular tree for the first time, is restarted and omit
the URL of the previous build job's trace log, as in this case it's
the same build job and the trace log has just been overwritten.

Note: this won't kick in if two identical trees are on two different
branches, because Travis CI caches are not shared between build jobs
of different branches.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Reviewed-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ci/lib-travisci.sh
ci/run-linux32-docker.sh
ci/run-static-analysis.sh
ci/run-tests.sh
ci/run-windows-build.sh
ci/test-documentation.sh
index 197aa14c11a9b42cb49d3c62a9f6a2e98cb1a39b..bade716171c60634e959d32b37a6b51e658b5cf2 100755 (executable)
@@ -21,6 +21,52 @@ skip_branch_tip_with_tag () {
        fi
 }
 
+good_trees_file="$HOME/travis-cache/good-trees"
+
+# Save some info about the current commit's tree, so we can skip the build
+# job if we encounter the same tree again and can provide a useful info
+# message.
+save_good_tree () {
+       echo "$(git rev-parse $TRAVIS_COMMIT^{tree}) $TRAVIS_COMMIT $TRAVIS_JOB_NUMBER $TRAVIS_JOB_ID" >>"$good_trees_file"
+       # limit the file size
+       tail -1000 "$good_trees_file" >"$good_trees_file".tmp
+       mv "$good_trees_file".tmp "$good_trees_file"
+}
+
+# Skip the build job if the same tree has already been built and tested
+# successfully before (e.g. because the branch got rebased, changing only
+# the commit messages).
+skip_good_tree () {
+       if ! good_tree_info="$(grep "^$(git rev-parse $TRAVIS_COMMIT^{tree}) " "$good_trees_file")"
+       then
+               # Haven't seen this tree yet, or no cached good trees file yet.
+               # Continue the build job.
+               return
+       fi
+
+       echo "$good_tree_info" | {
+               read tree prev_good_commit prev_good_job_number prev_good_job_id
+
+               if test "$TRAVIS_JOB_ID" = "$prev_good_job_id"
+               then
+                       cat <<-EOF
+                       $(tput setaf 2)Skipping build job for commit $TRAVIS_COMMIT.$(tput sgr0)
+                       This commit has already been built and tested successfully by this build job.
+                       To force a re-build delete the branch's cache and then hit 'Restart job'.
+                       EOF
+               else
+                       cat <<-EOF
+                       $(tput setaf 2)Skipping build job for commit $TRAVIS_COMMIT.$(tput sgr0)
+                       This commit's tree has already been built and tested successfully in build job $prev_good_job_number for commit $prev_good_commit.
+                       The log of that build job is available at https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$prev_good_job_id
+                       To force a re-build delete the branch's cache and then hit 'Restart job'.
+                       EOF
+               fi
+       }
+
+       exit 0
+}
+
 # Set 'exit on error' for all CI scripts to let the caller know that
 # something went wrong.
 # Set tracing executed commands, primarily setting environment variables
@@ -30,6 +76,7 @@ set -ex
 mkdir -p "$HOME/travis-cache"
 
 skip_branch_tip_with_tag
+skip_good_tree
 
 if test -z "$jobname"
 then
index 3a8b2ba42aeb9a5aaba94830a45329bfe871be73..870a41246310963383754289d514d15faf8b839f 100755 (executable)
@@ -22,3 +22,5 @@ docker run \
        --volume "${HOME}/travis-cache:/tmp/travis-cache" \
        daald/ubuntu32:xenial \
        /usr/src/git/ci/run-linux32-build.sh $(id -u $USER)
+
+save_good_tree
index 68dd0f080e88983a1be250ed2e4c9f9d8cf144f2..fe4ee4e06b1e33d3ee1cd9022df06c960dd8d75b 100755 (executable)
@@ -6,3 +6,5 @@
 . ${0%/*}/lib-travisci.sh
 
 make coccicheck
+
+save_good_tree
index ccdfc2b9d367ca0fe4a3bbbbfed8644409def8c2..eb5ba4058ad0cf34302d37907ebe98dfbfc06d02 100755 (executable)
@@ -7,3 +7,5 @@
 
 ln -s $HOME/travis-cache/.prove t/.prove
 make --quiet test
+
+save_good_tree
index 86999268a72c3dc1b81383cdcfe41919dd337e15..d99a180e528084a43dff3f640ad3036f4f9dbf07 100755 (executable)
@@ -99,3 +99,5 @@ gfwci "action=log&buildId=$BUILD_ID" | cut -c 30-
 
 # Set exit code for TravisCI
 test "$RESULT" = "success"
+
+save_good_tree
index 7a0a848e83d68152acaa1de50fd7fb5fe9990151..3d62e6c9539b04f1601b92617a514c1a8b63fe56 100755 (executable)
@@ -25,3 +25,5 @@ sed '/^GIT_VERSION = / d' stderr.log
 ! test -s stderr.log
 test -s Documentation/git.html
 grep '<meta name="generator" content="Asciidoctor ' Documentation/git.html
+
+save_good_tree