Fix GitLab runner "expected shallow list" error

Problem:

 Running with gitlab-runner 12.3.0 (a8a019e0)
   on dck02-dev.some.corp CFj3yCcn
Using Shell executor...
Running on dck02-dev.some.corp...
Fetching changes with git depth set to 50...
 Reinitialized existing Git repository in /home/gitlab-runner/builds/CFj3yCcn/0/myproject/poc-service/.git/
 fatal: git fetch-pack: expected shallow list
 ERROR: Job failed: exit status 1

Apparently the git version on the CentOS7 based runner host was quite outdated:

$ git --version
git version 1.8.3.1

This version doesn’t support commands like git fetch-pack. In order to fix this we have two options as noted on the official git website:

Red Hat Enterprise Linux, Oracle Linux, CentOS, Scientific Linux, et al.

RHEL and derivatives typically ship older versions of git. You can download a tarball and build from source, or use a 3rd-party repository such as the IUS Community Project to obtain a more recent version of git.

Source: https://git-scm.com/download/linux

I’ll cover option 2 below in detail using the git package from the IUS Community Project which requires the epel-release package so install it if you haven’t already:

If you don’t have extras repository enabled or you don’t want to enable extras repository for installing only one package, then you can just temporarily enable extras repository and install epel-release with the following command:

yum --enablerepo=extras install epel-release

Then follow these steps:

# First, uninstall the original git package
# Note that this will also remove the gitlab-runner package because it has defined git as dependency
yum remove git

# Install the ius repository
rpm -U https://centos7.iuscommunity.org/ius-release.rpm

# Install the IUS version of the git package
yum install git2u

# Finally, reinstall and enable the gitlab-runner:
yum install gitlab-runner
systemctl enable gitlab-runner
systemctl start gitlab-runner

The git version will be up-to-date and subsequent GitLab pipeline jobs will succeed:

$ git --version
git version 2.16.6