GitHub Actions 在 npm 安装私有 GitHub repo 时抛出 Git 错误

GitHub Actions throws Git Error when npm installing private GitHub repo

我有一个包含一些测试工作流程的 GitHub 回购协议:

name: Tests the App

on:
  push:
    branches: [main]

jobs:
  test:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: "npm"
    - run: echo "Testing App."
    - run: npm install
    - run: echo "Using ESLint."
    - run: npm run lint
    - run: echo "Testing App."
    - run: npm run test
    - run: echo "Test completed!"

不幸的是,它抛出一个 git 错误,退出代码为 128:

npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/MYNAME/REPONAME.git
npm ERR! Warning: Permanently added the RSA host key for IP address 'SOMEIPADDR' to the list of known hosts.
npm ERR! git@github.com: 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.
[…]
Error: Process completed with exit code 128.

当它尝试 npm 安装依赖项时,有一个私人 GitHub Repo REPONAME 需要从我的帐户安装。

"dependencies": {
   "pckgname": "git+ssh://git@github.com:MYNAME/REPONAME.git#main"
}

在 ci/cd 环境中实现此功能的最佳方式是什么?

您可以使用 webfactory/ssh-agent@v0.5.3。您要做的第一件事是在 REPONAME.git 存储库中创建一个 SSH 密钥对(最好专用于 Github 操作),然后将私钥作为秘密放入 Github 操作中,为此例如,我们将其称为 SSH_PRIVATE_KEY,然后只需像这样更新您的工作流程:

 steps:
    - uses: actions/checkout@v2
    - uses: webfactory/ssh-agent@v0.5.3
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
    - ...

有关此操作的更多详细信息 here