GitHub Actions with CMake

The GitHub Actions CI runners have a recent release of CMake and update regularly, often within weeks of a new CMake release. There are Actions in the GitHub Marketplace allowing one to pick specific CMake versions for testing the build system if desired. CMake environment variables are handy to set in top level “env” to avoid forgetting to specify them.

Note that Windows compiler tests in general, including CMake, are often done with distinct setups. These are best shown by example using MSYS2 or MSVC.

name: ci

env:
  CTEST_NO_TESTS_ACTION: error
  CTEST_PARALLEL_LEVEL: 4
  CMAKE_BUILD_PARALLEL_LEVEL: 4
  HOMEBREW_NO_INSTALL_CLEANUP: 1

on:
  push:
    paths:
      - "**.c"
      - "**.cpp"
      - "**.h"
      - "**/CMakeLists.txt"
      - "**.cmake"
      - ".github/workflows/ci.yml"

jobs:

  core:

    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        shared: [true, false]

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

    steps:

   - uses: actions/checkout@v4

    - name: config shared=${{ matrix.shared }}
      run: >-
        cmake
        -Bbuild
        -DBUILD_SHARED_LIBS=${{ matrix.shared }}        

    - name: build
      run: cmake --build build

    - name: test
      run: ctest --test-dir build -V