4

Using GitHub Actions, I would like to invoke a shell script with a list of directories. (Essentially equivalent to passing an Ansible vars list to the shell script)

I don't really know how, is this even possible? Here's what I have until now, how could one improve this?

name: CI

on:
  push:
    branches:
      - master

    tags:
      - v*

  pull_request:

jobs:
  run-script:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2

      - name: Run script on targets
        run: ./.github/workflows/script.sh {{ targets }}
        env:
          targets:
            - FolderA/SubfolderA/
            - FolderB/SubfolderB/

4
  • I don't think you can set arrays in the environment. For what you show, you could just insert the strings directly in the run: field; where do you really get the targets from? Commented Dec 17, 2020 at 17:33
  • One option could be to use the join function with a blank, but that requires that your directory names don't contain blanks. Commented Dec 17, 2020 at 17:34
  • That's no problem, folders don't contain blanks. Would I be able to specify the path to the subfolders separately from the run ? Commented Dec 17, 2020 at 17:36
  • For a robust solution, you could use a separate step where you set the directories and then reference them in your "Run script on targets" step. The previous step could write to a temporary file, or set an output with a JSON object to then reference... Commented Dec 17, 2020 at 17:44

1 Answer 1

4

Today I was able to do this with the following YAML (truncated):

...
with:
  targets: |
    FolderA/SubfolderA
    FolderB/SubfolderB

The actual GitHub Action passes this as an argument like the following:

runs:
  using: docker
  image: Dockerfile
  args:
    - "${{ inputs.targets }}"

What this does is simply sends the parameters as a string with the newline characters embedded, which can then be iterated over similar to an array in a POSIX-compliant manner via the following shell code:

#!/bin/sh -l

targets="${1}"

for target in $targets
do
  echo "Proof that this code works: $target"
done

Which should be capable of accomplishing your desired task, if I understand the question correctly. You can always run something like sh ./script.sh $target in the loop if your use case requires it.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Aaron, do you have a successful run in i.e. a public shareable GitHub action so as I can verify and approve your answer? The second option involving a loop doesn't address the problem fully, since 2 independent runs might run correctly but 2 dependent runs might not (depending on your core logic.)
@MihaiGalos sure! I just actually finished a GitHub Action repository today that depends on this logic, you can see a successful run of it here: github.com/meese-enterprises/meeseOS/runs/…. Let me know if you can't see it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.