Exécuterning actions in another directory

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Exécuterning actions in another directory

Message par ForumBot »

I've just started exploring Github actions however I've found myself placing a command in multiple places.

I have a PHP project where the `composer.json` is not in the root, my structure looks like:

```
my-project:
readme.md
app:
composer.json

```

Obviously there is more to it and there is a reason why, but my `composer.json` sits in a subdirectory called 'app'. As a result in my workflow, I have to cd into that folder every time to run a command:

```
name: CI

on: [push]

jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Setup Symfony
run: |
cd app
cp .env.dev .env
- name: Install Composer Dependencies
run: |
cd app
composer install --prefer-dist
- name: Run Tests
run: |
cd app
php bin/phpunit

```

How can I remove the `cd app` in every stage?
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Exécuterning actions in another directory

Message par ForumBot »

**Update**: It's now possible to set a `working-directory` default for a job. See [this answer](https://stackoverflow.com/a/63122434/11934042).

There is an option to set a `working-directory` on a step, but not for multiple steps or a whole job. I'm fairly sure this option only works for script steps, not action steps with `uses`.

[https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun)

Using `working-directory`, your workflow would look like this. It's still quite verbose but maybe a bit cleaner.

```
name: CI

on: [push]

jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Setup Symfony
working-directory: ./app
run: cp .env.dev .env
- name: Install Composer Dependencies
working-directory: ./app
run: composer install --prefer-dist
- name: Run Tests
working-directory: ./app
run: php bin/phpunit

```

Alternatively, you can run it all in one step so that you only need to specify `working-directory` once.

```
name: CI

on: [push]

jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Setup and run tests
working-directory: ./app
run: |
cp .env.dev .env
composer install --prefer-dist
php bin/phpunit

```
Répondre

Revenir à « GitHub Copilot »