7

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.

Tomi Li
  • 93
  • 1
  • 5

1 Answers1

10

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 tokens setting in: Settings > Repository. 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.

Clockwork
  • 245
  • 1
  • 4
  • 17
Jawad
  • 4,457
  • 1
  • 26
  • 29
  • thank you for you reply. but what i really want to ask is . I have a project with a package.json file. how could i set up a dependancy with correct schema or url with private gitlab repo – Tomi Li Apr 06 '17 at 09:30
  • If you do what's decribed in my answer, then you can add your private repository in your package.json with `"git+ssh://git@//.git"` – Jawad Apr 06 '17 at 10:03
  • @Jawad what username should be used in a Gitlab CI pipeline? – 7hibault Apr 06 '17 at 10:05
  • @7hibault The ssh user to connect to a gitlab repo is always git so a SSH URL will always look like `git@//.git` – Jawad Apr 06 '17 at 10:08
  • In both of my comments above, it should read `git@:/.git` (notice the `:` instead of the `/`between the domain and username) – Jawad Apr 06 '17 at 11:36
  • @Jawad I am using MAC 1) can you detail about the variable page: "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." 2) when you said private key you mean something like a4:r3:......, the one that is shown key that is shown when you submit the public key? 3) By the way I did not find .gitlab-ci.yml in my project, so created one in the main folder where gitigonre resides.. Did i do correct? – Sharan Ainapurapu Dec 10 '17 at 04:27
  • @Jawad I am trying to setup dev environment so that other developers in project can use the same package.json and just do npm install all the packages. – Sharan Ainapurapu Dec 10 '17 at 05:03
  • @Jawad I am giving my email id as user name.. Is that correct? Sorry for posting multiple times. I got the doubts subsequently and I am not allowed to edit after 5 minutes – Sharan Ainapurapu Dec 10 '17 at 05:10
  • After re-reading the answer, I just realised that my edit suggestion actually wasn't relevant. In fact, there is indeed a section called "Deploy keys" in "Settings > Repository", and my edit only made the answer confusing. If someone could revert my edit so that I don't get +2 reputation for fixing my own mistake, that would be great. – Clockwork Jun 16 '22 at 09:38