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
If you like my writing and want to support me then you can-
You can also find me here-
Top comments (6)
This doesn't work for
uses
unfortunately.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 acache-dependency-path
to thewith
section.Example:
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
your solution in addition to the article was helpful
uses
is for linking a repo, which works in its own directories, that is what is supposed to happen, that's why you cant useworking-directory
withuses
Thanks a lot!