diff --git a/.ci/prepare_git.sh b/.ci/prepare_git.sh
new file mode 100755
index 0000000000000000000000000000000000000000..8399de06ddc08bcfb643fbf837a8460469c5ca66
--- /dev/null
+++ b/.ci/prepare_git.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+eval $(ssh-agent -s)
+echo "$2" | tr -d '\r' | ssh-add - > /dev/null
+mkdir -p ~/.ssh
+chmod 700 ~/.ssh
+ssh-keyscan "$1" >> ~/.ssh/known_hosts
+git config --global user.email "$CI_GIT_USER_EMAIL"
+git config --global user.name "$CI_GIT_USER_USERNAME"
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index af85961fb90519ec064fba5d4b03caf4ea06bd89..ec958c73b18554e60ab292027bbf3a8715eb63f5 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -128,14 +128,8 @@ deploy-flatpak:
   rules:
     - if: '$CI_COMMIT_TAG'
   before_script:
-    - eval $(ssh-agent -s)
-    - echo "${FLATHUB_DEPLOY_KEY}" | tr -d '\r' | ssh-add - > /dev/null
-    - mkdir -p ~/.ssh
-    - chmod 700 ~/.ssh
-    - ssh-keyscan github.com >> ~/.ssh/known_hosts
+    - .ci/prepare_git.sh github.com ${FLATHUB_DEPLOY_KEY}
   script:
-    - git config --global user.email "$CI_GIT_USER_EMAIL"
-    - git config --global user.name "$CI_GIT_USER_USERNAME"
     - git clone git@github.com:flathub/org.gajim.Gajim.git
     - cd org.gajim.Gajim
     - mv ../flatpak/org.gajim.Gajim.yaml org.gajim.Gajim.yaml
@@ -152,14 +146,8 @@ publish-release:
   rules:
     - if: '$CI_COMMIT_TAG'
   before_script:
-    - eval $(ssh-agent -s)
-    - echo "${WEBSITE_DEPLOY_KEY}" | tr -d '\r' | ssh-add - > /dev/null
-    - mkdir -p ~/.ssh
-    - chmod 700 ~/.ssh
-    - ssh-keyscan dev.gajim.org >> ~/.ssh/known_hosts
+    - .ci/prepare_git.sh dev.gajim.org ${WEBSITE_DEPLOY_KEY}
   script:
-    - git config --global user.email "$CI_GIT_USER_EMAIL"
-    - git config --global user.name "$CI_GIT_USER_USERNAME"
     - git clone git@dev.gajim.org:gajim/website.git
     - cd website
     - git checkout master