9

It's possible to set the working directory for separate scripts:

- script: foo
  workingDirectory: bar

However, if all the steps are meant to run in a specific directory, it becomes repetitive to define it for each step.

Using cd doesn't affect other steps:

- script: cd foo
- script: pwd # returns default working dir instead of foo

Two specific examples for when this issue matters are:

  • when checking out multiple repos as resources, so the default working directory is one level above the checked out repos
  • when running a pipeline for a project that's located in a subdirectory (like in a monorepo)
slikts
  • 8,020
  • 1
  • 27
  • 47

2 Answers2

2

Instead of changing the working directory for the tasks, a workaround is to move the files into the default working directory, and a convenient way to do it is using git-sparse-checkout like so:

git sparse-checkout set example && mv example/{*,.*} . || true

The {*,.*} part is for also moving the dotfiles, and || true is needed because that also tries to move . and ...

slikts
  • 8,020
  • 1
  • 27
  • 47
1

I guess that would mess things up in relation to predefined variables in ADO which are read-only - so I don't think it's possible.

You would be probably better off by checking sources to or copying things to the default working directory right away at the start of the pipeline.

psfinaki
  • 1,814
  • 15
  • 29