diff --git a/.ci_templates/.preliminaries.yml b/.ci_templates/.preliminaries.yml
new file mode 100644
index 0000000000000000000000000000000000000000..c0f9a861efa4f7dec508a68e96a4f94ac25e2466
--- /dev/null
+++ b/.ci_templates/.preliminaries.yml
@@ -0,0 +1,25 @@
+.preliminaries_script:
+
+  ## Install ssh-agent if not already installed, it is required by Docker.
+  ## (change apt-get to yum if you use an RPM-based image)
+  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
+
+  ## Run ssh-agent (inside the build environment)
+  - eval $(ssh-agent -s)
+
+  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
+  ## We're using tr to fix line endings which makes ed25519 keys work
+  ## without extra base64 encoding.
+  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
+  - mkdir -p ~/.ssh
+  - chmod 700 ~/.ssh  
+  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
+  # - echo "$SSH_KNOWN_HOSTS" > $HOME/.ssh/known_hosts
+  - ssh-keyscan -H -p 2202 gitlab.iri.upc.edu >> $HOME/.ssh/known_hosts
+
+  # update apt
+  - apt-get update
+
+  # create 'ci_deps' folder (if not exists)
+  - cd ${CI_PROJECT_DIR}
+  - mkdir -pv ci_deps
\ No newline at end of file
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ea2f2bd27d98f6bddb4b0edc7d6782f57db86e9c..6d3a51538fddaaa90f57eb7894d785887e1a4f5e 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,6 +2,7 @@ include:
   - '.ci_templates/.license_headers.yml'
   - '.ci_templates/.yaml_schema_cpp.yml'
   - '.ci_templates/.clang_format.yml'
+  - '.ci_templates/.preliminaries.yml'
 
 workflow:
   rules:
@@ -54,28 +55,8 @@ stages:
   ## PRINT VARIABLES
   - *print_variables_definition
 
-  ## Install ssh-agent if not already installed, it is required by Docker.
-  ## (change apt-get to yum if you use an RPM-based image)
-  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
-
-  ## Run ssh-agent (inside the build environment)
-  - eval $(ssh-agent -s)
-
-  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
-  ## We're using tr to fix line endings which makes ed25519 keys work
-  ## without extra base64 encoding.
-  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
-  - mkdir -p ~/.ssh
-  - chmod 700 ~/.ssh  
-  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
-  # - echo "$SSH_KNOWN_HOSTS" > $HOME/.ssh/known_hosts
-  - ssh-keyscan -H -p 2202 gitlab.iri.upc.edu >> $HOME/.ssh/known_hosts
-
-  # update apt
-  - apt-get update
-
-  # create 'ci_deps' folder (if not exists)
-  - mkdir -pv ci_deps
+  ## preliminaries script (ssh, apt update and mkdir ci_deps)
+  - !reference [.preliminaries_script]
 
 .build_and_test_template: &build_and_test_definition
   - cd $CI_PROJECT_DIR