Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git credentials when npm has a git dependency

Tags:

git

npm

jenkins

I have a Jenkins job to build my JS application.

In my package.json I have a dependency that looks like this:

"devDependencies": {
  "my_private_package": "git+https://my-server/my-repo.git#1.0.0"
}

I use the Jenkins Git Plugin along with the Credentials Plugin to clone the repo, then a shell script to run npm install.

When Jenkins runs npm install, npm errors out with npm ERR! fatal: Authentication failed

Due to our self hosted git server and bureaucracy I'm unable to do anything with adding an oAuth token to the git url.

Is there a way for me to set my git credentials so that npm can install from my password protected git repo?

like image 914
maafk Avatar asked May 03 '17 13:05

maafk


People also ask

Where do I find my git credentials?

The default path for the git credential store is $HOME/. git-credentials (or $XDG_CONFIG_HOME/git/credentials, if the previous location doesn't exist).

How do I fix git always asking for credentials?

Entering Git Username and Password in Remote URL To prevent Git from asking for your username and password, you can enter the login credentials in the URL as shown. The main drawback of this method that your username and password will be saved in the command in the Shell history file.


2 Answers

You can vend HTTPS credentials to git using the credential-helper config with a file. The file format is just an HTTPS URL with the user:password credentials part filled in. Something like:

CREDENTIALS_FILE_PATH="$HOME/.git/my-ci-credentials"
echo 'https://ci-user:[email protected]/' > "$CREDENTIALS_FILE_PATH"

Because npm clones the repo outside the context of your project folder, you'll need to specify this config at the user, rather than project, level:

git config --global credential.helper "store --file=$CREDENTIALS_FILE_PATH"

After this, npm should be able to clone the repo.

like image 67
Jeremy W. Sherman Avatar answered Oct 17 '22 00:10

Jeremy W. Sherman


I solved this by using the git+ssh way of installing the dependency, like git+ssh://my-server/my-repo.git#1.0.0

Then on the Jenkins .ssh folder add a config file with the content:

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/<rsa file>

Then Jenkins knows to use that ssh key for any github.com url. You need to have a Jenkins git user or deploy key associated with the project you are trying to import.

like image 21
Nicolas Avatar answered Oct 17 '22 02:10

Nicolas