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

时间:2020-05-18 19:39:39

标签: node.js git github github-actions

我正在尝试通过运行npm install在github工作流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",
  },
  ...

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

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

3 个答案:

答案 0 :(得分:2)

标准令牌没有足够的权限:

令牌的权限仅限于包含您的工作流程的存储库。有关更多信息,请参见"Permissions for the GITHUB_TOKEN"

您必须手动创建用于访问软件包的个人访问令牌:

如果您需要令牌,而该令牌需要GITHUB_TOKEN中不可用的权限,则可以创建个人访问令牌并将其设置为存储库中的秘密:

  1. 使用或创建对该存储库具有适当权限的令牌。有关更多信息,请参见"Creating a personal access token for the command line"
  2. 将令牌作为秘密添加到您的工作流存储库中,并使用${{ secrets.SECRET_NAME }}语法对其进行引用。有关更多信息,请参见"Creating and using encrypted secrets"

来源:https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token

答案 1 :(得分:1)

私有存储库通过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密钥,而是设置专用的SSH密钥 供GitHub Actions使用。如果不确定,请参见以下一些提示 关于这一步。

  2. 确保您的私钥上没有设置密码短语。

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

答案 2 :(得分:0)

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

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

相反,我们要做的是重新配置 Git 以使用 HTTP 身份验证。

这是我在 GitHub Actions workflow 中使用它的方式。我已在 GitHub Actions 中将我的 GitHub PAT token 作为秘密添加为 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/