如何在github动作工作流ci中通过npm安装私有github存储库

问题描述:

我试图通过运行npm install在github工作流ci中安装npm依赖项.但是我收到以下错误:

I am trying to install npm dependencies within a github workflow ci by running npm install. However i get the following error:

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git@github.com/private-org/private-repo.git
npm ERR! 
npm ERR! Warning: Permanently added the RSA host key for IP address 'removed' to the list of known hosts.
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.

ci.yml

name: CI

on:
  push:
    branches: [master ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'
    - run: node --version
    - run: npm install

package.json

  ...
  "dependencies": {
    "some-pacakage": "git+ssh://git@github.com/private-org/private-repo.gitt",
  },
  ...

some-package由npm通过github安装.回购与工作流所在的组织位于同一组织内.要在本地解决此问题,请在与该组织绑定的github帐户上设置ssh密钥.

This some-package is being installed via github by npm. The repo is within the same organization as which the workflow is running in. To solve this problem locally you setup ssh key on your github account tied to the organization.

但是我该如何解决这个问题,以便它能够在不使用我的个人github帐户的情况下,通过工作流ci中的github repo安装该软件包.

But how can i solve this issue so that its able to install that package via github repo within the workfow ci where im not using my personal github account.

私有存储库是通过ssh安装的.如果您在管道中设置了ssh密钥,则在尝试安装时将使用该ssh密钥.

The private repository is being installed via ssh. If you set an ssh key in the pipeline it will use that ssh key when attempting to install.

幸运的是,有一个github动作允许我们执行 https://github.com/webfactory /ssh-agent

Fortunately there is a github action that allows us to do that https://github.com/webfactory/ssh-agent

在npm install上方添加以下内容:

Above npm install add the following:

  - uses: webfactory/ssh-agent@v0.2.0
  with:
    ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} 

设置/先决条件

https://github.com/webfactory/ssh-agent#usage

  1. 创建具有足够访问权限的SSH密钥.出于安全原因,请勿使用您的个人SSH密钥,而是设置专用的SSH密钥 供GitHub Actions使用.如果不确定,请参阅以下一些提示 关于这一步.

  1. Create an SSH key with sufficient access privileges. For security reasons, don't use your personal SSH key but set up a dedicated one for use in GitHub Actions. See below for a few hints if you are unsure about this step.

确保您没有在私钥上设置密码.

Make sure you don't have a passphrase set on the private key.

在您的存储库中,转到设置">秘密"菜单,然后创建一个新的秘密.在此示例中,我们将其称为SSH_PRIVATE_KEY.放在 私有SSH密钥文件的内容放入content字段.这个钥匙 应该以----- BEGIN ... PRIVATE KEY -----开头,包含许多 行并以----- END ... PRIVATE KEY -----结尾.

In your repository, go to the Settings > Secrets menu and create a new secret. In this example, we'll call it SSH_PRIVATE_KEY. Put the contents of the private SSH key file into the contents field. This key should start with -----BEGIN ... PRIVATE KEY-----, consist of many lines and ends with -----END ... PRIVATE KEY-----.