diff --git a/Jenkinsfile b/Jenkinsfile
index 91c02a5db731f29fa5a511fd19f047d253173548..956d55da67624cac6e7a46fd7499d8c509ceaf6e 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -227,21 +227,7 @@ def buildOStree(architecture, type, board, debosarguments = "", repo = "repo") {
   sh(script: """
       cd ${PIPELINE_VERSION}/${architecture}/${type}
       rm -rf ${repo}
-      mkdir ${repo}
-      ostree init --repo=${repo} --mode archive-z2
-      ostree remote --repo=${repo} add --no-gpg-verify origin ${ostree_pull_url}
-      http_code=\$(curl --location --silent -o /dev/null --head -w "%{http_code}" ${ostree_pull_url}/refs/heads/${branch})
-      case \$http_code in
-        200)
-          ostree pull --repo=${repo} --depth=-1 --mirror --disable-fsync origin ${branch}
-          ;;
-        404)
-          ;;
-        *)
-          echo "Error: Got HTTP \$http_code trying to fetch ${ostree_pull_url}/refs/heads/${branch}"
-          exit 1
-          ;;
-      esac
+      ${WORKSPACE}/scripts/ostree-prepare-local-repo.sh ${repo} ${ostree_pull_url} ${branch}
       """)
 
   sh(script: """
diff --git a/scripts/ostree-prepare-local-repo.sh b/scripts/ostree-prepare-local-repo.sh
new file mode 100755
index 0000000000000000000000000000000000000000..5fafe921a009b64689452dfa9bf52bf86f07810e
--- /dev/null
+++ b/scripts/ostree-prepare-local-repo.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+set -eu
+
+if [ $# -ne 3 ]
+then
+  echo "Usage: $0 REPO PULL_URL BRANCH" >&2
+  exit 1
+fi
+
+repo=$1
+ostree_pull_url=$2
+branch=$3
+
+mkdir -p "${repo}"
+ostree init --repo="${repo}" --mode archive-z2
+ostree remote --repo="${repo}" add --no-gpg-verify origin "${ostree_pull_url}"
+http_code=$(curl --location --silent -o /dev/null --head -w "%{http_code}" "${ostree_pull_url}/refs/heads/${branch}")
+case "$http_code" in
+  200)
+    ostree pull --repo="${repo}" --depth=-1 --mirror --disable-fsync origin "${branch}"
+    ;;
+  404)
+    ;;
+  *)
+    echo "Error: Got HTTP '$http_code' trying to fetch '${ostree_pull_url}/refs/heads/${branch}'"
+    exit 1
+    ;;
+esac