0

I'm trying to use yarn bin lerna command in pipeline for jenkins but it fails as it tries to fetch/update package git repository with ssh credentials.

Using below Jenkinsfile in multibranch project.

stages {
    stage('Install NPM Deps') {
        steps {
            configFileProvider([configFile(
                fileId: 'npm-auth',
                targetLocation: '.npmrc'
            )]) {
            withCredentials([sshUserPrivateKey(credentialsId: 'cred-id', keyFileVariable: 'KEY')]) {
            withEnv(['GIT_SSH=ssh -i ${KEY} -l git']) {
                script {
                    docker.image("node").inside("-u0 -vnode_home:/.npm") {
                        sh 'yarn'
                        sh 'yarn clean --yes'
                        sh 'yarn bootstrap'
                        sh '$(yarn bin lerna) version ${VERSION}'    
                        
                      //sh 'yarn test'
                      //sh 'yarn deploy'

                    }
                }
            }
        }
    }
}

Error when running pipeline.

+ yarn bin lerna
+ /var/jenkins/workspace/test-lib/node_modules/.bin/lerna version patch
lerna notice cli v3.22.1
lerna info versioning independent
lerna info ci enabled
lerna ERR! Error: Command failed: git remote update
lerna ERR! error: cannot run ssh -l git: No such file or directory
lerna ERR! fatal: unable to fork
lerna ERR! error: Could not fetch origin
lerna ERR! 
lerna ERR! Fetching origin
lerna ERR! lerna Command failed: git remote update
lerna ERR! lerna error: cannot run ssh -l git: No such file or directory
lerna ERR! lerna fatal: unable to fork
lerna ERR! lerna error: Could not fetch origin
lerna ERR! lerna 
lerna ERR! lerna Fetching origin
lerna ERR! lerna 

I've tried creating ~/.ssh/id_rsa file out of the key but still fails.

UPDATE: After updating GIT_SSH to GIT_SSH_COMMAND error has changed.

lerna info versioning independent
lerna info ci enabled
lerna ERR! Error: Command failed: git remote update
lerna ERR! Host key verification failed.
lerna ERR! fatal: Could not read from remote repository.
lerna ERR! 
lerna ERR! Please make sure you have the correct access rights
lerna ERR! and the repository exists.
lerna ERR! error: Could not fetch origin
lerna ERR! 
lerna ERR! Fetching origin
lerna ERR! 

I'm guessing it is due to prompt of adding host to known host file.

UPDATE So as predicted adding GIT_SSH_COMMAND=ssh -i ${KEY} -o "StrictHostKeyChecking no" -l git solved the issue.

1 Answer 1

1

The environment variable GIT_SSH (as distinct from GIT_SSH_COMMAND) cannot have options. See the main git documentation, which says in part:

GIT_SSH, GIT_SSH_COMMAND
...
$GIT_SSH_COMMAND takes precedence over $GIT_SSH, and is interpreted by the shell, which allows additional arguments to be included. $GIT_SSH on the other hand must be just the path to a program which can be a wrapper shell script, if additional arguments are needed).

Clearly you wanted to use GIT_SSH_COMMAND, not GIT_SSH, here.

That said, there's something else odd here:

withEnv(['GIT_SSH=ssh -i ${KEY} -l git']) {

Why is the -i ${KEY} missing?

1
  • Yeah I see will try with GIT_SSH_COMMAND but regarding -i ${KEY} I'm also not sure why error output is not showing that. May 23, 2022 at 16:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.