Search code examples
yamlworkflowgithub-actions

Using an array of values to repeat a step in GitHub Actions workflow


I am trying to create a GitHub Actions workflow which would collect specific paths changed in last commit and run a step for each of collected paths, if any.

Currently, in my workflow I'm creating an array of paths, but I'm not sure how to proceed with my array:

name: Test

on:
  push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      # This step will create "an array" of strings, e.g. "path1 path2 path3"
      - name: array
        id: arr
        run: |
          arr=()
          for i in "$(git diff-tree --no-commit-id --name-only -r ${{ github.sha }})"
          do
            if [[ $i == *"path1"* ]]; then
              arr+=("path1")
            fi
            if [[ $i == *"path2"* ]]; then
              arr+=("path2")
            fi
          done
          echo ::set-output name=arr::${arr[@]}

      # How to run this step by iterating the `${{ steps.arr.outputs.arr }}`?
      - name: reviewdog-lint
        uses: reviewdog/action-eslint@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          reporter: github-pr-review
          eslint_flags: 'my_project/some_folder/${{ SINGLE_PATH }}/'  # `SINGLE_PATH` would be a path from the array

Is something like this even possible in the first place? If not, what would be recommended way to loop through some values and use them as arguments in other workflow steps?


Solution

  • Difficult to say without running it, but I would say you need to use the output in the second step by assigning it to a variable, something like:

    env:
              OUTPUT: ${{ steps.id.outputs.arr }}
    

    Then you use $OUTPUT as an environment variable inside the action.

    The problem with that particular action is that it takes one commit at a time. But you can check out the code, it's a shell script. You can fork it from line 15 and make it split input and run a loop over it, applying eslint to every one of them.