GitHub Actions集成Lighthouse CI遇认证错误求助
问题
尝试将Lighthouse CI集成到GitHub Actions的CI/CD流程中生成应用性能报告,构建应用、SonarCloud扫描等其他任务均可正常运行,但Lighthouse CI任务执行失败,报错信息:
Error: fatal: could not read Username for 'https://github.com': terminal prompts disabled
对应的GitHub Actions工作流代码如下:
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions name: Node.js CI on: push: branches: [ "main" ] pull_request: branches: [ "main" ] env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: sonarcloud: name: SonarCloud runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} lighthouse: name: Lighthouse CI runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 with: token: ${{ secrets.LHCI_GITHUB_APP_TOKEN }} submodules: recursive - name: Use Node.js 16.x uses: actions/setup-node@v3 with: node-version: 16.x - name: Run the Lighthouse CI run: | npm install -g @lhci/cli@0.6.x lhci autorun build: runs-on: ubuntu-latest permissions: contents: read packages: write strategy: matrix: node-version: [ 16.x ] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} cache: 'npm' - run: npm ci - run: npm run build --if-present - run: npm test - name: Log in to the Container registry uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=sha - name: Build and push Docker image uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }}
报错截图:
解决方案
该报错是因为Lighthouse CI执行lhci autorun时,尝试向GitHub仓库提交性能报告,但当前环境未提供有效Git认证信息,终端无法弹出用户名输入提示导致失败。可通过以下方式解决:
1. 跳过Git提交,使用临时存储托管报告
若无需将报告提交到仓库,修改lhci autorun命令,指定临时公共存储作为上传目标:
lhci autorun --upload.target=temporary-public-storage
2. 配置Git认证与权限
若需要提交报告到仓库,先在任务中配置Git用户名和邮箱,同时确保Actions有仓库写入权限:
- 修改Lighthouse CI任务,添加Git配置步骤并更新权限:
lighthouse: name: Lighthouse CI runs-on: ubuntu-latest permissions: contents: write # 赋予仓库内容写入权限 steps: - name: Checkout code uses: actions/checkout@v3 with: token: ${{ secrets.GITHUB_TOKEN }} # 使用默认GITHUB_TOKEN,无需额外创建LHCI_GITHUB_APP_TOKEN submodules: recursive - name: Use Node.js 16.x uses: actions/setup-node@v3 with: node-version: 16.x - name: Configure Git credentials run: | git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" - name: Run the Lighthouse CI run: | npm install -g @lhci/cli@0.6.x lhci autorun
3. 使用自定义LHCI配置文件
创建项目根目录下的lighthouserc.js文件,明确配置上传目标和认证信息:
module.exports = { ci: { upload: { target: 'github', repoSlug: '你的用户名/仓库名', token: process.env.GITHUB_TOKEN, }, }, };
工作流中执行lhci autorun时会自动读取该配置。
内容的提问来源于stack exchange,提问作者Vercors
相关产品推荐
相关产品推荐

