build_package.sh: Retry SPM submodule operations

These fail frequently for LTS builds.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
Change-Id: I4accef70afc8fb90c7cf0d04e8e8208e572662bf
diff --git a/script/build_package.sh b/script/build_package.sh
index e1643d0..40674a2 100755
--- a/script/build_package.sh
+++ b/script/build_package.sh
@@ -98,6 +98,18 @@
 	fi
 }
 
+# Retry a command a number of times if it fails. Intended for I/O commands
+# in a CI environment which may be flaky.
+function retry() {
+    for i in $(seq 1 3); do
+        if "$@"; then
+            return 0
+        fi
+        sleep $(( i * 5 ))
+    done
+    return 1
+}
+
 # Call hook $1 in all chosen fragments if it's defined. Hooks are invoked from
 # within a subshell, so any variables set within a hook are lost. Should a
 # variable needs to be set from within a hook, the function 'set_hook_var'
@@ -1279,7 +1291,7 @@
 	pushd "$spm_root"
 	# Check if submodules need initialising
 	if git submodule status | grep '^-'; then
-		git submodule update --init
+		retry git submodule update --init
 	fi
 	popd