通过 GitHub CLI 在 GitHub Actions 中访问另一个存储库

5

我正在尝试作为工作流程的一部分,使用 gh cli 访问另一个 Github 仓库。 以下是我使用的命令:gh release view

run: |
    echo "::set-output name=description::$(gh release view --repo <owner/repo>)"
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

工作流失败,报404错误。我理解这是因为该仓库是私有的,尽管两个仓库都有相同的所有者。在本地进行身份验证时,命令可以正常工作。

有没有办法在工作流中访问该仓库?


你尝试过使用个人访问令牌(来自具有对私有存储库访问权限的帐户)而不是GITHUB_TOKEN吗? Github Actions的默认令牌仅具有特定范围的权限,可能不是您在此处需要的权限。 - GuiFalourd
1个回答

11
GITHUB_TOKEN仅限于触发存储库。 如果您需要访问其他存储库或其他帐户中的任何资源,则需要通过检出步骤传递具有更广泛作用域的令牌。 这可以是GitHub应用程序令牌、个人访问令牌等。

将令牌存储在Secrets/Actions中,并将其传递到checkout任务的token参数。

或者,您可以通过ssh-key参数传递ssh密钥。

- uses: actions/checkout@v2
  with:
    # Repository name with owner. For example, actions/checkout
    # Default: ${{ github.repository }}
    repository: ''

    # Personal access token (PAT) used to fetch the repository. The PAT is configured
    # with the local git config, which enables your scripts to run authenticated git
    # commands. The post-job step removes the PAT.
    #
    # We recommend using a service account with the least permissions necessary. Also
    # when generating a new PAT, select the least scopes necessary.
    #
    # [Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
    #
    # Default: ${{ github.token }}
    token: ''

    # SSH key used to fetch the repository. The SSH key is configured with the local
    # git config, which enables your scripts to run authenticated git commands. The
    # post-job step removes the SSH key.
    #
    # We recommend using a service account with the least permissions necessary.
    #
    # [Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
    ssh-key: ''

这同样适用于通过API或GitHub CLI调用其他存储库中的资源。

一个后续问题。我在Github下有几个属于同一组织的私人仓库。我们称它们为REPO-A、REPO-B和REPO-C。 REPO-A需要从REPO-B和REPO-C安装依赖项。我不能在REPO-B和REPO-C上添加相同的部署密钥,因此我需要使用两个单独的SSH密钥来访问这两个仓库。如何实现这一点? - Johnathan
由于每个检出都在自己的步骤中运行,因此您可以将3个SSH密钥注册为3个秘密,并将每个密钥传递给相应的密钥。普通用户的SSH密钥具有该用户拥有的所有相同权限,如果您创建一个“机器用户”(GitHub中用于自动化的普通用户),则可以授予其正确的权限并生成SSH密钥。 - jessehouwing
第二个结账似乎覆盖了先前存储库结账的内容。是否可以设置一个工作目录? - Iftikhar Ali
谢谢 @jessehouwing。那个方法有效。它创建了文件夹 Repo1/Repo2。有没有可能再往上一级?我还没试过 ../ - Iftikhar Ali
../folderName 不被允许。出现错误:仓库路径 '/home/ubuntu/actions-runner/_work/repo1/foldername' 不在 '/home/ubuntu/actions-runner/_work/repo1/repo1' 下面。 - Iftikhar Ali
显示剩余2条评论

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