0

I have a node server running on ec2 in ubuntu which should update when I push into master as I have created a hook for that in Gitab integrations.

I saw the hook working through the logs and executing every command expect simple git pull.

I have checked many similar questions which have suggestions like appending env -i to reset the GIT_DIR so that the command can execute but no luck so far.

I tried executing different commands like git status and they are executing through the hook in bash script normally.

Here is my script which is in my home folder along with the repository:

#!bin/bash
cd toTheFolder
git pull

here is the end-point which executes the script

childProcess.exec(
            "bash temp.sh",
            { cwd: "/home/ubuntu/repoFolder" },
            function(err, stdout, stderr) {
                console.log(stdout, stderr);
                if (err) {
                    return res.status(500).send(err);
                }
                res.status(200).send("OK");
            }
        );

The error it returns is {"killed":false,"code":1,"signal":null,"cmd":"bash temp.sh"}

Any thoughts on why simple git pull is not working would be a huge help.

-Thanks

EDIT: here is the output of stdout

git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
sidd
  • 629
  • 10
  • 30
  • 1) Change the shebang to `#!/bin/bash`. 2) Try `git --git-dir=path/to/repo/.git --work-tree=path/to/repo pull ` explicitly instead of `cd` and `git pull`. – ElpieKay Mar 19 '19 at 09:30
  • Tried both... same error – sidd Mar 19 '19 at 09:38
  • Can `git pull` succeed if you manually run it in the ubuntu repository? – ElpieKay Mar 19 '19 at 09:48
  • If I manually execute the script it runs without issues... issues like this arise when I do it though a hook... just did an update to the question. – sidd Mar 19 '19 at 09:51
  • A similar question had been raised earlier where the issues occured while executing `git push`. Check answer on https://stackoverflow.com/questions/48941193/node-js-exec-git-command-error-permission-denied-publickey – Marcus Mar 19 '19 at 10:06
  • 1
    It seems the hook runs the script as a different user from yours. If so, you need to add the public ssh key of the hook user to the github account settings. – ElpieKay Mar 19 '19 at 10:20
  • @ElpieKay you were right. the webhook endpoint was running on a different node server on a different port as I have to kill the node server and restart it so I made the hook on diff server. I was starting the other server using sudo which was not the user which generated the ssh for the Gitlab hence it was not able to pull. Thanks for your great insight. – sidd Mar 19 '19 at 12:20

1 Answers1

0

Use the following command

git pull https://username:password@mygithost.com/my/repository

Remember to replace the [username:password] with your git credentials, [mygithost.com] with your git host ( gitlab, ..etc), [my/repository] with your repository url

It would not work for github anyway cause they removed the username/password authentication support.

For github please check your ssh connection between your server and github.