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
sudo yum remove git

# Install the ius repository (new URL!)
sudo yum install -y https://repo.ius.io/ius-release-el7.rpm

# Install specific IUS version of the git package
# Note the hardcoded version in the package name. For details check:
# - https://github.com/iusrepo/wishlist/issues/241#issue-454462839
# - https://github.com/iusrepo/git222/issues/6
# To always install the latest IUS version of git run the following instead:
#   sudo yum install --exclude git git
sudo yum install git222

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

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

$ git --version
git version 2.22.2

Edit:

Originally this article installed the IUS repository via:

rpm -U https://centos7.iuscommunity.org/ius-release.rpm
yum install git2u

But now you’ll receive the following error when using that domain:

Complete!
Loaded plugins: fastestmirror, ovl
Cannot open: https://centos7.iuscommunity.org/ius-release.rpm. Skipping.
Error: Nothing to do

The reason is that the redirect of iuscommunity.org was removed at 2020-06-01.

See: https://github.com/iusrepo/announce/issues/18