how to have access to the files in the composite github actions directory

1.6k views Asked by At

I am trying to store the scripts of my composite github actions in the directory of action. let me explain by an example

  • Suppose I am in the myorg organization
  • I create a repository named actions
  • I create a directory named mydemo
  • I create a file mydemo/action.yaml
  • I create another file mydemo/echo.sh

In mydemo/action.yaml I have:

name: "mydemo"
description: ""

runs:
  using: "composite"
  steps:
    - name: echo hello world
      run: |
        source ${{ github.action_path }}/echo.sh
      shell: bash

and in mydemo/echo.sh I have:

echo "Hello World"

Then I create a tag named v1

I also provide access in settings so that it can be used as a composite action

However I have an error

It tries to run source /apps/ghe-actions/_work/_actions/myorg/mydemo/v1/mydemo/echo.sh

And I see this error

/apps/ghe-actions/_work/_actions/myorg/mydemo/v1/mydemo/echo.sh: No such file or directory

How can I run the echo.sh in this example?

2

There are 2 answers

5
jessehouwing On

Use the ${{ github.action_path }} magic variable:

github.action_path string The path where an action is located. This property is only supported in composite actions. You can use this path to access files located in the same repository as the action, for example by changing directories to the path: cd ${{ github.action_path }} .

See: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

This should work under "normal circumstances" - when not running in a container, but on a real machine or VM - but there are a number of situations where the runner assigns the wrong value. In that case, rely on the GITHUB_ACTION_PATH environment variable.

0
Amin Ba On

changed too source source ${{ github.action_path }}/echo.sh to source ${GITHUB_ACTION_PATH}/echo.sh and it worked

This helped me https://github.com/actions/runner/issues/716#issuecomment-795238933