为 Firebase 托管设置 Github 操作时找不到 package.json 文件

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

在初始化 Firebase 托管的 github 操作时,我指定了以下内容:

设置工作流程以在每次部署之前运行构建脚本?是的 每次部署之前应运行什么脚本?纱线运行构建。

这个工作流程给了我错误

Run yarn run build
yarn run v1.22.10
error Couldn't find a package.json file in "/home/runner/work/SpaceBar/SpaceBar"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.

我的项目目录中有一个 package.json 文件。为什么说找不到?

firebase-hosting-merge.yml 位于名为 spacebar 的目录中,其中包含另一个名为 spacebar 的目录,其中包含 package.json 文件。 有没有办法cd到这个文件的子目录

name: Deploy to Firebase Hosting on merge
'on':
  push:
    branches:
      - main
jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: yarn run build
      - uses: FirebaseExtended/action-hosting-deploy@v0
firebase github-actions firebase-hosting
3个回答
7
投票

由于

actions/checkout
操作使您可以访问存储库文件和目录,因此在执行
cd
命令之前,您只需
package.json
进入
yarn
文件所在的目录即可。否则,它只会在存储库根目录中查找它。

示例(第二步)

run: |
   cd spacebar/spacebar
   yarn run build

3
投票

我在类似的设置下遇到了同样的错误。但不幸的是,标记为已接受的答案并没有解决我的问题。

但是,经过大量的谷歌搜索后,我确实设法解决了我的问题,只是想分享我发现的内容,以防其他人仍然遇到像我一样的问题:

正如 GuiFalourd 正确提到的那样,错误要求您位于 package.json 文件所在的目录中。但事实证明,有多种方法可以做到这一点——有些方法比其他方法更正确。在我的具体情况下,我正在使用的操作以及其中运行的所有命令都应该在特定目录中运行,因此我选择在

working-directory
设置中使用关键字
defaults
声明该目录:

name: Build & deploy

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./frontend
    
    strategy:
      matrix:
        node-version: [12.x]
steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
        
    - name: Clean Cache
      run: |
        npm cache clean --force
    - name: CI Install
      run: |
        npm ci
    - name: NPM Install
      run: |
        npm install      
    - name: Production Build
      run: |
        npm run build
      env:
        CI: false
    - name: Unit Tests
      run: |
        npm run test --passWithNoTests

另一种可行的选择是使用命令在每个步骤中设置

working-directory
。如果您想在特定目录中运行特定命令一次,而其他所有内容都在其他地方运行,这可能会很有用。缺点是它看起来很混乱并且是多余的,如果一切都应该在同一个目录中运行的话:

name: Build & deploy

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [12.x]
        
    steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
        
    - name: Clean Cache
      working-directory: ./frontend
      run: |
        npm cache clean --force
    - name: CI Install
      working-directory: ./frontend
      run: |
        npm ci
    - name: NPM Install
      working-directory: ./frontend
      run: |
        npm install      
    - name: Production Build
      working-directory: ./frontend
      run: |
        npm run build
      env:
        CI: false
    - name: Unit Tests
      working-directory: ./frontend
      run: |
        npm run test --passWithNoTests

请注意:

working-directory
关键字不适用于
uses
with
关键字。根据您使用的部署操作,您必须在其自己的
env
设置中进行设置。

就我而言,我的最终 YML 文件如下所示:

name: Build & deploy

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./frontend
    
    strategy:
      matrix:
        node-version: [12.x]
        
    steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
        
    - name: Clean Cache
      run: |
        npm cache clean --force
    - name: CI Install
      run: |
        npm ci
    - name: NPM Install
      run: |
        npm install      
    - name: Production Build
      run: |
        npm run build
      env:
        CI: false
    - name: Unit Tests
      run: |
         npm run test --passWithNoTests
    - name: Deploy to S3
      uses: jakejarvis/s3-sync-action@master
      with:
        args: --acl public-read --delete
      env:
        AWS_S3_BUCKET: ${{ secrets.AWS_PRODUCTION_BUCKET_NAME }}
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_REGION: ${{ secrets.AWS_REGION }}
        SOURCE_DIR: "./frontend/build"

希望这有帮助。


0
投票

使用工作目录对我有用

- name: NPM install, dependencies, build, and test
  working-directory: ./WebApp
  run: |
    npm install
    npm run build --if-present
© www.soinside.com 2019 - 2024. All rights reserved.