Dynamic Node.js versions in Github Actions matrix

  1. Intro
  2. JSON data
  3. Action workflow

Intro

Publishing over 40 Node.js packages is okay for a hobby, but a pain to keep up with the Node.js updates. To automate the testing I had already built a unit-test runner. Later I added Travis and Github actions to execute those tests when new code is being pushed or a dependency is updated. For the most part that workflow is fine. But when it comes to the framework releases of Node itself you still have to manually update the .travis.yml or Github workflow files. Of course I had scripted the generation of those files but it still required manual interactions.

Until today. In just a couple of hours of researching an article by Tomas Vortruba and lots of trial and error, I build this Github workflow that retrieves the current Node.js LTS versions from a JSON file. Then it will dynamically create a build matrix from the data. And finally it runs the normal CI script.

Now I only need to find a way to tell the repos about new versions. Probably going to use a dispatch event.

JSON data

The build matrix is loaded from a remote JSON file. This can be a PHP script or whatever. In the example I use a Github hosted (raw) file. For the JSON data it is important to not use line breaks.

// This works
[ "16", "14", "12" ]

// And this will not
[
  "16",
  "14",
  "12"
]

Action workflow

Below is the whole workflow action with comments at the relevant lines.

name: "Node.js"

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]

jobs:
  lts_versions:
    name: "Get versions"
    runs-on: ubuntu-latest
    steps:
      - id: set-matrix
        run: echo "::set-output name=matrix::$(curl -s https://raw.githubusercontent.com/fvdm/nodejs-versions/main/lts.json)"
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}

  build:
    name: "Node"
    needs: lts_versions
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: ${{ fromJson(needs.lts_versions.outputs.matrix) }}
    steps:
      - uses: actions/checkout@v2
      - name: Test on Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - run: git fetch --prune --unshallow
      - run: npm install
      - run: npm test
      - run: npm audit --production
      - name: Coveralls Parallel
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          flag-name: node-${{ matrix.node-version }}
          parallel: true

  finish:
    name: "Finish"
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Coveralls Finished
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true

Like this article?
Buy me a coffee

Related stuff

Leave a Comment

Your email address will not be published. Required fields are marked *