如何在Github操作中指定节点的路径?

Vic*_*ico 10 npm github-actions

我正在尝试使用 Github 的 node.js 工作流程在我的存储库上进行自动化测试。但是,我遇到了困难,因为节点是在子目录中设置的,而不是在存储库的根目录中。我一直在寻找一种方法来指定运行 npm 命令的目录,但没有找到任何答案。

这是工作流程代码:

# This workflow will do a clean install of node dependencies, 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: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v2
      with: 
        path: backend_app
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
    - run: cd backend_app
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test
Run Code Online (Sandbox Code Playgroud)

这是操作 run 生成的错误:

    Run npm ci
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/runner/work/directory_name/directory_name/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/runner/work/directory_name/directory_name/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-06-16T03_36_41_777Z-debug.log
Error: Process completed with exit code 254.
Run Code Online (Sandbox Code Playgroud)

Vis*_*amy 7

请指定“工作目录”。请参考我的 .yml 文件。

  1. 工作目录:./Projects/books-store-mean/webapi

工作流程文件供参考:https://github.com/vishipayyallore/mini-projects-2021/actions/workflows/booksstore-nodeexpressmongo.yml

# This workflow will do a clean install of node dependencies, build the source code, and run tests across different versions of the 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: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
      working-directory: ./Projects/books-store-mean/webapi
    - run: npm run build --if-present
      working-directory: ./Projects/books-store-mean/webapi
    - run: npm test
      working-directory: ./Projects/books-store-mean/webapi
Run Code Online (Sandbox Code Playgroud)

  • 对于那些需要为 `setup-node@v2` 步骤指定锁定文件路径的人,您可以在 `with` 下添加一行,如下所示: `cache-dependency-path: ./some/path/package -lock.json` (6认同)
  • @derekbaker783 令人惊讶的是找到这个答案是多么困难。谢谢! (3认同)