3

I'm trying to keep multiple github actions in the same monorepo using subdirectories, and run them like:

workflow.yml

// [...]
jobs:
  run_my_script:
    runs-on: ubuntu-latest
    steps:
      - name: Check out current repo
        uses: actions/checkout@v2
      - uses: ./my_action2
        with:
          my_input_var: "david"

./my_action2/action.yml

// [...]
runs:
  using: "composite"
  steps:
    # Checkout files in this repo
    - name: Checkout
      uses: actions/checkout@v1

    - name: Run myscript
      run: python myscript.py "${{ inputs.my_input_var }}" # location: ./my_action2/myscript.py
      shell: bash

The problem I'm having is that my action uses a python script in it's subdirectory, but the uses: action appears to run from the GITHUB_WORKING_DIR of the workflow and not the directory of the action itself.

python: can't open file 'myscript.py': [Errno 2] No such file or directory

I've looked through most of the working-directory questions surrounding github actions, but I'm still stumped.

I've also tried adding working-directory: ./my_action2 to the job's defaults: but it looks like it's not propagating to run: commands within the uses: step.

My workaround in the meantime has been to add an input for myaction2_working_directory in the workflow, and then add working-directory: ${{ inputs.myaction2_working_directory }} to every run: command in the action. This seems inelegant and repetitive. Is there a better way to do this?

David
  • 53
  • 1
  • 7

2 Answers2

3

I had similar problem and for my composite actions I just added a first step as:

run: cd ${{ inputs.working_directory }}

and then all next steps are running in it. I couldn't find a better way and having working-directory copy pasted was also something I didn't like.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • 2
    Thank you - this helps a lot - I wasn't aware that the location would remain between multiple steps after changing directory. I'll give it a shot and mark as correct – David Mar 01 '22 at 17:44
2

contrary to the answer by Grzegorz, you cannot just run: cd foo and then expect all following steps to have a working directory of foo. as far as i can tell, the only way to do this is with the "workaround" the OP already posted -- add an input named e.g. working-directory to your action and then add working-directory: ${{ inputs.working-directory }} to every step. see https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsworking-directory

mziwisky
  • 335
  • 1
  • 12