baitendbidz
baitendbidz

Reputation: 893

Is it possible to loop a GitHub Actions step?

I have a GitHub Actions workflow dealing with new releases. Some last steps build the application for different platforms. Instead of creating multiple steps where each one builds for a different platform or creating a step running multiple commands I'm looking for a way to loop a step for each item in an array.

I know there is a matrix for jobs so this is my pseudo implementation to show what I'm looking for:

jobs:
  do_it:
    runs-on: ubuntu-latest

    steps:
      - name: For each entry in the array...
        strategy:
          matrix:
            target: [ this, that, something ]
        run: echo ${{ matrix.target }}

Is it possible to create something similar to a matrix so it will loop the step multiple times?

As a side note, I know there is a similar question but I don't want to split the job into multiple jobs because then I have to deal with all the build artifacts.

Upvotes: 14

Views: 4632

Answers (2)

Jallrich
Jallrich

Reputation: 469

If your action uses the workflow_dispatch event, you can trigger the action again by running a step that runs that same workflow:

- run: gh workflow run run-tests.yml
  env:
    GH_TOKEN: ${{ github.token }}

Upvotes: 0

Fcmam5
Fcmam5

Reputation: 6832

Posting this "late" answer hoping that someone finds it useful:

There is no direct or "native" way to loop through a GitHub action step, but you can achieve that with 2 different approaches:

  • Designing your workflow to have a job with a matrix, then create another job that depends on that matrix, you can share build artifacts between workflows with job outputs and upload/download actions
  • Create a custom (composite) action that takes inputs as an array (idea/workaround), then loop through your inputs with bash or JavaScript (if you use JS actions).

Upvotes: 0

Related Questions