how to use gitlab repo in NPM with package.json

2019-05-07 06:40发布

问题:

I'm working on a internal project and want to share a tools inside the group.

but if I use the npm tools. it either request the module has registered on npm website or public git repo.

I'm using gitlab. and there is a private token. git+https://gitlab-ci-token:\<private token>@<domain>/<username>/<repo_name>.git i tried to put it like this in package.json but it's not working.

Is there a way to install a private gitlab repo with authorization?

thanks a lot.

回答1:

If I understand your question correctly, you want your CI pipeline which runs npm to be able to pull a private project from your gitlab as a dependency.

In that case, you can use the deploy keys mechanism in gitlab for that (see here).

First generate a SSH key pair. You can use ssh-keygen -t rsa for that.

Then go to the private repository's gitlab page and locate the Deploy Keys setting. There you should paste the public key you just generated.

Then go to the project that runs npm in its CI and locate the Variables page. Create a new private variable with the name SSH_PRIVATE_KEY for instance and paste the private key you generated there.

Finally, in your .gitlab-ci.yml file add the following so that your private key will be available to your CI environment:

- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
# Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

Your CI environment should now be setup so that in can pull your private repository.