0

I have a problem running a shell-script which basically commits changes within a directory to a remote git-repository from within another script. The "super-script" is being run with superuser-permissions, and I have the needed ssh-keys generated at ~/.ssh/ in my super user's home directory. Also, running the inner script standalone works fine and pushes to the remote repository.

This is the error I get when running the super-script:

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 326 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@url.goes.here:groupspace/projectspace.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@url.goes.here:groupspace/projectspace.git'

and I call the inner script with this line of code, saving its exit-status for further processing

SUCCESS = $(bash /path/to/push-config.sh)

This is the content of push-config.sh

#!/bin/bash
# 
stamp=$(date +"%Y-%m-%d %T")

git add .
git commit -m "Config $stamp"

if [ $? -eq 0 ]
then
  git push -u origin master
  if [ $? -eq 0 ]
  then
    echo "Successfully pushed config to repo."
    exit 0
  else
    echo "Failure while pushing to repo."
    exit 1
  fi
else
  exit 1
fi

Any help appreciated, thanks in advance.

Daniel
  • 578
  • 6
  • 18
  • `running the inner script standalone works fine`: did you run it as super user/root? maybe showing your push-config.sh might also help – ikkentim Jul 10 '19 at 07:51
  • @ikkentim yes I ran it as root and it worked all fine. Will add the push-config.sh – Daniel Jul 10 '19 at 07:53
  • This is the error "remote: GitLab: You are not allowed to push code to protected branches on this project." – Vorac Jul 10 '19 at 07:59
  • @vorac I know that this is the error, yet I don't know why I am getting it. I am allowed to push code to protected branches on the said project, I have the necessary keys and the script on its own is working fine. – Daniel Jul 10 '19 at 08:01

2 Answers2

1

Since you're declined by the git pre-receive hook, the hook validates you on something you haven't set correctly in your super-script or the environment you run your superscript in.

Do a comparison of the commits created when you run the superscript and the inner scripts. Make sure they're both identical.

Next, ensure your git environment is identical for both pushes. By this, I mean that the git user variable should be the same, and that your current working directory should match the repository.

You'd have an easier time debugging this if you checked exactly what the pre-receive hook validates.

Niklas
  • 61
  • 4
  • Thank you for this reply @Niklas, that one might actually point in the right direction. Since the error-message indicates the problem might be the pre-receive-hook, I'd need to find and check this hook on the remote-repository, which I can only access via our git's webpages. Do you know if it is even possible to access server-side hooks this way? – Daniel Jul 10 '19 at 08:31
0

The root user has their own home directory. I expect you have your ssh key in your own home folder (~/.ssh as you mentioned). Either add the ssh key to the .ssh folder in the home of the root user or see How to specify the private SSH-key to use when executing shell command on Git?

ikkentim
  • 1,639
  • 15
  • 30
  • Sorry, I wasn't being precise enough there. The ssh-keys are within .ssh in the root user's home directory. Edited my OP as well. – Daniel Jul 10 '19 at 07:49