Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '[email protected]'
          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

like image 671
SirHawrk Avatar asked Sep 05 '25 03:09

SirHawrk


2 Answers

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.
like image 112
Wilson Avatar answered Sep 07 '25 22:09

Wilson


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

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

The action defaults to search for the dependency file (package-lock.json or yarn.lock) in the repository root, and uses its hash as a part of the cache key. Use cache-dependency-path for cases when multiple dependency files are used, or they are located in different subdirectories.

I have used yarn, and the node app resides in /node-app directory, so in cache-dependency-path I use the lock file from yarn. And using working-directory for running yarn commands in node-app directory.

like image 29
abitcode Avatar answered Sep 07 '25 21:09

abitcode