在 GitHub 操作的构建阶段安装私有存储库

Install private repository in build stage on GitHub Actions

我正在使用 GitHub 操作部署到 Azure。在这个项目中,我使用我们自己的私人存储库,我们托管在 GitHub 上。这些存储库将在构建期间安装,它们的链接存储在 requirements.txt 中,例如:

git+ssh://git@github.com/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 操作流程中包含 webfactory/ssh-agent 操作:

When running a GitHub Action workflow to stage your project, run tests or build images, you might need to fetch additional libraries or vendors from private repositories.

GitHub Actions only have access to the repository they run for.

So, in order to access additional private repositories:

  • create an SSH key with sufficient access privileges.
  • Then, use this action to make the key available with ssh-agent on the Action worker node.
  • Once this has been set up, git clone commands using ssh URLs will just work. Also, running ssh commands to connect to other servers will be able to use the key.

这样的工作流程如下:

# .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

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

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

别忘了create a personal access token and set it as ACCESS_TOKEN in your repository secrets