Status badges for matrix builds #52616
Replies: 1 comment
-
While such a feature seems like a great idea, in my opinion, I wanted to suggest a solution that is practical now. I came across this post searching for a similar situation on Google, where this was the first ranked result, so I think giving a practical solution for GitHub Actions as it is will help people searching for an answer, since that was the adventure I just took myself, haha. In short, the answer is forego matrices in favour of reusable workflows. This solves the code duplication issue mentioned by @patrikhuber. I will show a practical example that aligns with your problem specifically, but the solution can be adapted for other use-cases. Badges for Multiple Operating SystemsMatrix Solution, No Individual BadgesThe following is a concrete implementation using matrices, which I will use to illustrate adapting to a reusable workflow. main.ymlname: CI
on: push
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
go-version: 1.22
- name: Build application
run: go build -o api ./cmd/... Reusable Workflows, Individual Badgesmain.ymlname: CI
on:
# workflow_call is a required field of the "on" property,
# when intending to use the workflow as a reusable workflow
workflow_call:
inputs:
os:
required: true
type: string
jobs:
build:
runs-on: ${{ inputs.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
go-version: 1.22
- name: Build application
run: go build -o api ./cmd/... ubuntu-latest.ymlname: ubuntu-latest
on: push
jobs:
ubuntu-latest-build:
uses: ./.github/workflows/main.yml
with:
os: ubuntu-latest windows-latest.ymlname: windows-latest
on: push
jobs:
windows-latest-build:
uses: ./.github/workflows/main.yml
with:
os: windows-latest The badges can then be added to the
Replacing You can see this in action at this repository: https://github.com/rickydodd/reusable-workflows-example This can be adapted for what was my problem, too, which was having individual badges for different Go versions. You can also adapt it with another layer of indirection to have both different operating systems and different Go versions. I believe the limit is four layers of nested reusable workflows. Hope this helped somebody, even if it is unfashionably late. :) |
Beta Was this translation helpful? Give feedback.
-
Select Topic Area
Product Feedback
Body
Hi,
I'm using GitHub Actions to CI-build a software on Linux, Windows and macOS. I'm using a build matrix for that. However using the current badges, only a blanked "success" or "failing" is displayed. I'd like to display separate badges for the Linux, Windows and macOS builds.
Of course, I could create three separate jobs / workflow files, one for each platform - but that would mean a lot of workflow code duplication and it defeats the purpose of matrix builds.
I was able to find one or two third-party websites for this purpose but neither of them looked like it would still be here in a couple years' time (e.g. https://github.com/40ants/github-actions) and I'd like something (ideally official) that I can trust that it will keep working.
I've reported this in actions/runner-images#7413 but got directed here.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions