Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

John  Ajera
John Ajera

Posted on

     

Understanding GitHub Actions Working Directory

Understanding GitHub Actions Working Directory

GitHub Actions provides a powerful platform for automating workflows directly within your GitHub repository. A key part of understanding how workflows operate is knowing where files are stored during execution. In this article, we'll explore the default working directory, where action files are downloaded, and how you can manage these paths effectively.


Default Working Directory

Path

/home/runner/work/<repository-name>/<repository-name>
Enter fullscreen modeExit fullscreen mode

Example Structure

/home/runner/work/├── my-project│   └── my-project(repository files go here)
Enter fullscreen modeExit fullscreen mode

Setting Working Directory

You can set theworking-directory at different levels in your workflow file. Here’s how:

Workflow Level

Define thedefault-working-directory for the entire workflow in thedefaults section:

defaults:run:working-directory:./global-scripts
Enter fullscreen modeExit fullscreen mode

Effect: Allrun commands in the workflow will execute from the./global-scripts directory unless overridden.

Job Level

Set theworking-directory for all steps within a specific job:

jobs:example-job:runs-on:ubuntu-latestdefaults:run:working-directory:./job-scriptssteps:-name:Run job-level scriptrun:./script.sh
Enter fullscreen modeExit fullscreen mode

Effect: Allrun commands in theexample-job will execute from the./job-scripts directory.

Step Level

Set theworking-directory for an individual step:

steps:-name:Run step-level scriptrun:./script.shworking-directory:./step-scripts
Enter fullscreen modeExit fullscreen mode

Effect: Only this specific step will execute from the./step-scripts directory.


Environment Variables Related to Paths

GitHub Actions runners expose several environment variables that you can use to dynamically reference paths during workflows. These include:

VariableDescription
GITHUB_WORKSPACEThe default working directory of the repository. Equivalent to/home/runner/work/<repo>/<repo>.
RUNNER_TEMPPath to a directory for temporary files. Equivalent to/home/runner/work/_temp.
RUNNER_TOOL_CACHEDirectory for cached tools and dependencies. Equivalent to/opt/hostedtoolcache.
GITHUB_ACTION_PATHPath to the action's files when using a custom or third-party action.
GITHUB_ENVFile path for exporting environment variables for subsequent steps.
GITHUB_PATHFile path for appending to the systemPATH for subsequent steps.
GITHUB_REFThe full reference (branch or tag) that triggered the workflow.

Example Usage

Accessing a Tag Dynamically

If your workflow is triggered by a tag, you can extract the tag name fromGITHUB_REF:

steps:-name:Extract Tag Namerun:echo "Tag:${GITHUB_REF#refs/tags/}"
Enter fullscreen modeExit fullscreen mode

You can use this dynamic value to construct paths:

steps:-name:Run with Dynamic Tag Pathrun:echo "Using path /home/runner/work/_actions/my-actions/my-actions/v1/"
Enter fullscreen modeExit fullscreen mode

Referencing the Repository Workspace

steps:-name:Use Workspacerun:ls $GITHUB_WORKSPACE
Enter fullscreen modeExit fullscreen mode

Storing Temporary Files

steps:-name:Create Temp Filerun:echo "Temporary data" > $RUNNER_TEMP/temp.txt
Enter fullscreen modeExit fullscreen mode

Using Cached Tools

steps:-name:Check Tool Cacherun:ls $RUNNER_TOOL_CACHE
Enter fullscreen modeExit fullscreen mode

Where Do Action Files Go?

Path

/home/runner/work/_actions
Enter fullscreen modeExit fullscreen mode

Example Structure

For the actionactions/checkout@v4:

/home/runner/work/_actions/├── actions│   └── checkout│       └── v4(action files)
Enter fullscreen modeExit fullscreen mode

Accessing Custom Files

If you have a custom filehello-world.py included in a custom actionmy-actions/my-actions@v1:

/home/runner/work/_actions/├── my-actions│   └── my-actions│       └── v1│           └── hello-world.py
Enter fullscreen modeExit fullscreen mode

Example Workflow Step

steps:-name:Run custom Python scriptrun:python /home/runner/work/_actions/my-actions/my-actions/v1/hello-world.py
Enter fullscreen modeExit fullscreen mode

Temporary and Cache Directories

Temporary Directory

/home/runner/work/_temp
Enter fullscreen modeExit fullscreen mode

Example 1 Structure

/home/runner/work/├── _temp    └──(temporary files)
Enter fullscreen modeExit fullscreen mode

Tool Cache Directory

/opt/hostedtoolcache
Enter fullscreen modeExit fullscreen mode

Example 2 Structure

/opt/hostedtoolcache/├──(cached tools and dependencies)
Enter fullscreen modeExit fullscreen mode

Cleaning Up Directories

All files and directories created during a workflow run are ephemeral and automatically cleaned up on GitHub-hosted runners. For self-hosted runners, you may need to manage cleanup manually to avoid storage issues.


Summary of Key Paths

PathPurpose
/home/runner/work/<repo>/<repo>Default working directory for workflows.
/home/runner/work/_actionsDirectory for downloaded action files.
/home/runner/work/_tempTemporary file storage for workflows.
/opt/hostedtoolcacheTool cache for reusable dependencies.

Best Practices

  1. Useworking-directory Option: Set theworking-directory explicitly for custom workflows.
  2. Avoid Hardcoding Paths: Use environment variables likeGITHUB_WORKSPACE andRUNNER_TEMP for compatibility.
  3. Extract Tags Dynamically: UseGITHUB_REF to dynamically determine tags and construct paths.
  4. Clean Up for Self-Hosted Runners: Implement cleanup scripts to manage storage effectively.

By understanding the directory structure of GitHub Actions runners, you can design workflows that are more efficient, portable, and easier to debug. Happy automating!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
mezieb profile image
Okoro chimezie bright
I love human development,education ,technology and have great passion in web development.
  • Email
  • Location
    vietnam, hanoi city.
  • Education
    fpt university (vietnam,hanoi city)
  • Work
    FULL-STACK WEB DEVELOPER (freelancer)
  • Joined

Nice work thanks for sharing

CollapseExpand
 
urbanisierung profile image
Adam
Fullstack Engineer • NodeJS, TypeScript, Serverless • join my newsletter at weeklyfoo.com • lifosy.com • boringdevtools.com • linker1.com • flethy.com • ethme.at • diypunks.xyz
  • Location
    Southern Germany
  • Education
    KIT
  • Work
    Fullstack Engineer at Camunda
  • Joined

Very helpful, thanks!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Platform Engineer
  • Location
    Wellington, New Zealand
  • Work
    Earth Sciences New Zealand
  • Joined

More fromJohn Ajera

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp