Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active November 2, 2023 15:11
Show Gist options
  • Save eschwartz/8518b21d5d2854d8a4f7c7e7507f0561 to your computer and use it in GitHub Desktop.
Save eschwartz/8518b21d5d2854d8a4f7c7e7507f0561 to your computer and use it in GitHub Desktop.
npm install from private repo, in docker build
# Add these lines to your dockerfile, before `npm install`
# Copy the bitbucket private key to your docker image
COPY ./bitbucket_ssh_key /opt/my-app
# Copy the ssh script to your docker image
COPY ./ssh-bitbucket.sh /opt/my-app
# Tell git to use your `ssh-bitbucket.sh` script
ENV GIT_SSH="/opt/map-project-tile-server/ssh-bitbucket.sh"
RUN npm install
# Remove the private key once npm install is complete
# To previous any nefarious activities
RUN rm ./bitbucket_ssh_key

Goal: Install a package from a private bitbucket repository, using npm, from within a docker build script.

Steps:

  • Add a project from a private repo to your package.json. For example: git+ssh://[email protected]/hamweather/private-repo
  • Generate ssh keys using ssh-keygen (see https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html)
  • Save the public key to the bitbucket repo you want to install (see Settings > Deployment Keys)
  • Commit the private key to your repo
  • Commit the ssh-bitbucket.sh script to your repo
  • Add executable permissions to ssh-bitbucket.sh
  • Set permissionson the private key to 0600
  • Update your Dockerfile, as described below
#!/usr/bin/env bash
# http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use/920849#920849
ssh -i /opt/my-app/bitbucket_ssh_key \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
$*
@buksy90
Copy link

buksy90 commented Jul 30, 2019

At line 4 we are copying bitbucket_ssh_key

COPY ./bitbucket_ssh_key /opt/my-app

But at line 16, we are deleting bitbucket_rsa

RUN rm ./bitbucket_rsa

Is that an error? Should have been there "rm ./bitbucket_ssh_key" ?

@eschwartz
Copy link
Author

At line 4 we are copying bitbucket_ssh_key

COPY ./bitbucket_ssh_key /opt/my-app

But at line 16, we are deleting bitbucket_rsa

RUN rm ./bitbucket_rsa

Is that an error? Should have been there "rm ./bitbucket_ssh_key" ?

Sure looks like an mistake to me. Thanks -- fixed it.

@tigran-gruv
Copy link

@Aposhian
Copy link

Aposhian commented May 11, 2020

Commit the private key to your repo

That sounds like a bad idea.

Copy link

ghost commented Sep 29, 2021

I tried this and works like a charm on my local machine. But somehow it does not work with github actions. I keep getting following error (repo url updated to prevent leak):

npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://[email protected]/My-Organization/My-Repository.git
npm ERR! Warning: Permanently added 'github.com,140.82.113.4' (RSA) to the list of known hosts.
npm ERR! [email protected]: Permission denied (publickey).
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.

Any idea why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment