75

I'm using GitHub Actions to build my project but my Dart project is in a sub-directory in the repository. The Action script can't find my pubspec.yaml and get the dependencies.

How can I point my GitHub Action to look for the source code in a sub-directory within my repository?

. (root of my GitHub repository)
└── dart_project
    ├── pubspec.yaml   <-- Git Hub action must point to this sub-dir
└── node_project
    ├── packages.json

This is the error I am getting:

Could not find a file named "pubspec.yaml" in "/__w/<my_project_path>".
##[error]Process completed with exit code 66.

This is the dart.yml file auto-generated by GitHub.

name: Dart CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    container:
      image:  google/dart:latest

    steps:
    - uses: actions/checkout@v1
    - name: Install dependencies
      run: pub get
    - name: Run tests
      run: pub run test

enter image description here

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165

2 Answers2

104

If I understand your needs, you need the pub steps to run as if you'd done a cd dart_project first, right? Add the working-directory parameter to your steps:

steps:
- uses: actions/checkout@v1
- name: Install dependencies
  run: pub get
  working-directory: dart_project
- name: Run tests
  run: pub run test
  working-directory: dart_project

If you want to apply it to every step, use the tag defaults

defaults:
  run:
    working-directory: dart_project

I believe that should be all you need.

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
rmunn
  • 34,942
  • 10
  • 74
  • 105
  • 5
    I use this as a `default` run parameter, e.g. `defaults:\n\s\srun:\n\s\s\s\sworking-directory: sub-directory-name`. I added escape characters to indicate the document/syntax structure as this certainly doesn't warrant an additional answer. – Jason R Stevens CFA Dec 29 '20 at 02:49
  • 6
    `working-directory` appears deprecated in `v3`, what's the workaround instead? – Kelsey Hannan Jul 06 '22 at 23:57
32

You can configure a working-directory on the step level for this purpose. You can also configure a default directory for the steps. Defaults can be on job or global level.

Example on step level.

jobs:
  build:
    steps:
      - uses: actions/checkout@v1
      - name: Install dependencies
        run: pub get
        working-directory: dart_project
      - name: Run tests
        run: pub run test
        working-directory: dart_project

Example on job level. This reduces duplication on the job level. This is suited for a job that is working on a sub-directory.

jobs:
  build:
    defaults:
      run:
        working-directory: dart_project
    steps:
      - uses: actions/checkout@v1
      - name: Install dependencies
        run: pub get
      - name: Run tests
        run: pub run test

Example on global level. This reduces duplication on the global level. This is suited when all jobs in the workflow file are for a project located in a sub-directory.

defaults:
  run:
    working-directory: dart_project
jobs:
  build:
    steps:
      - uses: actions/checkout@v1
      - name: Install dependencies
        run: pub get
      - name: Run tests
        run: pub run test