Adding a binary to the PATH from a Docker GitHub Action for use by later workflow steps
Asked Answered
F

2

7

I am trying to create a Dockerfile based action that adds a program to the $PATH so that it can be used by later actions. My action runs code like this:

mkdir -p $GITHUB_WORKSPACE/bin
echo "echo Hello, world!" > $GITHUB_WORKSPACE/bin/hello-world
echo "::add-path::$GITHUB_WORKSPACE/bin"

My test workflow uses this like so:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
      - name: Add program to path
        uses: ./
      - name: Use program
        run: hello-world

This fails because while the program has been added to $GITHUB_WORKSPACE/bin/hello-world the value of $GITHUB_WORKSPACE is different in the action and in the workspace step.

In the action it is /github/workspace/, while in the workflow it is /home/runner/work/setup-gleam/setup-gleam/, so the $PATH addition set by the action is not correct.

How can I add a file to a directory from a dockerfile based GitHub action so that it is on the path for the rest of the workflow? It seems that there is no writable $PATH directory shared between dockerfile actions and non-dockerfile actions.

Formerly answered 15/11, 2019 at 20:41 Comment(1)
Does this answer your question? How to update the PATH in a github action workflow file for a windows-latest hosted runnerIodic
E
1

Runner path is stored in $RUNNER_WORKSPACE environment variable and can be used to get the right path.

echo "::add-path::$GITHUB_WORKSPACE/bin" # Make it accessible from docker containers
echo "::add-path::$RUNNER_WORKSPACE/$(basename $GITHUB_REPOSITORY)/bin" # Make it accessible from runner

But it looks more like a workaround than solution.

Equalizer answered 27/12, 2019 at 16:42 Comment(1)
Error: The add-path command is disabled. Please upgrade to using Environment Files or opt into unsecure command execution by setting the ACTIONS_ALLOW_UNSECURE_COMMANDS environment variable to true. For more information see: github.blog/changelog/…Iodic
G
0

As of 2024 the approach to add something to path would be to use echo "$HOME/.local/bin" >> $GITHUB_PATH

So your action would have to look like:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
      - name: Add program to path
        run: |
          mkdir -p $HOME/.local/bin
          echo "$HOME/.local/bin" >> $GITHUB_PATH
          cp hello-world $HOME/.local/bin/hello-world

      - name: Use program
        run: hello-world

https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-adding-a-system-path

Glomerulus answered 16/7 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.