Github Actions 和 git clone 问题

sha*_*kka 5 terraform github-actions

在 Github Actions 中使用 git clone 时遇到一些问题,无论我尝试什么,我都会得到以下信息:

我的 main.yml 中失败的代码:

jobs:
 terraform:
   name: 'Terraform with Github Actions!'
   runs-on: ubuntu-latest
   steps:
   - name: 'Login to Azure'
    uses: azure/login@v1
    with:
      creds: ${{ secrets.AZURE_CREDENTIALS }}
  - name: 'Checkout'
    uses: actions/checkout@master
  - name: 'Preparing blueprint-environment'
    run: |
      snip
      git clone git@github.com:ourcompany/whateverrepo.git
Run Code Online (Sandbox Code Playgroud)

错误信息:

git@github.com:权限被拒绝(公钥)。

我看过很多关于添加 ssh 密钥的帖子,但那是本地的,而不是在从 Github 操作运行的 ubuntu 版本中 - 我在这里缺少什么?我无法生成 ssh 密钥并将私钥动态添加到 Github 存储库设置,我该如何解决此问题?

pet*_*ans 10

如果您需要签出两个存储库,我建议checkout再次使用相对路径。请参阅文档并排检查多个存储库。您可能需要使用repo范围个人访问令牌 (PAT)

  - name: 'Checkout'
    uses: actions/checkout@v2
  - name: 'Preparing blueprint-environment'
    uses: actions/checkout@v2
    with:
      token: ${{ secrets.PAT }}
      repository: ourcompany/whateverrepo
      path: whateverrepo
Run Code Online (Sandbox Code Playgroud)

如果确实有必要,可以使用部署密钥通过 SSH 克隆存储库。

  1. 为您的存储库创建新的 SSH 密钥对。不要设置密码。
  2. 将公钥(.pub 文件)的内容复制到新的存储库部署密钥,然后选中“允许写入访问”框。
  3. 将秘密添加到包含私钥全部内容的存储库。
  4. 如下例所示,配置actions/checkout为使用您创建的部署密钥。
    steps:
      - uses: actions/checkout@v2
        with:
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
Run Code Online (Sandbox Code Playgroud)

  • 创建 PAT 后,您需要将其作为机密添加到运行工作流的存储库中。您可以随意命名该秘密。我在示例中使用了“PAT”。 (2认同)