diff --git a/templates/gitlab-ci-maven.yml b/templates/gitlab-ci-maven.yml
index 501a06600840154bf0eb43a3149a29592946de78..d450d09162cac77854f63e0eaa0e9e862af1ef27 100644
--- a/templates/gitlab-ci-maven.yml
+++ b/templates/gitlab-ci-maven.yml
@@ -473,6 +473,10 @@ mvn-build:
 mvn-sonar:
   stage: test
   extends: .mvn-base
+  variables:
+    # Tells git to fetch all the branches of the project, required by the analysis task
+    # see: https://docs.sonarqube.org/8.5/analysis/gitlab-cicd/#header-2
+    GIT_DEPTH: 0
   script:
     - sonar_autodetect_mr
     - if [[ "$SONAR_GITLAB_TOKEN" ]]; then sonar_extra_args="$SONAR_GITLAB_ARGS"; fi