为什么可重用工作流程步骤在不同的存储库中失败,但在同一存储库中成功?

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

我正在使用 GitHub Actions 进行 CI/CD。我在名为 BuildTemplate 的存储库中添加了一个可重用的工作流程,以便将来的项目可以使用此模板。可重复使用的工作流程只需一个简单的步骤即可登录 Azure,然后从密钥保管库下载文件。 Azure 登录的机密全部保存在 BuildTemplate 存储库的设置 -> 机密和变量 -> 操作 -> 存储库机密下。

  jobs:
    download_secure_file:
     runs-on: [self-hosted, github-my-selfhosted-runner]
     steps:
      - name: Login to Azure
        uses: Azure/login@v1
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

当从不同的存储库调用此可重用工作流程步骤时,如下所示:

    jobs:
      download_secure_file:
        name: Download Secure File
        uses: MyOrg/MyReusableRepo/.github/workflows/template-download-secure-file.yml@main

该步骤失败并出现以下错误:

Run Azure/login@v1
Error: Az CLI Login failed. Please check the credentials and make sure az is installed on the runner. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows

当此登录步骤位于同一存储库中的可重用工作流程中时,它可以工作:

Run Azure/login@v1
Using OIDC authentication...
Federated token details: 
 issuer - https://token.actions.githubusercontent.com 
 subject claim - repo:MyOrg/MyRepo:ref:refs/heads/feature/MyFeature-7596
/usr/bin/az cloud set -n azurecloud
Done setting cloud: "azurecloud"
Login successful.

为什么从不同的仓库调用可重用工作流时登录失败?我浏览了 GitHub Actions 文档,没有看到我在这里遗漏的任何内容,导致可重用工作流程失败。请注意,即使错误显示“确保运行器上安装了 az”,也不需要它,因为它是成功执行此步骤的同一运行器计算机,无需安装 Azure CLI。

这里的实际问题是什么?

github github-actions openid-connect azure-cli
1个回答
1
投票

你就快到了...你所要做的就是从调用工作流程中传递秘密,否则秘密将为空,因此你的工作将会失败。

jobs:
  download_secure_file:
    name: Download Secure File
    uses: MyOrg/MyReusableRepo/.github/workflows/template-download-secure-file.yml@main
    secrets: inherit

指定secrets:inherit即可解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.