AI Hints
Home›CI/CD with GitHub Actions Tutorial›Concurrency and Practice
CI/CD with GitHub Actions · Lesson 56 of 138

Working Directory and Shell

Where and how steps run.

⏱ 2 min read▶ Video at 1:03:18🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Steps run in the repository's top folder, using bash. But in a project with several parts, you might want steps to run inside a sub folder. And sometimes a different language is easier than bash. Two keys handle this.

Working Directory and Shell
From the video: Working Directory and Shell
  • Steps start in the top folder
  • Projects have sub folders
  • Bash isn't always easiest

defaults and shell

Defaults, run, working directory makes every run step in this job start inside services slash api. And shell, set to python, runs a step's script with Python instead of bash. That's handy for small bits of logic that would be awkward in shell.

.github/workflows/monorepo.ymlyaml
name: Monorepo
on: push
jobs:
  api:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: services/api
    steps:
      - run: pwd | sed "s|$HOME/||"
      - run: ls
      - name: A Python step
        shell: python
        run: |
          import platform
          print("Python", platform.python_version(), "says hello")

Inside the sub folder

The first step prints the folder, services slash api. LS lists that folder's files, not the top level ones. And the Python step runs real Python code, printing its version. Each key changed where, or how, the steps ran.

Terminal
run-steps .github/workflows/monorepo.yml
Workflow: Monorepo  (event: push, branch: main)
Job: api
  ✓ Run pwd | sed "s|$HOME/||"
      shop/services/api
  ✓ Run ls
      main.py
      requirements.txt
  ✓ A Python step
      Python 3.12.3 says hello
  Job succeeded

Summary

Working directory changes where steps run, and shell changes what runs them. Next, practice with a matrix pipeline.

Key takeaways
  • working-directory:
  • defaults: run:
  • shell: python
Next up: Practice: A Matrix Pipeline — your turn