9

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?

1
  • can you possibly add the public key on your jenkins slaves to the hosted git repo attached to a user that has access? Commented May 3, 2017 at 14:31

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I found this helpful. Thanks for the answer. But there is a fix in this. It's not cat but echo.
@MuhammedKK thanks. i've updated the snippet to use echo.
1

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.

Comments

0

Very usefule info is that npm apparently also can use the GIT_SSH_COMMAND environment variable. So you could specify an ssh credential like this:

export GIT_SSH_COMMAND='ssh -i ~/.ssh/your_private_key'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.