Daniel
Daniel

Reputation: 618

Why can't I run this shell-script pushing to git from another shell script?

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 [email protected]:groupspace/projectspace.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]: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.

Upvotes: 0

Views: 429

Answers (2)

Niklas
Niklas

Reputation: 61

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.

Upvotes: 1

ikkentim
ikkentim

Reputation: 1648

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?

Upvotes: 0

Related Questions