16

I want to run GitHub Actions on more than one environment, let's say on Windows and Linux. I managed to do it with Travis CI, but I could not find information about how to do it with GitHub Actions.

Has anyone tried it?

This is my nodejs.yml.

name: Node CI

on: [push]

jobs:
    build:
        runs-on: ubuntu-latest

        strategy:
            matrix:
                node-version: [12.x]

        steps:
            - uses: actions/checkout@v1
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}
            - name: npm install
              run: |
                  npm ci
            - name: prettier format check
              run: |
                  npm run prettier-check
            - name: lint format check
              run: |
                  npm run lint-check
            - name: build, and test
              run: |
                  npm run build
                  npm test --if-present
              env:
                  CI: true
Pang
  • 9,564
  • 146
  • 81
  • 122
Yordan Kanchelov
  • 511
  • 9
  • 26

1 Answers1

17

You can use a strategy / matrix for that (see https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategy)

name: Node CI

on: [push]

jobs:
    build:
        runs-on: ${{ matrix.os }}

        strategy:
            matrix:
                os: [ubuntu-latest, windows-latest]
                node-version: [12.x]

        steps:
            - uses: actions/checkout@v1
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}
            - name: npm install
              run: |
                  npm ci
            - name: prettier format check
              run: |
                  npm run prettier-check
            - name: lint format check
              run: |
                  npm run lint-check
            - name: build, and test
              run: |
                  npm run build
                  npm test --if-present
              env:
                  CI: true

riQQ
  • 9,878
  • 7
  • 49
  • 66
  • Works like a charm - and adheres to DRY principle better then duplicating the same workflow solely to have it build on multiple environments. – jonashackt Feb 04 '21 at 20:21
  • Many thanks, indeed. Would like to add that you can use the ${{ matrix.os }} in regular shell scripts, e.g. to allow for the installation of os-specific additional packages. – smoe Aug 28 '23 at 18:05