在 GitHub Actions 上的构建阶段安装私有存储库

问题描述 投票:0回答:2

我正在使用 GitHub Actions 部署到 Azure。在这个项目中,我使用我们自己的私有存储库,该存储库托管在 GitHub 上。这些存储库将在构建期间安装,它们的链接存储在

requirements.txt
中,例如:

git+ssh://[email protected]/org-name/package-name.git

在本地,安装需求没有问题,因为我可以通过 SSH 访问这些私有存储库。但我如何在 GitHub 操作构建过程中访问这些内容。

我收到错误:

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.

这是有道理的,因为它是一个私有存储库。

github github-actions continuous-deployment
2个回答
11
投票

当使用“requirements.txt”或“pyproject.toml”时,最好的解决方案是使用

git config url.insteadOf
。例如,使用“requirements.txt”:

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

不要忘记创建个人访问令牌并将其设置为

ACCESS_TOKEN
在您的存储库机密中。

“pyproject.toml”版本(带有“test”可选依赖项)将是:

- name: Install requirements
  run: |
    git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github
    python -m pip install ".[test]"

它基本上相当于字符串替换。例如,

pyproject.toml
中列出的依赖项为:

[project]
dependencies = ["my-package @ git+https://github.com/myorg/my-package"]

将像以前一样工作:

[project]
dependencies = ["my-package @ git+https://[email protected]/myorg/my-package"]

正如一些评论所指出的,您需要替换的 URL 片段取决于您的

requirements.txt
pyproject.toml
中的内容。如果您在本地计算机上使用 SSH,则可能是:

git config --global url."https://github-actions:${{ secrets.ACCESS_TOKEN }}@github".insteadOf ssh://git@github

10
投票

您可以尝试在 GitHub Action 工作流程中包含

webfactory/ssh-agent
操作:

运行 GitHub Action 工作流程来暂存项目、运行测试或构建映像时,您可能需要从私有存储库获取其他库或供应商。

GitHub Actions 只能访问它们运行的存储库。

因此,为了访问其他私有存储库:

  • 创建具有足够访问权限的 SSH 密钥。
  • 然后,使用此操作使密钥可通过操作工作节点上的 ssh-agent 使用。
  • 设置完成后,使用 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/[email protected]
              with:
                  ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
            - ... other steps
© www.soinside.com 2019 - 2024. All rights reserved.