【问题标题】:Couldn't find a package.json file when setting up Github actions for Firebase Hosting为 Firebase 托管设置 Github 操作时找不到 package.json 文件
【发布时间】:2021-08-27 05:10:11
【问题描述】:

在为 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 文件。 有没有办法进入这个文件的子目录

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


    【解决方案1】:

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

    示例(第二步)

    run: |
       cd spacebar/spacebar
       yarn run build
    

    【讨论】:

    • 我有同样的问题,这对我不起作用。它只是说它找不到目录“空格键/空格键”。
    • 你能分享一下仓库看看哪里有问题吗?
    • 很遗憾我不能作为它的公司财产...
    【解决方案2】:

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

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

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

    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 关键字不适用于 useswith 关键字。根据您使用的部署操作,您必须在其自己的 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"
    

    希望这会有所帮助。

    【讨论】:

    • 请仅在与手头问题相关的情况下编辑我的答案。不相关的修改将被回滚。
    猜你喜欢
    • 2020-08-20
    • 2021-11-23
    • 2020-10-20
    • 2021-05-19
    • 2022-01-08
    • 2019-06-13
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多