无法使用 CI 部署到 firebase 托管:未找到 OAuth 令牌

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

我尝试将我的 astro 项目部署到 firebase 托管中,但我无法使用 CI 部署它们,但能够将它们部署在我自己的机器中。这是 CI 的日志

[2024-10-21T08:50:57.760Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2024-10-21T08:50:57.906Z] [iam] checking project my-project for permissions ["firebase.projects.get","firebasehosting.sites.update"]
[2024-10-21T08:50:57.908Z] No OAuth tokens found
[2024-10-21T08:50:57.911Z] >>> [apiv2][query] POST https://cloudresourcemanager.googleapis.com/v1/projects/my-project:testIamPermissions [none]
[2024-10-21T08:50:57.911Z] >>> [apiv2][(partial)header] POST https://cloudresourcemanager.googleapis.com/v1/projects/my-project:testIamPermissions x-goog-quota-user=projects/my-project
[2024-10-21T08:50:57.912Z] >>> [apiv2][body] POST https://cloudresourcemanager.googleapis.com/v1/projects/my-project:testIamPermissions {"permissions":["firebase.projects.get","firebasehosting.sites.update"]}
[2024-10-21T08:50:57.978Z] <<< [apiv2][status] POST https://cloudresourcemanager.googleapis.com/v1/projects/my-project:testIamPermissions 200
[2024-10-21T08:50:57.978Z] <<< [apiv2][body] POST https://cloudresourcemanager.googleapis.com/v1/projects/my-project:testIamPermissions {"permissions":["firebase.projects.get","firebasehosting.sites.update"]}
[2024-10-21T08:50:57.980Z] No OAuth tokens found
[2024-10-21T08:50:57.981Z] >>> [apiv2][query] GET https://firebase.googleapis.com/v1beta1/projects/my-project [none]
[2024-10-21T08:50:58.191Z] <<< [apiv2][status] GET https://firebase.googleapis.com/v1beta1/projects/my-project 200
[2024-10-21T08:50:58.192Z] <<< [apiv2][body] GET https://firebase.googleapis.com/v1beta1/projects/my-project {"my-project":"my-project","projectNumber":"885544713240","displayName":"My Project","name":"projects/my-project","resources":{"hostingSite":"my-project","storageBucket":"my-project.appspot.com","locationId":"asia-southeast1"},"state":"ACTIVE","etag":"1_9e3865a8-3d58-4604-8afd-2bef41db3846"}
[2024-10-21T08:51:51.951Z] SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at findDependency (/home/builds/7Wjzy3dMG/0/project-dir/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-tools/lib/frameworks/utils.js:188:23)
    at getNuxtVersion (/home/builds/7Wjzy3dMG/0/project-dir/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-tools/lib/frameworks/nuxt/utils.js:9:45)
    at discover (/home/builds/7Wjzy3dMG/0/project-dir/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-tools/lib/frameworks/nuxt2/index.js:25:48)
    at async discover (/home/builds/7Wjzy3dMG/0/project-dir/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-tools/lib/frameworks/index.js:47:32)
    at async prepareFrameworks (/home/builds/7Wjzy3dMG/0/project-dir/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-tools/lib/frameworks/index.js:192:25)
    at async deploy (/home/builds/7Wjzy3dMG/0/project-dir/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-tools/lib/deploy/index.js:58:13)
Error: An unexpected error has occurred.

这是我的

.firebaserc

{
  "projects": {
    "default": "my-project"
  },
  "targets": {
    "my-project": {
      "hosting": {
        "prd": [
          "production"
        ],
        "stg": [
          "staging"
        ]
      }
    }
  },
  "etags": {},
  "dataconnectEmulatorConfig": {}
}

这是我的 firebase.json

{
  "$schema": "https://raw.githubusercontent.com/firebase/firebase-tools/master/schema/firebase-config.json",
  "hosting": {
    "source": ".",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "frameworksBackend": {
      "region": "asia-east1"
    }
  }
}

我假设

No OAuth tokens found
是问题所在,但我已经在我的 ci cd 中设置了
GOOGLE_APPLICATION_CREDENTIALS
环境变量,但它仍然不起作用

这是我的ci

deploy-stg:
  stage: deploy
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"'
      changes:
        - '**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'
        - '.firebaserc'
        - firebase.json
        - .gitlab-ci.yml
  script:
    - touch credential.json && cp "$GCP_SERVICE_ACCOUNT" credential.json
    - gcloud auth activate-service-account --key-file=./credential.json
    - export GOOGLE_APPLICATION_CREDENTIALS=./credential.json
    - cp "$DOT_ENV_STAGING" .env
    - nvm use 20 && pnpm install --no-frozen-lockfile
    - pnpm exec firebase experiments:enable webframeworks
    - pnpm exec firebase deploy --only hosting --debug
  tags:
    - gcp-shared
firebase gitlab gitlab-ci firebase-hosting astrojs
1个回答
0
投票

我认为问题出在 CI 中的 credential.json 文件上。错误“JSON 输入意外结束”意味着它正在尝试读取空或损坏的 JSON 文件。

在您的脚本中,您使用 cp "$GCP_SERVICE_ACCOUNT" credential.json。但是,如果 $GCP_SERVICE_ACCOUNT 本身具有 JSON 内容,则 cp 将不起作用,因为它复制文件,而不是文本。

请尝试以下操作:echo "$GCP_SERVICE_ACCOUNT" > credential.json。这会将 JSON 内容写入文件中。

此外,请确保 $GCP_SERVICE_ACCOUNT 实际上具有您的凭据并且已在您的 CI 环境中正确设置。

仔细检查服务帐户是否拥有部署到 Firebase 所需的所有权限。如果有帮助请告诉我。

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