在 GitHub Actions 的构建阶段安装私有仓库

15

我正在使用GitHub Actions将内容部署到Azure。在这个项目中,我使用了我们自己私有的存储库,这些存储库托管在GitHub上。这些存储库将在构建过程中被安装,并且它们的链接将存储在requirements.txt文件中,例如:

git+ssh://git@github.com/org-name/package-name.git

在本地安装要求没有问题,因为我可以通过SSH访问这些私有存储库。但是在GitHub actions构建期间如何访问它们。

我得到了以下错误:

Collecting git+ssh://****@github.com/org-name/package-name.git (from -r requirements.txt (line 1))
  Cloning ssh://****@github.com/org-nam/package-name.git to /tmp/pip-req-build-9nud9608
ERROR: Command errored out with exit status 128: git clone -q 'ssh://****@github.com/org-name/package-name.git' /tmp/pip-req-build-9nud9608 Check the logs for full command output.
Error: Process completed with exit code 1.

这是有道理的,因为它是一个私人仓库。

2个回答

9

对于那些想知道的人,我发现另一个更容易应用的解决方案是使用访问令牌:

- name: Install requirements
  run: |
    git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github
    pip install -r requirements.txt

不要忘记创建个人访问令牌并将其设置为ACCESS_TOKEN在您的存储库密码中。

你想使用 --global 标志的原因是什么? - Tzane
不是很确定,你可以用 --global 标志进行测试。@Tzane - Erfan
没对我起作用。 - Anton Matiash

9
您可以在GitHub Action工作流程中加入webfactory/ssh-agent action以获取私有仓库的附加库和厂商。
当运行GitHub Action工作流以准备您的项目、运行测试或构建映像时,可能需要从私有仓库中获取附加库和厂商。GitHub Actions仅可以访问它们所运行的存储库。因此,要想访问额外的私有仓库:
1. 创建一个SSH密钥并授予权限。 2. 然后使用这个action将密钥在Action worker节点上作为ssh-agent可用。 3. 设置完成后,使用带有ssh URL的git clone命令就可以轻松工作。此外,运行ssh命令来连接其他服务器也可以使用该密钥。
如下是一个示例工作流:
# .github/workflows/my-workflow.yml
jobs:
    my_job:
        ...
        steps:
            - actions/checkout@v1
            # Make sure the @v0.4.1 matches the current version of the
            # action 
            - uses: webfactory/ssh-agent@v0.4.1
              with:
                  ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
            - ... other steps

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