'error: cannot open .git/FETCH_HEAD: Permission denied on gitlab-runner

I'm trying to pull code with gitlab-runner with a shell executor.

 $ git pull origin master
 error: cannot open .git/FETCH_HEAD: Permission denied
 ERROR: Job failed: exit status 1

git status seems to work fine but when I try git fetch

 $ git fetch
 error: cannot open .git/FETCH_HEAD: Permission denied
 ERROR: Job failed: exit status 1


Solution 1:[1]

This could be because you did not add a deploy key for your gitlab-runner user in your gitlab project, so the runner user does not have access to your gitlab repository.

You should follow theses steps to create the keypair for gitlab-runner user (the shell executor user) https://docs.gitlab.com/ee/ci/ssh_keys/#ssh-keys-when-using-the-shell-executor

Then copy the created public key in /home/gitlab-runner/.ssh/id_rsa.pub

And add it to your gitlab project's deploy key :

  • Go to you gitlab project
  • Settings => Repository
  • Deploy keys
  • Add a title, past the public key and click "Add key"

Trigger you CI again and the shell executor runner should be able to pull the repository.

Solution 2:[2]

I just solved this problem by running this command sudo chown -R $(whoami) .git/ in my project directory.

You can change $(whoami) to the username of your server. For example sudo chown -R gitlab-runner .git/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tchartron
Solution 2 Hasibur Rahman