Get the Absolute Path of the Default Working Directory in GitHub Actions

This is a note on how to get the absolute path of the default working directory in GitHub Actions.

Introduction

Sometimes, you may need to get the absolute path of the initial working directory in GitHub Actions.

I encountered some confusion while retrieving it, so I’m leaving this note.


※ The source code used for verification is available here:

github.com

Note: This article was translated from my original post.

Getting the Absolute Path of the Default Working Directory

Cannot Retrieve Using ${{ env.GITHUB_WORKSPACE }}

Looking through the documentation, I found an environment variable that seemed appropriate:

GITHUB_WORKSPACE

The default working directory on the runner for steps, and the default location of your repository when using the checkout action. For example, /home/runner/work/my-repo-name/my-repo-name.

I tried referencing this value using the env context as ${{ env.GITHUB_WORKSPACE }}, but it didn’t work.

      - run: |
          echo ${{ env.GITHUB_WORKSPACE }}
# No output

It appears that GITHUB_WORKSPACE is not set in the env context.

Can Be Retrieved Using $GITHUB_WORKSPACE

This value can be obtained by directly referencing the environment variable with $GITHUB_WORKSPACE or ${GITHUB_WORKSPACE}.

      - run: |
          echo $GITHUB_WORKSPACE
# Example output
# /home/runner/work/actions-tests/actions-tests

Can Also Be Retrieved Using ${{ github.workspace }}

You can also get the same value from the workspace property of the github context.

github.workspace

The default working directory on the runner for steps, and the default location of your repository when using the checkout action.

      - run: |
          echo ${{ github.workspace }}
# Example output
# /home/runner/work/actions-tests/actions-tests

Conclusion

This post summarized the methods for getting the absolute path of the default working directory in GitHub Actions.

I realized I had a vague understanding of environment variables and the env context in GitHub Actions, so this was a good opportunity to clarify my thoughts.

I hope this helps someone!

References