Logo

dev-resources.site

for different kinds of informations.

Run your Github Actions jobs from a specific directory

Published at
5/26/2020
Categories
todayilearned
github
webdev
Author
Anower Jahan Shofol
Categories
3 categories in total
todayilearned
open
github
open
webdev
open
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

If you like my writing and want to support me then you can-


Buy me a coffee button for Revolab by Anower Jahan Shofol, the write of this article

You can also find me here-

Featured ones: