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

Erf*_*fan 15 github continuous-deployment github-actions

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

git+ssh://git@github.com/org-name/package-name.git
Run Code Online (Sandbox Code Playgroud)

在本地,安装需求没有问题,因为我可以通过 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.
Run Code Online (Sandbox Code Playgroud)

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

Erf*_*fan 10

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

- name: Install requirements
  run: |
    git config --global url."https://${{ secrets.ACCESS_TOKEN }}@github".insteadOf https://github
    pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

不要忘记创建个人访问令牌并将其设置ACCESS_TOKEN存储库的 Secrets


Von*_*onC 9

您可以尝试在 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/ssh-agent@v0.4.1
              with:
                  ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
            - ... other steps
Run Code Online (Sandbox Code Playgroud)