From e01bb36b464fa33b7bafaf2113e7ccf8785a5372 Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@stfc.ac.uk>
Date: Tue, 27 Mar 2018 09:18:39 +0100
Subject: [PATCH] Reduce potential triggers for building code on build servers

Attempts to add more intelligence into the unix buildscript to avoid unnecessary build
steps when there are no code changes.
---
 buildconfig/Jenkins/buildscript       | 82 ++++++++++++++++++++-------
 buildconfig/Jenkins/check_for_changes | 20 +++++++
 2 files changed, 82 insertions(+), 20 deletions(-)

diff --git a/buildconfig/Jenkins/buildscript b/buildconfig/Jenkins/buildscript
index ce0f5519f11..2e53db7ad74 100755
--- a/buildconfig/Jenkins/buildscript
+++ b/buildconfig/Jenkins/buildscript
@@ -25,7 +25,7 @@ if [[ ${NODE_LABELS} == *osx* ]]; then
 fi
 
 ###############################################################################
-# All nodes currently have PARAVIEW_DIR=5.2.0 and PARAVIEW_NEXT_DIR=5.3.0-RC1
+# All nodes currently have PARAVIEW_DIR and PARAVIEW_NEXT_DIR set
 ###############################################################################
 export PARAVIEW_DIR=${PARAVIEW_DIR}
 
@@ -68,20 +68,47 @@ fi
 
 # For pull requests decide on what to build based on changeset and Jenkins
 # parameters.
-BUILDPKG=true
-SYSTEMTESTS=false
+DO_BUILD_CODE=true
+DO_UNITTESTS=true
+DO_DOCTESTS_USER=true
+DO_BUILD_DEVDOCS=true
+DO_BUILD_PKG=true
+DO_SYSTEMTESTS=false
 if [[ ${PRBUILD} == true ]]; then
+  DO_DOCTESTS_USER=false
+  DO_BUILD_DEVDOCS=false
+  DO_BUILD_PKG=false
+  DO_SYSTEMTESTS=false
+
+  # check job parameter
   if [[ -n ${BUILD_PACKAGE} ]]; then
-    BUILDPKG=${BUILD_PACKAGE}
+    DO_BUILD_PKG=${BUILD_PACKAGE}
+  fi
+  # ubuntu does docs testing
+  if [[ ${ON_UBUNTU} == true ]]; then
+    DO_DOCTESTS_USER=true
+    if ${SCRIPT_DIR}/check_for_changes dev-docs-only; then
+      DO_BUILD_CODE=false
+      DO_UNITTESTS=false
+      DO_DOCTESTS_USER=false
+      DO_BUILD_DEVDOCS=true
+    fi
+    if ${SCRIPT_DIR}/check_for_changes user-docs-only; then
+      DO_BUILD_CODE=true # make sure code is up to date on this node
+      DO_UNITTESTS=false
+    fi
   fi
+  # rhel does system testing
   if [[ ${ON_RHEL7} == true ]]; then
     if ${SCRIPT_DIR}/check_for_changes docs-gui-only; then
-      SYSTEMTESTS=false
+      DO_SYSTEMTESTS=false
     else
-      BUILDPKG=true
-      SYSTEMTESTS=true
+      DO_BUILD_PKG=true
+      DO_SYSTEMTESTS=true
     fi
   fi
+
+
 fi
 
 ###############################################################################
@@ -183,7 +210,7 @@ fi
 ###############################################################################
 # Packaging options
 ###############################################################################
-if [[ "$BUILDPKG" == true ]]; then
+if [[ ${DO_BUILD_PKG} == true ]]; then
   PACKAGINGVARS="-DPACKAGE_DOCS=ON"
   # Set some variables relating to the linux packages
   if [[ "${ON_MACOS}" == true ]]; then
@@ -266,11 +293,13 @@ fi
 ###############################################################################
 # Build step
 ###############################################################################
-${CMAKE_EXE} --build . -- -j${BUILD_THREADS:?}
-${CMAKE_EXE} --build . --target AllTests -- -j${BUILD_THREADS:?}
+if [[ ${DO_BUILD_CODE} == true ]]; then
+  ${CMAKE_EXE} --build . -- -j${BUILD_THREADS:?}
+  ${CMAKE_EXE} --build . --target AllTests -- -j${BUILD_THREADS:?}
+fi
 
 ###############################################################################
-# static analysis builds stop here
+# Static analysis builds or stop here
 ###############################################################################
 if [[ $USE_CLANG ]] && [[ ${JOB_NAME} == *clang_tidy* ]]; then
   exit 0
@@ -280,30 +309,43 @@ fi
 ###############################################################################
 # Run the unit tests
 ###############################################################################
-# Remove any Mantid.user.properties file
-userprops=~/.mantid/Mantid.user.properties
-rm -f $userprops
-$CTEST_EXE -j${BUILD_THREADS:?} --schedule-random --output-on-failure
+if [[ ${DO_UNITTESTS} == true ]]; then
+  # Remove any Mantid.user.properties file
+  userprops=~/.mantid/Mantid.user.properties
+  rm -f $userprops
+  $CTEST_EXE -j${BUILD_THREADS:?} --schedule-random --output-on-failure
+fi
 
 ###############################################################################
-# Run the documentation tests on Ubuntu when doing a pull request build but not for python 3.
+# User Documentation
 ###############################################################################
-if [[ ${ON_UBUNTU} == true ]] && [[ ${PRBUILD} == true ]]; then
+if [[ ${DO_DOCTESTS_USER} == true ]]; then
   # Remove doctrees directory so it forces a full reparse. It seems that
   # without this newly added doctests are not executed
   if [ -d $BUILD_DIR/docs/doctrees ]; then
-    rm -rf $BUILD_DIR/docs/doctrees/*
+    rm -fr $BUILD_DIR/docs/doctrees/*
   fi
   # Build HTML to verify that no referencing errors have crept in.
   ${CMAKE_EXE} --build . --target docs-html
   ${CMAKE_EXE} --build . --target docs-test
 fi
 
+###############################################################################
+# Developer Documentation
+###############################################################################
+# Uncomment this when the dev-docs are ready to build without warnings
+# if [[ ${DO_BUILD_DEVDOCS} == true ]]; then
+#   if [ -d $BUILD_DIR/dev-docs/doctree ]; then
+#     rm -fr $BUILD_DIR/dev-docs/doctree/*
+#   fi
+#   ${CMAKE_EXE} --build . --target dev-docs-html
+# fi
+
 ###############################################################################
 # Create the install kit if required. This includes building the Qt help
 # documentation
 ###############################################################################
-if [[ ${BUILDPKG} == true ]]; then
+if [[ ${DO_BUILD_PKG} == true ]]; then
   # Workaround so that the target can find the properties file
   # CMake doesn't easily allow environment variables on custom targets
   if [[ ${ON_MACOS} == true ]]; then
@@ -328,7 +370,7 @@ fi
 # Run the system tests if required. Run from a package to have at least one
 # Linux checks it install okay
 ###############################################################################
-if [[ ${SYSTEMTESTS} == true ]]; then
+if [[ ${DO_SYSTEMTESTS} == true ]]; then
   if [[ ${PRBUILD} == true ]]; then
     EXTRA_ARGS="--exclude-in-pull-requests" $SCRIPT_DIR/systemtests
   else
diff --git a/buildconfig/Jenkins/check_for_changes b/buildconfig/Jenkins/check_for_changes
index 27e070d4afe..68c391450ab 100755
--- a/buildconfig/Jenkins/check_for_changes
+++ b/buildconfig/Jenkins/check_for_changes
@@ -46,6 +46,26 @@ case "$TYPE" in
 	    exit $NOTFOUND
 	fi
     ;;
+    dev-docs-only)
+	# FOUND=1 iff changes are limited to dev-docs only
+	# Find all changed files and grep for required type. -v inverts match so grep=0 means
+	# there are other changes besides this
+	if git diff --name-only ${BRANCH_TIP} ${BRANCH_BASE} -- | grep -q -E -v '^dev-docs/'; then
+	    exit $FOUND
+	else
+	    exit $NOTFOUND
+	fi
+    ;;
+    user-docs-only)
+	# FOUND=1 iff changes are limited to user docs only
+	# Find all changed files and grep for required type. -v inverts match so grep=0 means
+	# there are other changes besides this
+	if git diff --name-only ${BRANCH_TIP} ${BRANCH_BASE} -- | grep -q -E -v '^docs/'; then
+	    exit $FOUND
+	else
+	    exit $NOTFOUND
+	fi
+    ;;
     *)
         echo "do not have case for type \"$TYPE\""
     ;;
-- 
GitLab