如何在 GitHub Actions 工作流程中通过 npm 安装私有的 GitHub 存储库

6
我正尝试通过运行npm install在GitHub Workflow CI中安装npm依赖项。然而,我遇到了以下错误:
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",
  },
  ...

通过npm在github上安装some-package。该仓库与工作流程所在的组织相同。要在本地解决此问题,您需要设置与该组织相关联的Github帐户的ssh密钥。

但是,在我不使用个人Github帐户的工作流CI中,如何解决此问题以便能够通过github存储库安装该软件包。

4个回答

7

私有仓库通过ssh安装。如果在流水线中设置了ssh密钥,则在尝试安装时将使用该ssh密钥。

幸运的是,有一个GitHub操作可以让我们做到这一点:https://github.com/webfactory/ssh-agent

在npm install之前添加以下内容:

  - 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密钥,而是设置专用密钥 以在GitHub Actions中使用。如果您对此步骤不确定,请参阅下文中的一些提示。

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

  3. 在存储库中,前往“设置”>“Secrets”菜单并创建一个新的secret。在本示例中,我们将其称为SSH_PRIVATE_KEY。将私人SSH密钥文件的内容放入内容字段。该键 应以-----BEGIN ... PRIVATE KEY-----开头,由许多行组成,并以-----END ... PRIVATE KEY-----结尾。


6

标准令牌权限不足:

该令牌的权限限于包含您的工作流程的存储库。更多信息,请参见"GITHUB_TOKEN权限"

您需要手动创建一个可访问软件包的个人访问令牌:

如果您需要一个需要超出权限的令牌,您可以创建一个个人访问令牌并将其设置为存储库中的密钥:
  1. 使用或创建具有该存储库所需权限的令牌。有关更多信息,请参见"为命令行创建个人访问令牌"
  2. 将令牌作为工作流程存储库中的秘密添加,并使用${{ secrets.SECRET_NAME }}语法引用它。有关更多信息,请参见"创建和使用加密密钥"
来源: https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token

3

当我从Travis迁移到GitHub Actions时,遇到了类似的问题。

基本上你需要做的就是确认 Git 如何获取你的远程仓库。在 package.json 中,使用了 "git+ssh://git@github.com/private-org/private-repo.gitt"。因此它试图使用ssh访问密钥来获取仓库,如果你没有添加访问密钥,它将会失败。

相反,我们需要重新配置 Git 使用HTTP身份验证。

以下是我在我的 GitHub Actions 工作流程 中如何使用它。我已经将我的 GitHub PAT token 添加为 GitHub Actions 的密钥, 命名为 GA_TOKEN

  - name: Reconfigure git to use HTTP authentication
    run: |
      git config --global url.https://${{ secrets.GA_TOKEN }}@github.com/.insteadOf ssh://git@github.com/

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接