How to Run GitHub Actions in Another Directory Using Working Directory
In this blog post, we are going to discuss how to run GitHub Actions in a different directory by using the
working-directory option. In some scenarios, you may want to run specific steps of your workflow within a different directory of your project. For example, if you have a mono-repo setup with different projects in separate directories, you might want to build, test, or deploy each project independently within their own directories.
Working Directory in GitHub Actions
working-directory option in GitHub Actions allows you to define the working directory for a run step. In other words, the command(s) you execute in the
run field will be executed in the directory specified by
Let’s look at a practical example:
name: Working directory example
- name: Checkout code
- name: Execute command in another directory
echo "This command is run in another directory"
Here’s a breakdown of the steps:
name: Working directory example: This is the name of our workflow. You can replace “Working directory example” with any name you want.
on: [push]: This specifies that the workflow will run on any push event.
jobs:: Here we define our jobs. In this case, we have only one job named
runs-on: ubuntu-latest: This specifies that our job will run on the latest version of Ubuntu.
steps:: This is where we define the steps that our job will run.
- name: Checkout code: This is our first step. We are using the
actions/checkout@v2action to checkout our code.
- name: Execute command in another directory: This is our second step where we will execute a command in another directory.
working-directory: ./another-directory: This sets the working directory for the command executed in the
./another-directorywith the path to the directory in which you want to execute your command.
run: |: The
runkeyword is followed by a pipe (
|), indicating we’ll be running multiple commands.
echo "This command is run in another directory": This is the command that will be run in the
Remember that the path you provide in
working-directory should be relative to the root of your repository. Also, the path should exist in your repository; otherwise, you’ll get an error.
working-directory option in GitHub Actions offers a convenient way to execute steps in a specific directory. This is very useful when dealing with mono-repos or any situation where you need to perform actions in different directories.