[CI/CD](EN) Usage and example of github actions matrix

Usage and example of github actions matrix


Environment and Prerequisite

  • Github Actions


Background


Usage

  • According to the document, you can use it as shown below.
  • jobs.<job_id>.strategy.matrix is where you define the variables and values for the matrix.
  • In the example below, the values corresponding to version are [10, 12, 14] and the same applies to os.
jobs:
  example_matrix:
    strategy:
      matrix:
        version: [10, 12, 14]
        os: [ubuntu-latest, windows-latest]


Example

If you want to run Jekyll builds on multiple OS, you can write it as shown below. This is a workflow example actually used in this blog.

Code

  • This include other steps but just look on strategy in jobs’s build.
  • Define os on statement os: [ubuntu-22.04, ubuntu-20.04, macos-latest] and use it on runs-on: ${{ matrix.os }}.
name: Jekyll Build CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:
    inputs:
      ruby-version:
        required: true
        type: string
        default: "3.2.2"
        
jobs:
  build:
    name: jekyll build
    strategy:
      matrix:
        os: [ubuntu-22.04, ubuntu-20.04, macos-latest]
      fail-fast: false
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout Repository
      uses: actions/checkout@v4
    - name: Setup Ruby
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: ${{ github.event.inputs.ruby-version }}
    - name: Install Bundler
      run: |
        gem install bundler
    - name: Install Dependencies
      run: |
        bundle install
    - name: Build Jekyll Site
      run: |
        bundle exec jekyll build

Result

  • To make it easier to understand, I’m attaching an image that shows the results of the execution.


Reference