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

Viewed 2286

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
3 Answers

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.

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/

Changing the permissions helped!

sudo chown -R .git/

Related