William Forty
William Forty

Reputation: 252

npm install with git+ssh doesn't use existing ssh key for user

I want to reference a private GitHub repo in my project. I'm trying commands like:

npm i git+ssh://[email protected]/<myusername>/<myprivaterepo>.git

However it seems to not notice that I have SSH keys already configured on my (Windows 11) system. I can happily clone the repo, but executing the above command instead prompts me to sign in to GitHub:

GitHub Sign In

According to my searches online, this should work without additional authentication. For example, this article explains how to set this up - I can skip steps 2 and 3 as my SSH key already works (verified with ssh -T [email protected]), but I still get a prompt, and I don't want to add unnecessary additional access to my GitHub account where it isn't technically needed.

Does anyone know why I am getting this prompt?

Upvotes: 3

Views: 3630

Answers (2)

hyunfei
hyunfei

Reputation: 111

The reason is the git ssh private key is not stored on the jenkins host,you need use the private key from jenkins credential,with version 2.30+ git,git support use GIT_SSH_COMMAND to set private key,the code in jenkinsfile as bellow

 stage('install') {
                withCredentials([sshUserPrivateKey(credentialsId: "keyId", keyFileVariable: 'key')]) {
                // from git 2.30+,git support use GIT_SSH_COMMAND to set private key 
                sh 'GIT_SSH_COMMAND="ssh -i $key" npm install'
                }
     }

Upvotes: 0

William Forty
William Forty

Reputation: 252

I figured this out myself - it's apparently a common issue in a variety of contexts, but in my search around npm related commands, I didn't find anywhere referencing this happening with npm, hence my question.

Thanks to this comment I stumbled upon, I discovered there's a setting for controlling handling credentials. You can check its value with git config credential.helper. I was blissfully ignorant that this existed as I normally use SSH key pairs from the command line.

I solved my issue by turning off the credential helper completely for git on my system, with:

git config --system --unset credential.helper

Note that this required administrative privileges to do, as the relevant config file lives in the Program Files directory (C:\Program Files\Git\etc\gitconfig).

Upvotes: 6

Related Questions