Home > Mobile >  How to specify working directory for github actions for monorepo
How to specify working directory for github actions for monorepo

Time:01-13

Recently I switched from multiple repositories to a monorepo, and I am trying to rewrite my workflows to work with the monorepo. I want to specify path to my subproject for the jobs. For example I have the following job.

- name: Gradle Build
      uses: eskatos/gradle-command-action@v1
      with:
        arguments: build test --stacktrace --info

How do I make this run from a specific subdirectory? (or, if possible, I want to specify a base directory for all jobs in the file)

Thank you in advance.

CodePudding user response:

You can specify a working-directory for a step or an entire job: https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepsrun

jobs:
  job1:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
        working-directory: [base-directory]

    steps:
      - name: step1
        run: [cmd]
        working-directory: ./dir1

    - name: step2
        run: [cmd]
        working-directory: ./dir2

You could have a look on this post: https://github.community/t/use-working-directory-for-entire-job/16747

CodePudding user response:

Nicolas G. answer is correct and would work, but as you explained you wanted to specify a working directory for all jobs in the file, the defaults field is also valid at the workflow level, to avoid repetition.

At the workflow level

defaults:
  run:
    working-directory: ./your_working_dir
jobs:
  job1:
    [...]
  job2:
    [...]

At the job level

jobs:
  job1:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./your_working_dir1
    steps:
    [...]

  job2:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./your_working_dir2
    steps:
    [...]

You can learn more about the defaults field on the Github official documentation.

  •  Tags:  
  • Related