lerna publish on Jenkins "git remote update" Fails "Could not read from remote repository."

I am trying to set up lerna with jenkins to publish after all my tests pass. I have the command running but it’s failing.

I can’t find any documentation on how to get this to work. I need this command to have access to push the lerna tags, as well as the updated version numbers to the master branch, and I also need Jenkins to not fall into an infinite loop.

My first issue was this:

> lerna publish --yes --cd-version=patch

lerna info version 3.0.0-beta.17
lerna info versioning independent
lerna ERR! ENOGIT Detached git HEAD, please checkout a branch to publish changes.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] publish: `lerna publish --yes --cd-version=patch`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] publish script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/lib/jenkins/.npm/_logs/2018-04-24T03_18_59_152Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] jenkins: `npm run versions && npm run test && npm run publish`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] jenkins script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/lib/jenkins/.npm/_logs/2018-04-24T03_18_59_167Z-debug.log
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Which I fixed by doing this:
Jenkins Git plugin detached HEAD

Here’s what I set everything to:

enter image description here

Now I am having this issue:

> lerna publish --yes --cd-version=patch

lerna info version 3.0.0-beta.17
lerna info versioning independent
lerna ERR! Error: Command failed: git remote update
lerna ERR! Permission denied (publickey).
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! 
lerna ERR!     at makeError (/var/lib/jenkins/workspace/abide/node_modules/@lerna/child-process/node_modules/execa/index.js:169:9)
lerna ERR!     at Function.module.exports.sync (/var/lib/jenkins/workspace/abide/node_modules/@lerna/child-process/node_modules/execa/index.js:338:15)
lerna ERR!     at Object.execSync (/var/lib/jenkins/workspace/abide/node_modules/@lerna/child-process/index.js:21:16)
lerna ERR!     at Object.isBehindUpstream (/var/lib/jenkins/workspace/abide/node_modules/@lerna/git-utils/index.js:206:25)
lerna ERR!     at PublishCommand.initialize (/var/lib/jenkins/workspace/abide/node_modules/@lerna/publish/index.js:92:24)
lerna ERR!     at Promise.resolve.then (/var/lib/jenkins/workspace/abide/node_modules/@lerna/command/index.js:228:24)
lerna ERR! lerna Command failed: git remote update
lerna ERR! lerna Permission denied (publickey).
lerna ERR! lerna fatal: Could not read from remote repository.
lerna ERR! lerna 
lerna ERR! lerna Please make sure you have the correct access rights
lerna ERR! lerna and the repository exists.
lerna ERR! lerna error: Could not fetch origin
lerna ERR! lerna 
lerna ERR! lerna Fetching origin
lerna ERR! lerna 
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] publish: `lerna publish --yes --cd-version=patch`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] publish script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/lib/jenkins/.npm/_logs/2018-04-24T03_29_06_133Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] jenkins: `npm run versions && npm run test && npm run publish`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] jenkins script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/lib/jenkins/.npm/_logs/2018-04-24T03_29_06_144Z-debug.log
Build step 'Execute shell' marked build as failure
Finished: FAILURE

How can I get lerna publish to work in Jenkins?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I ssh’d into the jenkins box and cd into the workspace.

I ran git remote update and it stdout this:

Warning: Permanently added the RSA host key for IP address ‘192.30.253.112’ to the list of known hosts.

It seems I didn’t have github’s IP added.

Solution 2

You need to make sure with which account Jenkins is running on the Jenkins box:

If you do have the right ~/.ssh/id_rsa(.pub) private/public key, Jenkins must run with that user for ~ to reflect the right HOME.

If you can connect to the Jenkins machine, you can then test your ssh connection with:

cd /apth/to/repo
git remote -v
  # should return an ssh URL
  # like:
  # [email protected]
ssh -T [email protected]

The OP ThomasReggi confirms the account issue in the comments with:

I have been SSH’d into the machine via root and can view access.
I am pulling the repo down with a user and have a key in Jenkins using the Git plugin, but now lerna is running outside that plugin and needs ssh access to the repo and it’s not available at the user level.

All I needed to do was add the SSH Agent plugin and in my job’s configure use the same SSH credentials I used to setup the GitHub plugin.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply