Reputation: 15886
I love GitHub Actions' Matrix feature that allows me to build many combinations of my output.
However, I'd love to also use it as a kind of for-loop, but on the same build agent (so that it doesn't run in parallel, and has access to previous outputs).
Is that somehow possible?
Upvotes: 6
Views: 5002
Reputation: 14776
Judging by everything the matrix documentation has to say, it seems that as soon as you define a matrix strategy, you get a number of jobs equal to the number of elements in your matrix (even if you don't use the value at all).
This little workflow demonstrates it:
name: Test
on:
push: { branches: master }
jobs:
test:
name: Matrix test
runs-on: ubuntu-latest
strategy:
matrix: { ruby: ['2.4', '2.5'] }
steps:
- name: Checkout code
uses: actions/checkout@v2
# - name: Do something with the matrix value
# run: echo Doing it with ${{ matrix.ruby }}
Output:
If you want to run consecutive (and not parallel) operations, especially if one step relies on the output of the previous step, it sounds like your best option is to have your build logic tucked away in a shell script, and have a job that runs it.
Since your use case is not described in the question, I would like to propose another approach which I am using for many of my production workflows, and might be suitable for your needs.
I also need to have sort of "loops" in some of my workflows (for example, for deploying multiple docker containers based on the same built image). For this, I am using a templating engine to create a YAML file that has loops in it, and I then generate the final YAML, which is standard and valid, and contains the same steps over and over again.
The tool I am using to do this is open source (full disclosure, I also built it) and is called Kojo, and was designed specifically for this purpose of configuration templating - but you can do it with any templating engine of your choice.
Upvotes: 3