如何在 Github 中指定 actions/setup-node 的路径

Sir*_*wrk 22 yaml node.js github-actions

注意:我已经看过这两个:

如何在 github 操作中的特定文件夹中运行 CI 步骤
如何在 Github 操作中指定节点的路径?

但我仍然无法让它工作,这就是为什么我问如何设置命令的工作目录uses。我的 yaml 当前如下所示:

# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
  push:
    branches: [ main, Create-.yml-file ]
  pull_request:
    branches: [ main, Create-.yml-file ]

jobs:
  javatest:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-java@v2
      with:
        java-version: '16'
        distribution: 'adopt'
    - name: Cache Maven packages
      uses: actions/cache@v2
      with:
        path: ~/.m2
        key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
        restore-keys: ${{ runner.os }}-m2
    - name: Build with Maven
      run: |
          mvn -f ./backend/pom.xml -B test
          #mvn -f ./notification/pom.xml -B test

    - name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        on-missing-report: quiet
        jacoco-csv-file: >
          -backend/target/site/jacoco/jacoco.csv

    - name: Log coverage percentage
      run: |
        echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
        echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'myusername'
          git config --global user.email 'myId@users.noreply.github.com'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

    - name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v2
      with:
        name: jacoco-report
        path: target/site/jacoco/
      
  nodejstest:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
      working-directory: ./frontend
    - run: npm run build --if-present
      working-directory: ./frontend
    - run: npm test
      working-directory: ./frontend

Run Code Online (Sandbox Code Playgroud)

此处发生错误:

 - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

Run actions/setup-node@v2
/usr/local/bin/npm config get cache
/home/runner/.npm
Error: Dependencies lock file is not found in /home/runner/work/path/to/main/directory. Supported file patterns: package-lock.json,yarn.lock
Run Code Online (Sandbox Code Playgroud)

我的包锁位于 .../path/to/main/directory/frontend 中,因此很明显找不到它,但根据其他两个解决方案,这个片段应该可以工作,不是吗?我还已经尝试将最后三个运行语句合并到并将工作目录设置移动到不同的位置。全部都有不同程度的失败

Wil*_*son 44

2.4 版本中添加了对自定义路径(相对于存储库根目录)的支持: https: //github.com/actions/setup-node/releases/tag/v2.4.0

  1. 您可以尝试'**/package-lock.json'在中 指定cache-dependency-path(对于其他模式,它对我不起作用)。
  2. working directory您也可以尝试将(所有作业或您的特定作业)设置nodejstest为指向您的frontend文件夹。
  nodejstest:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: 'frontend' # Here the path to the folder where package-lock.json is located.
    strategy:
      matrix:
        node-version: [16.x] # Are you are missing this specification?
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
        cache-dependency-path: '**/package-lock.json' # THIS PATTERN did the trick for me.
Run Code Online (Sandbox Code Playgroud)


abi*_*ode 9

setup_build_environment:

runs-on: ubuntu-latest
defaults:
  run:
    shell: bash
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 }}
    cache: 'yarn'
    cache-dependency-path: ./node-app/yarn.lock

- name: run npm commands
  working-directory: node-app
  run: | 
    yarn && CI=true
    # yarn build --if-present
    # yarn test
    node test.js
Run Code Online (Sandbox Code Playgroud)

参考: https: //github.com/actions/setup-node#caching-packages-dependencies

该操作默认在存储库根目录中搜索依赖文件(package-lock.json 或yarn.lock),并使用其哈希作为缓存键的一部分。当使用多个依赖文件或它们位于不同的子目录中时,请使用cache-dependency-path。

我使用了yarn,并且节点应用程序位于/node-app目录中,因此在cache-dependency-path中我使用yarn中的锁定文件。并使用工作目录在节点应用程序目录中运行纱线命令。