You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用google-github-actions/setup-gcloud@v0时GitHub Actions凭据解析报错

解决GitHub Actions中google-github-actions/setup-gcloud@v0的JSON凭据解析错误

执行google-github-actions/setup-gcloud@v0时触发如下错误:

Error: google-github-actions/setup-gcloud failed with: error parsing credentials: Unexpected token � in JSON at position 0
Ensure your credentials are base64 encoded or validate JSON format: 
    {
      "type": "service_account",
      "project_id": "project-id",
      "private_key_id": "key-id",
      "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key\n-----END PRIVATE KEY-----\n",
      "client_email": "service-account-email",
      "client_id": "client-id",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account-email"
    }

以下是具体排查和解决步骤:

  • 验证服务账号JSON格式合法性
    打开下载的GCP服务账号JSON密钥,检查是否存在乱码、多余字符(如开头不可见符号)。用编辑器的JSON语法检查功能确认结构完全符合示例要求,重点核对private_key字段是否完整包含首尾标记,换行符是否正确。

  • 检查GitHub Secrets存储规范

    • 若直接存储JSON内容:全选原始JSON文件内容后直接粘贴到Secret输入框,避免手动修改或引入额外空格、换行。
    • 若使用Base64编码凭据:
      • 本地执行编码命令:Mac/Linux用base64 -i your-service-account-key.json;Windows用certutil -encode your-service-account-key.json encoded-key.txt,复制编码结果时去掉文件首尾的证书注释内容。
      • 将编码后的完整字符串存入Secret,不要添加任何额外字符。
  • 核对工作流中的凭据引用逻辑

    • 原始JSON凭据直接引用:
      - name: Set up GCloud
        uses: google-github-actions/setup-gcloud@v0
        with:
          credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
      
    • Base64编码凭据需先解码:
      - name: Decode GCP credentials
        run: echo "${{ secrets.GCP_SERVICE_ACCOUNT_KEY_BASE64 }}" | base64 -d > /tmp/gcp-key.json
      - name: Set up GCloud
        uses: google-github-actions/setup-gcloud@v0
        with:
          credentials_file: /tmp/gcp-key.json
      
  • 排查文件编码问题
    确保服务账号JSON文件为UTF-8编码(无BOM),避免因编码格式导致解析时出现乱码字符。可通过VS Code等编辑器查看并转换编码格式。

内容的提问来源于stack exchange,提问作者Summy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 19:54:20