Update: It's now possible to set a working-directory default for a job. See this answer.
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
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
Answer from peterevans on Stack OverflowUpdate: It's now possible to set a working-directory default for a job. See this answer.
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
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
You can now add a default working directory for all steps in a job: docs
For the example here, this would be:
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./app
steps:
- uses: actions/checkout@v1
- name: Setup Symfony
run: .env.dev .env
- name: Install Composer Dependencies
run: composer install --prefer-dist
- name: Run Tests
run: php bin/phpunit
Caveat: this only applies to run steps; eg you'll still need to add the subdirectory to with parameters of uses steps, if required.
How do I change where a GitHub Action runs?
Use working-directory for entire job
Why is GitHub Actions 'doubling' my working directory name?
GitHub actions - get absolute path to working directory - Stack Overflow
Been pulling my hair out over this for a while, I'm a github actions noob, but everything was working fine on a different project.
in my workflow/depoly.yml I have
- name: Print current working directory run: pwd
In the console it prints out
/home/runner/work/TestApp/TestApp
Which makes no sense. because my repository is just
username/TestApp
I even deleted all github files, created a new repo, init push etc. and it does the exact same thing.
I've never had this issue.
This is now apparantly happening in ALL of my github repos - no idea what changed....
Greetings! Trying to figure out if this is even possible, but I'm looking to standardize something via a re-usable workflow.
I would like to pass a directory with source code to a re-useable workflow. The re-useable workflow will then use that passed folder as the working directory:
Here is some dummy code to represent what I'm trying to do, see re-useable workflow.yml:
name: My Re-usable Workflow!
on:
workflow_call:
inputs:
dir:
required: true
type: string
jobs:
run-job:
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Test Hello World!
working-directory: ${{ inputs.dir }}
run: |
chmod +x hello-world.sh
./hello-world.shBelow is my original workflow that calls the re-usable one:
name: Original workflow calling re-useable one
jobs:
call-reusable-workflow:
uses: org/repo/.github/workflows/function1.yml@dev
with:
dir: /my-folderMy workflow above is essentially just passing the string /my-folder, but I would like to pass the entire directory. Inside /my-folder I have a hello world script just so I can test for functionality between passing a directory via workflow.
Would appreciate any guidance and if this is the way to proceed regarding my objective.
You didn't check out the repository on the second job.
Each job runs on a different instance, so you have to checkout it separately for each one of them.
You have to checkout the repository on the runner in order to use files in the repository.
For example, my repository has a /requirements.txt file which I want to pip install -r requirements.txt from, but I have to checkout my repository to ensure the file exists on the runner first.
jobs:
my-job:
name: my job
runs-on: ubuntu-latest
steps:
- name: Setup Python
uses: actions/[email protected]
with:
python-version: 3.11
# without this step, the pip install step will fail
- name: Checkout the repo
uses: actions/checkout@v3
- name: Install Python requirements
run: |
pip install -r requirements.txt
