travis-ci: record and skip successfully built trees

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>

SZEDER Gábor committed Dec 31, 2017 at 11:12 UTC 9cc2c76f5eaab557c42f67b9d574db89fd0865c2
6 files changed +57
ci/lib-travisci.sh
+47
@@ -21,6 +21,52 @@ skip_branch_tip_with_tag () {
21 fi
22 }
23
24 +good_trees_file="$HOME/travis-cache/good-trees"
25 +
26 +# Save some info about the current commit's tree, so we can skip the build
27 +# job if we encounter the same tree again and can provide a useful info
28 +# message.
29 +save_good_tree () {
30 + echo "$(git rev-parse $TRAVIS_COMMIT^{tree}) $TRAVIS_COMMIT $TRAVIS_JOB_NUMBER $TRAVIS_JOB_ID" >>"$good_trees_file"
31 + # limit the file size
32 + tail -1000 "$good_trees_file" >"$good_trees_file".tmp
33 + mv "$good_trees_file".tmp "$good_trees_file"
34 +}
35 +
36 +# Skip the build job if the same tree has already been built and tested
37 +# successfully before (e.g. because the branch got rebased, changing only
38 +# the commit messages).
39 +skip_good_tree () {
40 + if ! good_tree_info="$(grep "^$(git rev-parse $TRAVIS_COMMIT^{tree}) " "$good_trees_file")"
41 + then
42 + # Haven't seen this tree yet, or no cached good trees file yet.
43 + # Continue the build job.
44 + return
45 + fi
46 +
47 + echo "$good_tree_info" | {
48 + read tree prev_good_commit prev_good_job_number prev_good_job_id
49 +
50 + if test "$TRAVIS_JOB_ID" = "$prev_good_job_id"
51 + then
52 + cat <<-EOF
53 + $(tput setaf 2)Skipping build job for commit $TRAVIS_COMMIT.$(tput sgr0)
54 + This commit has already been built and tested successfully by this build job.
55 + To force a re-build delete the branch's cache and then hit 'Restart job'.
56 + EOF
57 + else
58 + cat <<-EOF
59 + $(tput setaf 2)Skipping build job for commit $TRAVIS_COMMIT.$(tput sgr0)
60 + This commit's tree has already been built and tested successfully in build job $prev_good_job_number for commit $prev_good_commit.
61 + The log of that build job is available at https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$prev_good_job_id
62 + To force a re-build delete the branch's cache and then hit 'Restart job'.
63 + EOF
64 + fi
65 + }
66 +
67 + exit 0
68 +}
69 +
70 # Set 'exit on error' for all CI scripts to let the caller know that
71 # something went wrong.
72 # Set tracing executed commands, primarily setting environment variables
@@ -30,6 +76,7 @@ set -ex
76 mkdir -p "$HOME/travis-cache"
77
78 skip_branch_tip_with_tag
79 +skip_good_tree
80
81 if test -z "$jobname"
82 then
ci/run-linux32-docker.sh
+2
@@ -22,3 +22,5 @@ docker run \
22 --volume "${HOME}/travis-cache:/tmp/travis-cache" \
23 daald/ubuntu32:xenial \
24 /usr/src/git/ci/run-linux32-build.sh $(id -u $USER)
25 +
26 +save_good_tree
ci/run-static-analysis.sh
+2
@@ -6,3 +6,5 @@
6 . ${0%/*}/lib-travisci.sh
7
8 make coccicheck
9 +
10 +save_good_tree
ci/run-tests.sh
+2
@@ -7,3 +7,5 @@
7
8 ln -s $HOME/travis-cache/.prove t/.prove
9 make --quiet test
10 +
11 +save_good_tree
ci/run-windows-build.sh
+2
@@ -99,3 +99,5 @@ gfwci "action=log&buildId=$BUILD_ID" | cut -c 30-
99
100 # Set exit code for TravisCI
101 test "$RESULT" = "success"
102 +
103 +save_good_tree
ci/test-documentation.sh
+2
@@ -25,3 +25,5 @@ sed '/^GIT_VERSION = / d' stderr.log
25 ! test -s stderr.log
26 test -s Documentation/git.html
27 grep '<meta name="generator" content="Asciidoctor ' Documentation/git.html
28 +
29 +save_good_tree