DEV Community 👩‍💻👨‍💻

Cover image for Run your Github Actions jobs from a specific directory
Anower Jahan Shofol
Anower Jahan Shofol

Posted on

Run your Github Actions jobs from a specific directory

I am working on a current project where the Backend and the Frontend of the application are on the same repository of Github. That's why I faced a situation to run the Github Actions jobs for my React app from a specific directory. And, I find a quick solution to it.

Specify for all Jobs:

Github actions have a working-directory option to declare on workflow. It specifies the working directory for all run steps.

defaults:
  run:
    working-directory: web

Documentation:
defaults.run

Specify for specific Job:

So, you got the idea. This can be configured for specific jobs too. To set working_directory for a specific job, here is the procedure-

jobs:
  job1:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: scripts

Documentation:
jobs..defaults.run

Here is a simple Actions for NodeJs build job. The 'web' directory will be used for all jobs run-

name: Node.js CI

on:
  push:
    branches: [ frontend-develop ]
  pull_request:
    branches: [ frontend-develop ]

defaults:
  run:
    working-directory: web

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x, 12.x, 14.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test 

From the first documentation an important note to quote-

When more than one default setting is defined with the same name, GitHub uses the most specific default setting. For example, a default setting defined in a job will override a default setting that has the same name defined in a workflow.

Thanks for checking out the article. I hope it may help someone somehow. Let me know if I missed something or did something wrong.

Good Luck!

Cover Image: Github

You can find me on-
Twitter
LinkedIn

Top comments (5)

Collapse
 
spydon profile image
Lukas Klingsbo

This doesn't work for uses unfortunately.

Collapse
 
corleroux profile image
Cor le Roux

For anyone struggling with the Node Setup action step, specifically with the uses section when your project is in a sub-folder, you need to add a cache-dependency-path to the with section.

Example:


 - uses: actions/setup-node@v2
  with:
    node-version: '14'
    cache: 'npm'
    cache-dependency-path: subdir/package-lock.json`
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rickychongjl profile image
rickychongjl • Edited on

To add to this, if your package.lock.json is in another directory, this would most probably mean that you need to target npm build command to that particular directory by adding the default-directory syntax on your steps or job (depending on your setup)

npm:
runs-on: windows-latest
defaults:
run:
working-directory: ./subdir/
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
cache-dependency-path: ./subdir/package-lock.json

Collapse
 
seriosus profile image
Alekz

uses is for linking a repo, which works in its own directories, that is what is supposed to happen, that's why you cant use working-directory with uses

Collapse
 
bgrand_ch profile image
Benjamin Grand

Thanks a lot!

🌚 Friends don't let friends browse without dark mode.

Good news! You can update to dark mode in your DEV settings.