Search code examples
github-actions

Running Github actions in another directory


I would like to execute a github action in a sub-directory of my project because my Github repo contains 2 applications (front/back) and I need to test the front. (see below)

my-project
 ├─ api/
 │   └─ ...
 └─ front-app/
     └─ (node-js application I want to run the tests for)

I want to execute only the tests for the front-app running on svelte (node-js) The tests fails because they run in the project directory instead of the front-app/ one.

However I already implemented :

# Just after `on: push / pull_request`
defaults:
   run:
     working-directory: ./front-app/
# And before `jobs`

And I still get the error :

Run actions/setup-node@v3
Found in cache @ /opt/hostedtoolcache/node/16.16.0/x64
/opt/hostedtoolcache/node/16.16.0/x64/bin/npm config get cache
/home/runner/.npm
Error: Dependencies lock file is not found in /home/runner/work/my-project/my-project. Supported file patterns: package-lock.json,npm-shrinkwrap.json,yarn.lock

The file is define as :

# This workflow will do a clean installation of node dependencies, cache/restore them, 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: Tests for front-app

on:
  push:
    branches: [ "main" ]
    paths: ["front-app/**"]
  pull_request:
    branches: [ "main" ]
    paths: ["front-app/**"]

defaults:
  run:
    working-directory: ./front-app/

jobs:
  test-front-app:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3     # <-- I think the error occurs here
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci         # <-- This is not executed - The error occurs before
    - run: npm run build --if-present
    - run: npm test

Solution

  • So, I found the solution in the documentation here by adding cache-dependency-path: with the path of the package-lock.json (see below)

        - name: Use Node.js ${{ matrix.node-version }}
          uses: actions/setup-node@v3
          with:
            node-version: ${{ matrix.node-version }}
            cache: 'npm'
            cache-dependency-path: 'front-app/package-lock.json'