15

我想priv_gem_a通过 github 操作在 gem 上运行 rspec(调用它)。

priv_gem_a取决于私人回购中的另一个宝石(称之为priv_gem_b)。priv_gem_b但是,由于权限无效,我无法捆绑安装。

错误:

Fetching gem metadata from https://rubygems.org/..........
Fetching git@github.com:myorg/priv_gem_b
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Host key verification failed.
Retrying `git clone 'git@github.com:myorg/priv_gem_b' "/opt/hostedtoolcache/Ruby/2.6.3/x64/lib/ruby/gems/2.6.0/cache/bundler/git/priv_gem_b-886cdb130fe04681e92ab5365f7a1c690be8e62b" --bare --no-hardlinks --quiet` due to error (2/4): Bundler::Source::Git::GitCommandError Git error: command `git clone 'git@github.com:myorg/priv_gem_b' "/opt/hostedtoolcache/Ruby/2.6.3/x64/lib/ruby/gems/2.6.0/cache/bundler/git/priv_gem_b-886cdb130fe04681e92ab5365f7a1c690be8e62b" --bare --no-hardlinks --quiet` in directory /home/runner/work/priv_gem_a/priv_gem_a has failed.

我认为这与跑步者无法访问同一组织中的不同私人仓库有关。

所以我尝试将环境变量添加到我的工作流文件中,包括GITHUB_TOKENs,但这不起作用:

name: Test Code

on:
   push:
     branches:
     - master

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up Ruby 2.6
      uses: actions/setup-ruby@v1
      with:
        ruby-version: 2.6.x
    - name: Install dependencies
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        BUNDLE_GITHUB__COM: ${{ secrets.GITHUB_TOKEN }}:x-oauth-basic
      run: |
        gem install bundler
        gem update bundler
        bundle install --without development --jobs 4 --retry 3
    - name: Test with RSpec
      run: |
        bundle exec rspec

只是 Gemfile 中关于此的一个片段:

gem 'priv_gem_b', '>= 7.0.1', '< 8', git: 'git@github.com:my_org/priv_gem_b', branch: :master

4

2 回答 2

13

我相当确定GITHUB_TOKEN存储库中的默认机密仅适用于该存储库。您不能使用它来访问其他存储库。

尝试改用repo作用域令牌。在https://github.com/settings/tokens创建一个,然后将其作为机密添加到您的工作流运行的存储库中。它将位于https://github.com/[username]/[repo]/settings /秘密

在您的工作流程中使用该密钥,而不是GITHUB_TOKEN.

BUNDLE_GITHUB__COM: ${{ secrets.REPO_SCOPED_TOKEN }}:x-oauth-basic

或者,使用x-access-token我认为更可取的方法。

BUNDLE_GITHUB__COM: x-access-token:${{ secrets.REPO_SCOPED_TOKEN }}

此外,我认为您需要更改对私有 gem 的引用,以便它使用 HTTPS。您现在引用它的方式意味着它将尝试使用 SSH 密钥而不是BUNDLE_GITHUB__COM.

gem 'my_private_repo', git: 'https://github.com/username/my_private_repo.git'
于 2019-09-19T03:07:50.920 回答
3

所以我的项目包含一个使用 ssh 密钥访问的私有 gem。我们不想更改 Gemfile 并通过 访问它https,因为这会涉及更改部署过程。

我们使用了这个动作ssh-key-action我们在github secrets中添加了私有 ssh 密钥SSH_KEY

- name: Install SSH keys
  uses: shimataro/ssh-key-action@v2
  with:
    key: ${{ secrets.SSH_KEY }}
    known_hosts: "Add known public keys of github here"

- name: Install gems # usual step to install the gems.
  run: |
    bundle config path vendor/bundle 
    bundle install --jobs 4 --retry 3

它无缝地工作。

注意 - 确保key参数没有给出硬编码的私钥。它不会那样工作。

于 2020-04-20T08:34:31.447 回答