ci-dockerfiles-*: Refactor build name/version preparation

Share/reuse the code to keep behavior consistent.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
Change-Id: Iae6b9e7a053eb52c439e67566d09368ea0e610b3
diff --git a/ci-dockerfiles-deployment.yaml b/ci-dockerfiles-deployment.yaml
index 0ecf0ef..23ce2d6 100644
--- a/ci-dockerfiles-deployment.yaml
+++ b/ci-dockerfiles-deployment.yaml
@@ -66,9 +66,8 @@
                 credential-id: DOCKER_AUTH
                 variable: DOCKER_AUTH
     builders:
-        - shell: |
-            #!/bin/bash -e
-            echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
+        - shell:
+            !include-raw: ci-dockerfiles-deployment/build-version.sh
         - build-name-setter:
             name: 'version.txt'
             file: true
diff --git a/ci-dockerfiles-deployment/build-version.sh b/ci-dockerfiles-deployment/build-version.sh
new file mode 100644
index 0000000..71657a3
--- /dev/null
+++ b/ci-dockerfiles-deployment/build-version.sh
@@ -0,0 +1,6 @@
+#!/bin/bash -e
+if [ -n "${GERRIT_PATCHSET_REVISION}" ]; then
+    echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
+else
+    echo "#${BUILD_NUMBER}" > ${WORKSPACE}/version.txt
+fi
diff --git a/ci-dockerfiles-sanity-check.yaml b/ci-dockerfiles-sanity-check.yaml
index ee4ed40..39c025b 100644
--- a/ci-dockerfiles-sanity-check.yaml
+++ b/ci-dockerfiles-sanity-check.yaml
@@ -69,13 +69,8 @@
     wrappers:
         - timestamps
     builders:
-        - shell: |
-            #!/bin/bash -e
-            if [ -n "${GERRIT_PATCHSET_REVISION}" ]; then
-                echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
-            else
-                echo "#${BUILD_NUMBER}" > ${WORKSPACE}/version.txt
-            fi
+        - shell:
+            !include-raw: ci-dockerfiles-deployment/build-version.sh
         - build-name-setter:
             name: 'version.txt'
             file: true