git pull not executing through a webhook in bash script - node.js

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.

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.

Related

How to change the user to make pull requests in Bitbucket using jenkins

I have a pipeline to create two new branches and push them to BitBucket, then create a new pull request.
The jenkins pipeline is running on a linux slave.
I've setup the credential to bitbucket repo in jenkins credentials.
So far creating new branches and pushing them to BitBucket works finely with my credentials.
sh script: "git init"
sh script: "git add *"
sh script: 'git commit -m "new branch created"'
sh script: "git checkout -b ${new_branch}"
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'mycred',
usernameVariable: 'username',
passwordVariable: 'pass']]){
sh('git push https://${username}:${pass}#bitbucket.example.com:8080/scm/user/reponame ${new_branch})
Like this I create and push two new branches ${new_branch}, ${new_branch2}. Even though only my credentials mycred has access to the repo, two branches gets pushed with cloud user. In commit section it shows "cloud user"
Then I'm trying to create a pull request from ${new_branch} to ${new_branch2}.
When I try to create a pull request with my credentials using a curl, it says
{"errors":[{"context": null,"message":"Authentication failed. please check your credentials and try again.","exceptionName":"com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"}]}
My curl command using bitbucket api is,
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'mycred',
usernameVariable: 'username',
passwordVariable: 'pass']]){
sh """curl --insecure -X POST https://${username}:${pass}#bitbucket.example.com:8080/rest/api/1.0/repos/myrepo/pull-request?create\
-H 'Content-Type:application/jason'\
-u ${username}:${pass}\
-d '{"title":"testpullreq",\
"description":"sampledescription",\
"state":"open",\
"open":true,\
"closed":false,\
"fromRef":{\
"id":"refs/heads/${new_branch}",\
"repository":{\
"slug":"branch-to-pr",\
"name":"branch-To-PR",\
}\
},\
"toRef":{\
"id":"refs/heads/${new_branch2}",\
"repository":{\
"slug":"branch-to-pr2",\
"name":"branch-To-PR2",\
}\
},\
"locked":false,\
"reviewers":[\
{\
"user":{\
"name":"myemail#example.com"\
}\
}\
]\
}' """
So curl command also works fine, the error is it says I don't have authentication permission, and that's because the PR tries to get made using my slave. I need to do this with my user account (mycred).
I'm assuming your issue occurs because curl is a command that runs within the Jenkins host. Therefore, it uses the credentials already present in the host when running the command.
You can consider using the HttpRequest plugin instead. The plugin allows you to use your specified credentials to send the post request with a body. The difference is that no system commands are used, so the command is sent independent of the credentials already present in the host. This should force Jenkins to use your mycred credentials.

Git push send parallel copy to test server

I search the method to copy files to "test" host when I launch the "git push" command.
git push ------- TO REPO --> REPO_SERVER
\
\_________ TO DIR --> Host_TEST
Git Version: 2.20.1
It sounds like you are trying to (re) invent CI/CD.
If you are using GitHub or GitLab as a remote server you can use Pipelines (or Actions in GitHub).
In there you can define (almost) anything you want to happen after a push, I am assuming your Host_TEST is accessible online.
In case you are running your own git server
You can implement "push to deploy" using the post-receive hook. Hooks are scripts that are placed inside .git/hooks and executed at a precise phase of a git command. you can find several example implementations in .git/hook. See here for more information: Setting up Push-to-Deploy with git
In case you don't have access to your own git server
You can use the pre-push script on your local machine, BUT THIS IS A BAD IDEA
This hooks is executed after you execute git push but before git actually pushes anything. If your script fails (i.e non-zero return code) it will not push.
Also, if your script manages to copy but then git fails to push you will end up testing code that's not in your repo.
If all this sound way too complicated
You can create a bash function that does both operations and add it to your .bashrc.
Here is an example:
push_copy() {
if git push
then
# Copy for command here: scp ...
else
echo "Failed..."
fi
}

Node.js Child Processes failing GitHub ssh authentication

Goal
Hello, I am creating a Node.js application to update my code when a push is made automatically.
Problem
Everything works in it, except, the actual git pull. The repo is private and needs to use ssh, however when I use the same command in terminal it works. I have keychaining on so it doesn't ask for my passphrase. Any ideas and how to fix this?
Relevant code
const exec = require("child_process").exec;
exec('cd ' + repo + ' && git pull origin deployment', (egitpull,stdoutgitpull,stderrgitpull)=>{
if(egitpull) return console.error(`git pull exec error:${egitpull}`)
console.log(`git pull stdout: ${stdoutgitpull}`);
console.log(`git pull stderr: ${stderrgitpull}`);
Command run:
cd /mp/ && git pull origin deployment
Out of child process vrs. in child process
Edit:
Removing the passphrase from the key entirely does seem to solve the issue, but I would much prefer having it in there for security reasons.
If you are using an ssh-agent for your ssh keys, try forwarding the SSH_AUTH_SOCK and SSH_AGENT_PID env variables to the child process, like this:
exec('cd ' + repo + ' && git pull origin deployment',
{
env: {
SSH_AUTH_SOCK: process.env.SSH_AUTH_SOCK,
SSH_AGENT_PID: process.env.SSH_AGENT_PID
}
},
(egitpull,stdoutgitpull,stderrgitpull) => {
// ...
});

Get git remote info using NodeJS

I have to get remote git information using NodeJS. I have managed to get info from a cloned repo using simple-git. The code I have test is the following:
require('simple-git')('/my/local/git/repo/path')
.pull()
.tags(function(err, tags) {
console.log("These are my tags: %s", tags.all);
});
However, it requires the repo to be locally cloned. Is there any way (using this or another module) to connect the remote git to get this info?
You need to specify the path to any valid repo or leave it empty considered git repo is under current directory. That's just required to make commands worked.
After that you can use listRemote method in the following way:
require('simple-git')([optional path])
// .init() - in case it's totally empty folder
.addRemote('remote_repo_alias', 'path/to/remote/repo')
.listRemote(['--tags', 'remote_repo_alias'], function(err, tags) {
// ...
});

Why does git clone hang when calling from a shell script which SSH's to a remote server?

I am writing a small app for those in the company who don't like command line and to help with productivity. The app runs a shell script which gets input from the user, uses it to SSH to a remote staging server and git clone the repo to that remote server. SSH keys are all good for the staging server and on Bitbucket. I can manually SSH into the server and git clone without any issue. I have also managed to get it to work going to a live server for initial setup. All other commands sent to the server are working.
The code used for this particular part is:
ssh $SUSER#$SHOST "git clone --depth 1 git#bitbucket.org:team/$SITEADDRESS.git --branch staging $CURRDIR"
I've also tried to run this manually from terminal with the following code:
ssh user#server "GIT_CURL_VERBOSE=1 GIT_TRACE=1 git clone -v --depth 1 git#bitbucket.org:team/repo.git --branch staging /path/to/folder"
This is the output I get:
trace: built-in: git 'clone' '-v' '--depth' '1' 'git#bitbucket.org:team/repo.git' '--branch' 'staging' '/path/to/folder'
Cloning into '/path/to/folder'...
trace: run_command: 'ssh' 'git#bitbucket.org' 'git-upload-pack '\''team/repo.git'\'''
trace: run_command: '--shallow-file' '/path/to/folder/.git/shallow.lock' 'index-pack' '--stdin' '--fix-thin' '--keep=fetch-pack 557 on ubuntu'
trace: exec: 'git' '--shallow-file' '/path/to/folder/.git/shallow.lock' 'index-pack' '--stdin' '--fix-thin' '--keep=fetch-pack 557 on ubuntu'
trace: built-in: git 'index-pack' '--stdin' '--fix-thin' '--keep=fetch-pack 557 on ubuntu'
At this point it just hangs without errors instead of moving on to remote: Counting objects: 4535, done. and cloning the repo.
I've been trying to get this working for quite a few hours now and have been unable to find any info on similar problems. Using Git v1.9.1

Resources