THRIFT-3907 Skip Docker image build on CI when unchanged
diff --git a/.travis.yml b/.travis.yml
index 847a5f8..fc5f953 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -26,16 +26,16 @@
   - docker
 
 install:
-  - cp -r ./build/docker/scripts/*.sh ./build/docker/$DISTRO/scripts/
-  - travis_retry travis_wait docker build -q -t thrift-build build/docker/$DISTRO
+  - (travis_wait ./build/docker/check_unmodified.sh $DISTRO && touch .unmodified) || true
+  - if [ ! -f .unmodified ]; then travis_retry travis_wait docker build -q -t thrift-build:$DISTRO build/docker/$DISTRO; fi
 
 script:
-  - docker run --net=host -e BUILD_LIBS="$BUILD_LIBS" $BUILD_ENV -v $(pwd):/thrift/src -it thrift-build $BUILD_CMD $BUILD_ARG
+  - docker run --net=host -e BUILD_LIBS="$BUILD_LIBS" $BUILD_ENV -v $(pwd):/thrift/src -it thrift-build:$DISTRO build/docker/scripts/$SCRIPT $BUILD_ARG
 
 env:
   global:
     - TEST_NAME=""
-    - BUILD_CMD="none"
+    - SCRIPT="cmake.sh"
     - BUILD_ARG=""
     - BUILD_ENV="-e CC=clang -e CXX=clang++"
     - DISTRO=ubuntu
@@ -44,12 +44,12 @@
   matrix:
     # TODO: Break up by protocol types to avoid 50min limit
     - TEST_NAME="Cross Language Tests"
-      BUILD_CMD="../cross-test.sh"
+      SCRIPT="cross-test.sh"
       BUILD_ARG="--enable-tutorial=no"
       BUILD_ENV="-e CC=clang -e CXX=clang++ -e THRIFT_CROSSTEST_CONCURRENCY=4"
 
     - TEST_NAME="Cross Language Tests (Debian)"
-      BUILD_CMD="../cross-test.sh"
+      SCRIPT="cross-test.sh"
       BUILD_ARG="--enable-tutorial=no"
       BUILD_ENV="-e CC=clang -e CXX=clang++ -e THRIFT_CROSSTEST_CONCURRENCY=4"
       DISTRO=debian
@@ -57,78 +57,67 @@
     # TODO: Remove them once migrated to CMake
     # Autotools builds
     - TEST_NAME="C C++ C# D Erlang Haxe Go (automake)"
-      BUILD_CMD="../autotools.sh"
+      SCRIPT="autotools.sh"
       BUILD_ARG="--without-dart --without-haskell --without-java --without-lua --without-nodejs --without-perl --without-php --without-php_extension --without-python --without-ruby"
 
     - TEST_NAME="C C++ - GCC (automake)"
-      BUILD_CMD="../autotools.sh"
+      SCRIPT="autotools.sh"
       BUILD_ARG="--without-csharp --without-java --without-erlang --without-nodejs --without-lua --without-python --without-perl --without-php --without-php_extension --without-dart --without-ruby --without-haskell --without-go --without-haxe --without-d"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
 
     - TEST_NAME="Java Lua PHP Ruby Dart (automake)"
-      BUILD_CMD="../autotools.sh"
+      SCRIPT="autotools.sh"
       BUILD_ARG="--without-cpp --without-haskell --without-c_glib --without-csharp --without-d --without-erlang --without-go --without-haxe --without-nodejs --without-python --without-perl"
 
     # These are flaky (due to cabal and npm network/server failures) and also have lengthy output
     - TEST_NAME="Haskell Node.js Python Perl (automake)"
-      BUILD_CMD="../autotools.sh"
+      SCRIPT="autotools.sh"
       BUILD_ARG="--without-cpp --without-c_glib --without-csharp --without-d --without-dart --without-erlang --without-go --without-haxe --without-java --without-lua --without-php --without-php_extension --without-ruby"
 
     # CMake build
     - TEST_NAME="All"
-      BUILD_CMD="../cmake.sh"
 
     - TEST_NAME="All (Debian)"
-      BUILD_CMD="../cmake.sh"
       DISTRO=debian
 
     - TEST_NAME="C C++ - GCC"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="CPP C_GLIB TESTING TUTORIALS"
       BUILD_ARG="-DWITH_PYTHON=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
 
     - TEST_NAME="C++ (Boost Thread)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="CPP TESTING TUTORIALS"
       BUILD_ARG="-DWITH_BOOSTTHREADS=ON -DWITH_PYTHON=OFF -DWITH_C_GLIB=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
 
     - TEST_NAME="C++ (Boost Thread - GCC)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="CPP TESTING TUTORIALS"
       BUILD_ARG="-DWITH_BOOSTTHREADS=ON -DWITH_PYTHON=OFF -DWITH_C_GLIB=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
 
     - TEST_NAME="C++ (Std Thread)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="CPP TESTING TUTORIALS"
       BUILD_ARG="-DWITH_STDTHREADS=ON -DCMAKE_CXX_FLAGS='-std=c++11' -DWITH_PYTHON=OFF -DWITH_C_GLIB=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
 
     - TEST_NAME="C++ (Std Thread - GCC)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="CPP TESTING TUTORIALS"
       BUILD_ARG="-DWITH_STDTHREADS=ON -DCMAKE_CXX_FLAGS='-std=c++11' -DWITH_PYTHON=OFF -DWITH_C_GLIB=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
 
     - TEST_NAME="Compiler (mingw)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS=""
       BUILD_ARG="-DCMAKE_TOOLCHAIN_FILE=../build/cmake/mingw32-toolchain.cmake -DBUILD_COMPILER=ON -DBUILD_LIBRARIES=OFF -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF"
       BUILD_ENV=""
 
     - TEST_NAME="All - GCC (CentOS)"
-      BUILD_CMD="../cmake.sh"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
       DISTRO=centos
 
     - TEST_NAME="C C++ - Clang (CentOS)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="CPP C_GLIB TESTING TUTORIALS"
       BUILD_ARG="-DWITH_PYTHON=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
       DISTRO=centos
 
     - TEST_NAME="Python 2.6 (CentOS 6)"
-      BUILD_CMD="../cmake.sh"
       BUILD_LIBS="PYTHON TESTING TUTORIALS"
       BUILD_ARG="-DWITH_PYTHON=ON -DWITH_CPP=OFF -DWITH_JAVA=OFF -DWITH_HASKELL=OFF"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
@@ -136,20 +125,20 @@
 
     # Distribution
     - TEST_NAME="make dist"
-      BUILD_CMD="../make-dist.sh"
+      SCRIPT="make-dist.sh"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
 
     - TEST_NAME="Debian Packages"
-      BUILD_CMD="../dpkg.sh"
+      SCRIPT="dpkg.sh"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
 
     - TEST_NAME="make dist (Debian)"
-      BUILD_CMD="../make-dist.sh"
+      SCRIPT="make-dist.sh"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
       DISTRO=debian
 
     - TEST_NAME="Debian Packages (Debian)"
-      BUILD_CMD="../dpkg.sh"
+      SCRIPT="dpkg.sh"
       BUILD_ENV="-e CC=gcc -e CXX=g++"
       DISTRO=debian
 
diff --git a/build/docker/centos/Dockerfile b/build/docker/centos/Dockerfile
index a31492b..59bbfd6 100644
--- a/build/docker/centos/Dockerfile
+++ b/build/docker/centos/Dockerfile
@@ -138,5 +138,5 @@
 
 ENV THRIFT_ROOT /thrift
 RUN mkdir -p $THRIFT_ROOT/src
-COPY scripts $THRIFT_ROOT
+COPY Dockerfile $THRIFT_ROOT/
 WORKDIR $THRIFT_ROOT/src
diff --git a/build/docker/centos/scripts/keepit b/build/docker/centos/scripts/keepit
deleted file mode 100644
index cb885df..0000000
--- a/build/docker/centos/scripts/keepit
+++ /dev/null
@@ -1 +0,0 @@
-keep it
diff --git a/build/docker/centos6/Dockerfile b/build/docker/centos6/Dockerfile
index 4df75de..aa19731 100644
--- a/build/docker/centos6/Dockerfile
+++ b/build/docker/centos6/Dockerfile
@@ -50,5 +50,5 @@
 
 ENV THRIFT_ROOT /thrift
 RUN mkdir -p $THRIFT_ROOT/src
-COPY scripts $THRIFT_ROOT
+COPY Dockerfile $THRIFT_ROOT/
 WORKDIR $THRIFT_ROOT/src
diff --git a/build/docker/centos6/scripts/keepit b/build/docker/centos6/scripts/keepit
deleted file mode 100644
index cb885df..0000000
--- a/build/docker/centos6/scripts/keepit
+++ /dev/null
@@ -1 +0,0 @@
-keep it
diff --git a/build/docker/check_unmodified.sh b/build/docker/check_unmodified.sh
new file mode 100755
index 0000000..564e4c1
--- /dev/null
+++ b/build/docker/check_unmodified.sh
@@ -0,0 +1,41 @@
+#!/bin/bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# Download prebuilt docker image and compare Dockerfile hash values
+
+set -ex
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+DISTRO=$1
+SRC_IMG=thrift/thrift-build:$DISTRO
+
+function try_pull {
+  docker pull $SRC_IMG
+  cd ${SCRIPT_DIR}/$DISTRO
+  docker run $SRC_IMG bash -c 'cd .. && sha512sum Dockerfile' > .Dockerfile.sha512
+  sha512sum -c .Dockerfile.sha512
+}
+
+if try_pull; then
+  echo Dockerfile seems identical. No need to rebuild from scratch.
+else
+  echo Either Dockerfile has changed or pull failure. Need to build brand new one.
+  exit 1
+fi
diff --git a/build/docker/debian/Dockerfile b/build/docker/debian/Dockerfile
index 285c85b..99eda7a 100644
--- a/build/docker/debian/Dockerfile
+++ b/build/docker/debian/Dockerfile
@@ -186,5 +186,5 @@
 
 ENV THRIFT_ROOT /thrift
 RUN mkdir -p $THRIFT_ROOT/src
-COPY scripts $THRIFT_ROOT
+COPY Dockerfile $THRIFT_ROOT/
 WORKDIR $THRIFT_ROOT/src
diff --git a/build/docker/debian/scripts/dpkg.sh b/build/docker/scripts/dpkg.sh
similarity index 100%
rename from build/docker/debian/scripts/dpkg.sh
rename to build/docker/scripts/dpkg.sh
diff --git a/build/docker/ubuntu/Dockerfile b/build/docker/ubuntu/Dockerfile
index 2797a1c..7792aae 100644
--- a/build/docker/ubuntu/Dockerfile
+++ b/build/docker/ubuntu/Dockerfile
@@ -204,5 +204,5 @@
 
 ENV THRIFT_ROOT /thrift
 RUN mkdir -p $THRIFT_ROOT/src
-COPY scripts $THRIFT_ROOT
+COPY Dockerfile $THRIFT_ROOT/
 WORKDIR $THRIFT_ROOT/src
diff --git a/build/docker/ubuntu/scripts/dpkg.sh b/build/docker/ubuntu/scripts/dpkg.sh
deleted file mode 100755
index 3ba0cd4..0000000
--- a/build/docker/ubuntu/scripts/dpkg.sh
+++ /dev/null
@@ -1,5 +0,0 @@
-#!/bin/sh
-set -ev
-
-dpkg-buildpackage -tc -us -uc
-ls -al ..