Npm install package from private gitlab repository from inside a CI pipeline

Hi.

We have a number of nodejs packages that are dependant on some private libraries. All of these packages and libraries are hosted in the same private GitLab self hosted service.

What we are trying to do is run gitlab-ci tests on the services but we get errors during the npm install phase as we need an ssh key to install the private libraries. I found some information on doing something similar with submodules but I can’t find anything specific to nodejs and npm.

Example:

I have a project hosted at git@gitlab.example.com:js-libs/eg-parent-project.git
That has a dependency in the package.json that looks like "eg-child-library": "git+ssh://git@gitlab.example.com:js-libs/eg-child-library"

The ci pipeline then failed with the following error

$ npm ci
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t git@gitlab.example.com:js-libs/eg-child-library
npm ERR! 
npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR! 
npm ERR! exited with error code: 128

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-10-23T12_18_23_448Z-debug.log

Thanks for any help

Hi, Do you resolve it?
I have the same problem!

Simply use HTTPS inside your pipelines with an access token.

  • Generate an access_token, for only one project or for a group of projects

  • Add a this token as TOKEN_NAME:TOKEN_VALUE inside a ci varible

  • Then use those command before installing your npm dependencies.

- git config --global url."https://".insteadOf "git://"
- git config --global url."https://$GITLAB_ACCESS_TOKEN@$CI_SERVER_HOST/".insteadOf "git@$CI_SERVER_HOST:"
- npm install

So I ended up using a mix of olive007’s answer and this SO answer git - Using GitLab token to clone without authentication - Stack Overflow was the last piece of the puzzle.

The code ended up looking like

    - git config --global url."https://".insteadOf "git://"
    - git config --global url."https://oauth2:$GITLAB_ACCESS_TOKEN@$CI_SERVER_HOST/".insteadOf 
    "git@$CI_SERVER_HOST:"
    - npm install