Support branch input to unit test docker script

With the upcoming OpenBMC release, it will be the first time
an official branch is done to all of the repos. The CI
infrastructure needs to support building against this
new branch (as well as master).

Testing:
- Verified with no branch input it behaves as it always did
- Verified with a non-existent branch it behaves as it always did
- Verified with a valid branch in a single repo that it used it
  and defaulted to master for the rest

Change-Id: Ic71153483eb407f596afea1b748387b23bbc81fd
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/build-unit-test-docker.sh b/build-unit-test-docker.sh
index 7607c0f..0f2040e 100755
--- a/build-unit-test-docker.sh
+++ b/build-unit-test-docker.sh
@@ -7,11 +7,16 @@
 #                     default is openbmc/ubuntu-unit-test
 #   DISTRO:           <optional, the distro to build a docker image against>
 #                     default is ubuntu:bionic
+#   BRANCH:           <optional, branch to build from each of the openbmc/
+#                     respositories>
+#                     default is master, which will be used if input branch not
+#                     provided or not found
 
 set -uo pipefail
 
 DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test"}
 DISTRO=${DISTRO:-"ubuntu:bionic"}
+BRANCH=${BRANCH:-"master"}
 
 # Determine the architecture
 ARCH=$(uname -m)
@@ -59,8 +64,15 @@
   local package="$1"
 
   local tip
+  # Need to continue if branch not found, hence || true at end
   tip=$(git ls-remote "https://github.com/openbmc/${package}" |
-        grep 'refs/heads/master' | awk '{ print $1 }')
+        grep "refs/heads/$BRANCH" | awk '{ print $1 }' || true)
+
+  # If specific branch is not found then try master
+  if [ ! -n "$tip" ]; then
+    tip=$(git ls-remote "https://github.com/openbmc/${package}" |
+         grep "refs/heads/master" | awk '{ print $1 }')
+  fi
 
   # Lock the file to avoid interlaced writes
   exec 3>> "$DEPCACHE_FILE"
diff --git a/run-unit-test-docker.sh b/run-unit-test-docker.sh
index 8475556..e3d3ec6 100755
--- a/run-unit-test-docker.sh
+++ b/run-unit-test-docker.sh
@@ -5,12 +5,17 @@
 #   DISTRO = Docker base image. Ubuntu and Fedora are supported.
 #   WORKSPACE = <location of unit test execution script>
 #   dbus_sys_config_file = <path of the dbus config file>
+#   BRANCH = <optional, branch to build from each of the openbmc/respositories>
+#            default is master, which will be used if input branch not
+#            provided or not found
+#   DOCKER_IMG_NAME = Default is openbmc/ubuntu-unit-test-master with a
+#            -$BRANCH replacing -master if $BRANCH provided
 
 # Trace bash processing. Set -e so when a step fails, we fail the build
 set -uo pipefail
 
 # Default variables
-DOCKER_IMG_NAME="openbmc/ubuntu-unit-test"
+DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH:-master}"}
 DISTRO=${DISTRO:-ubuntu:bionic}
 WORKSPACE=${WORKSPACE:-$(mktemp -d --tmpdir unit-test.XXXXXX)}
 OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
@@ -62,7 +67,11 @@
 # Configure docker build
 cd ${WORKSPACE}/${OBMC_BUILD_SCRIPTS}
 echo "Building docker image with build-unit-test-docker.sh"
-./build-unit-test-docker.sh ${DOCKER_IMG_NAME} ${DISTRO}
+# Export input env variables
+export DOCKER_IMG_NAME
+export DISTRO
+export BRANCH
+./build-unit-test-docker.sh
 
 # Unit test and parameters
 UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},-p,${UNIT_TEST_PKG},-v"