Search code examples
yamlgithub-actions

GitHub Actions pass list of variables to shell script


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/


Solution

  • 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.