node.jsyamlgithub-actions

How to specifiy path for actions/setup-node in Github


Note: I have already seen these two:

How do I run my CI steps in a specific folder in github action
How to specify node's path in Github action?

But I still cant get it to work, thats why I am asking how I am able to set the working directory for a uses command. My yaml currently looks as follows:

# 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

with the error occuring here:

 - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'

And looking like this:

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

My package-lock ist located in the .../path/to/main/directory/frontend so it is obvious that it can not be found but according to the other two solutions this snippet should work shouldn't it? I also already tried combining the last three run statements into one as well as move the working-directory setting to different places. All with varying amounts of failure


Solution

  • The support for custom path (relative to repository root) was added in version 2.4: https://github.com/actions/setup-node/releases/tag/v2.4.0

    1. You can try specifying '**/package-lock.json' in the cache-dependency-path (it didn't work for me with other patterns).
    2. Also you can try setting the working directory (either for all jobs or your nodejstest specific job) to point to your frontend folder.
      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.