diff --git a/includes/include.drupalci.main-d7.yml b/includes/include.drupalci.main-d7.yml
index 53362eb6cce6f7611a522d61e45d8b5ec3a49508..5226e3d59e8d8b356a57fd84c7e412bc1d6fc932 100644
--- a/includes/include.drupalci.main-d7.yml
+++ b/includes/include.drupalci.main-d7.yml
@@ -79,11 +79,17 @@
     export DRUPAL_PROJECT_FOLDER=$CI_PROJECT_DIR/$_WEB_ROOT/$DRUPAL_PROJECTS_PATH/$CI_PROJECT_NAME
     echo "DRUPAL_PROJECT_FOLDER=$DRUPAL_PROJECT_FOLDER" >> build.env
 
+# Determine the Gitlab Templates version and store for later use.
+.templates-version: &templates-version
+  - |
+    echo "Executing curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/get-gitlab-templates-version.php"
+    curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/get-gitlab-templates-version.php
+    export GITLAB_TEMPLATES_VERSION=$(php get-gitlab-templates-version.php $_CURL_TEMPLATES_REF) && rm get-gitlab-templates-version.php
+    echo "GITLAB_TEMPLATES_VERSION=$GITLAB_TEMPLATES_VERSION" >> build.env
+
 # Display the Gitlab Templates version, the Composer version and some useful CI variables.
 .show-ci-variables: &show-ci-variables
-  - echo "Executing curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/extract-version.php"
-  - curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/extract-version.php
-  - php extract-version.php $_CURL_TEMPLATES_REF && rm extract-version.php
+  - echo "Gitlab Templates version $GITLAB_TEMPLATES_VERSION"
   - composer --version
   - |
     printf "CI_PIPELINE_SOURCE          = %s\n" $CI_PIPELINE_SOURCE &&
@@ -298,6 +304,7 @@ stages:
     - cd $CI_PROJECT_DIR && pwd
     - *create-environment-variables
     - *calculate-gitlab-ref
+    - *templates-version
     - *show-ci-variables
     - *show-environment-variables
     - printf "$DIVIDER\n* If you have defined _TARGET_PHP or _TARGET_DB_VERSION on your project, please rename them to be _TARGET_D7_PHP or _TARGET_D7_DB_VERSION.$DIVIDER\n"
diff --git a/includes/include.drupalci.main.yml b/includes/include.drupalci.main.yml
index 7876647a60b680339b16e25e7538c941d8626cd4..5f8bc061c8b0db4b77f756852f6ed467c6395b4d 100644
--- a/includes/include.drupalci.main.yml
+++ b/includes/include.drupalci.main.yml
@@ -108,11 +108,17 @@
     export DRUPAL_PROJECT_FOLDER=$CI_PROJECT_DIR/$_WEB_ROOT/$DRUPAL_PROJECTS_PATH/$CI_PROJECT_NAME
     echo "DRUPAL_PROJECT_FOLDER=$DRUPAL_PROJECT_FOLDER" >> build.env
 
+# Determine the Gitlab Templates version and store for later use.
+.templates-version: &templates-version
+  - |
+    echo "Executing curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/get-gitlab-templates-version.php"
+    curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/get-gitlab-templates-version.php
+    export GITLAB_TEMPLATES_VERSION=$(php get-gitlab-templates-version.php $_CURL_TEMPLATES_REF) && rm get-gitlab-templates-version.php
+    echo "GITLAB_TEMPLATES_VERSION=$GITLAB_TEMPLATES_VERSION" >> build.env
+
 # Display the Gitlab Templates version, the Composer version and some useful CI variables.
 .show-ci-variables: &show-ci-variables
-  - echo "Executing curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/extract-version.php"
-  - curl -OL https://git.drupalcode.org/$_CURL_TEMPLATES_REPO/-/raw/$_CURL_TEMPLATES_REF/scripts/extract-version.php
-  - php extract-version.php $_CURL_TEMPLATES_REF && rm extract-version.php
+  - echo "Gitlab Templates version $GITLAB_TEMPLATES_VERSION"
   - composer --version
   - |
     printf "CI_PIPELINE_SOURCE          = %s\n" $CI_PIPELINE_SOURCE &&
@@ -431,6 +437,7 @@ stages:
     - cd $CI_PROJECT_DIR && pwd
     - *create-environment-variables
     - *calculate-gitlab-ref
+    - *templates-version
     - *show-ci-variables
     - *show-environment-variables
     - *add-composer-drupal-lenient
diff --git a/scripts/extract-version.php b/scripts/get-gitlab-templates-version.php
similarity index 95%
rename from scripts/extract-version.php
rename to scripts/get-gitlab-templates-version.php
index 1c445b70d9f5e99e0aa07fb3c111a9ae253a9c50..0dd0057d1f1bd55af5845d50b254bc0d870fcbbb 100644
--- a/scripts/extract-version.php
+++ b/scripts/get-gitlab-templates-version.php
@@ -67,5 +67,6 @@ if (isset($tags[$current_ref])) {
 
 $version = $found_tag ? $found_tag : "$current_ref (branch)";
 
-echo "GitLab Templates version: $version\n";
+// This output is stored in the GITLAB_TEMPLATES_VERSION environment variable.
+echo $version;
 exit;