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?
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With