Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 767
errata Avatar asked Dec 04 '19 16:12

errata


People also ask

How do I rerun actions on GitHub?

Under your repository name, click Actions. In the left sidebar, click the workflow you want to see. From the list of workflow runs, click the name of the run to see the workflow run summary. In the upper-right corner of the workflow, use the Re-run jobs drop-down menu, and select Re-run all jobs.

Are GitHub Actions steps sequential?

Jobs can run at the same time in parallel or run sequentially depending on the status of a previous job. For example, a workflow can have two sequential jobs that build and test code, where the test job is dependent on the status of the build job. If the build job fails, the test job will not run.

Do GitHub Actions jobs run sequentially?

A workflow run is made up of one or more jobs , which run in parallel by default. To run jobs sequentially, you can define dependencies on other jobs using the jobs.

How do I echo variables in GitHub Actions?

You should use run: echo "$GITHUB. REPOSITORY" and run: echo "$GITHUB. REPOSITORY_OWNER" to see them directly on your workflow.


2 Answers

There is some support for this in Github Actions. There is a very good tutorial here that explains how to do it in detail, but essentially what you'll do is split the steps into two jobs. The first job will output a JSON object that will serve as the input to the matrix of the second job.

Here's a simple example:

jobs:

  setup:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.matrix.outputs.value }}
    steps:
      - id: matrix
        run: |
          echo '::set-output name=value::[\"a\", \"b\", \"c\"]'
  build:
    needs: [ setup ]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        value: ${{fromJson(needs.setup.outputs.matrix)}}
    steps:
      - run: |
          echo "${{ matrix.value }}"

like image 185
Michael Meyers Avatar answered Sep 20 '22 01:09

Michael Meyers


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.

like image 26
jjmerelo Avatar answered Sep 20 '22 01:09

jjmerelo