Use working-directory for entire job

Is it possible to specify a working-directory for an entire job? e.g. the equivilent of specifying the same one on every step?

3 Likes

When you use jobs.<job_id>.steps.run , using the working-directory keyword, you can specify the working directory of where to run the command. You need to add work-directory keyword to all jobs.<job_id>.steps.run.

Please refer to this document for more details.

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepsrun

If you want to use working-directory for entire job, I would suggest to set a job level environment variable, set its value with the directory, and then use context with ${{ env.variablename}} as the value of work-directory keyword in jobs.<job_id>.steps.run.  

For Contexts syntax for GitHub Actions , please refer to:

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#contexts

Please see my example, my repo file structure is like this:

I add a job level env named “working-directory”. Then set ${{env.working-directory}} as the value of working-directory keyword in jobs.<job_id>.steps.run.  

  dir.png

20 Likes

Its a workaround, but a lot of reptition code and not error prone!

3 Likes

Yes this is not a great solution since you cannot use

uses

with 

working-directory
16 Likes

Is there still no way to set a working directory that applies to all the steps in a job?

6 Likes

@nodesocket I am afraid that I have to say no. What’s your scenario which need to run all steps in a specific path? Let me see whether there’s a work around for you. 

Hey @yanjingzhu ,

In my case I am using GitHub Actions in a monorepo and I would love to be able the set the working directory on the job level for all steps since I tend to only run a job for a specific sub directory / repo.

9 Likes

Hi @sharkyze , 

For job level working-directory , I would recommand you to share your scenario and requirement in the Feedback form for GitHub Actions. Thank you for your understanding. 

1 Like

 @nodesocket  Doesn’t this get it done: https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_iddefaultsrun

10 Likes

@masahigo that worked perfectly. Must have missed that.

defaults:  
&nbsp; run:  
&nbsp; &nbsp; working-directory: ./foo  
10 Likes

Take a look at: https://dev.to/shofol/run-your-github-actions-jobs-from-a-specific-directory-1i9e

2 Likes