Github action for projects that use cmake
10K+
ghaction-cmake is a github action for projects that use cmake. By default, it builds, tests and installs the project - but it can as easily run linters, tests with coverage, valgrind or sanitizers, by using presets.
ghaction-cmake runs in phases:
make VERBOSE=1 by default (build commands are shown).ctest --output-on-failure . by default.presetSet a preset, more information on the Presets section below.
dependencies_debianProject dependencies as Debian packages to install in the container, separated by spaces.
working-directoryUse this directory as the source dir for cmake. Mostly used when the cmake project is in a subdirectory of the repository.
cmakeflagsFlags for cmake. -DSOME_OPTION=On, for instance, to pass an option
to CMakeLists.txt.
build_commandCustom test command. Defaults to make VERBOSE=1.
test_commandCustom test command. Defaults to ctest --output-on-failure . if no preset is used.
post_commandCustom command to run after tests. Empty by default, if no preset is used.
cmake is a very versatile tool that can do a lot of different things given
the appropriate arguments. To make matrix builds easier, ghaction-cmake
provides presets that configure those options for specific modes.
The available presets are:
cppcheck: run cppcheck static analysis.
-DCMAKE_C/CXX_CPPCHECK=cppcheck to cmakeflags.iwyu: run include-what-you-use static analysis.
-DCMAKE_C/CXX_INCLUDE_WHAT_YOU_USE=iwyu to cmakeflags.install: test installation.
'-DCMAKE_INSTALL_PREFIX' to cmakeflags.make install as a test.find to show all installed files.clang-tidy: run clang-tidy static analysis.
-DCMAKE_C/CXX_CLANG_TIDY=clang-tidy to cmakeflags.clang-sanitize-<sanitizer>: compile with one of the clang sanitizers and run the tests.
-DCMAKE_C/CXX_COMPILER=clang/clang++ -DCMAKE_C/CXX_FLAGS=-fno-omit-frame-pointer -fsanitize=<sanitizer> to cmakeflags.valgrind: run the tests with valgrind.
ctest -DExperimentalMemCheck --output-on-failure .coverage: runs the tests with coverage.
-DCMAKE_C/CXX_FLAGS=--coverage to cmakeflagslcov -c -d . -o lcov.infoThis preset works well with github actions that upload coverage data results to online services like codecov and coveralls. The example below shows how that can be done.
The table below summarizes the changes specific to each preset:
| Preset | cmake | test | post |
| cppcheck | | (delete) | |
| iwyu | | (delete) | |
| install | | | |
| clang‑tidy | | (delete) | |
| clang‑sanitizer‑<sanitizer> |
| ||
| valgrind | | ||
| coverage | | |
Keep in mind that presets override the defaults, and are overriden by
the other more specific inputs build_command, test_command and
post_command.
The workflow below shows how to use presets in a matrix job:
---
name: CI
on: [push, pull_request]
jobs:
# Regular C build with two compilers, using the environment:
build_using_compiler_in_environment:
strategy:
matrix:
compiler:
- gcc
- clang
runs-on: ubuntu-latest
# We can use cmakeflags for this, or we can just use
# regular environment variables, as they are already
# supported by github actions:
env:
- CC: ${{ matrix.compiler }}
steps:
- uses: actions/checkout@v2
- uses: docker://lpenz/ghaction-cmake:v0.10
# Regular C build with two compilers, using cmakeflags:
build_using_compiler_in_cmakeflags:
strategy:
matrix:
compiler:
- gcc
- clang
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# This examples uses the appropriate cmakeflags
- uses: docker://lpenz/ghaction-cmake:v0.10
with:
cmakeflags: ${{ format('-DCMAKE_C_COMPILER={0}', matrix.compiler) }}
# Coverage with codecov:
codecov:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker://lpenz/ghaction-cmake:v0.10
with:
preset: coverage
# ghaction-cmake works well with the github action
# provided by codecov:
- uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
# Coverage with coveralls:
coveralls:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker://lpenz/ghaction-cmake:v0.10
with:
preset: coverage
# ghaction-cmake works well with the github action
# provided by coveralls if you pass path-to-lcov:
- uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: lcov.info
# Static analyzers:
linters:
strategy:
matrix:
preset: [ cppcheck, iwyu, clang-tidy ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker://lpenz/ghaction-cmake:v0.10
with:
preset: ${{ matrix.preset }}
# Tests with various sanitizers and valgrind:
test:
strategy:
matrix:
preset:
- clang-sanitizer-address
- clang-sanitizer-memory
- clang-sanitizer-undefined
- clang-sanitizer-dataflow
- clang-sanitizer-safe-stack
- valgrind
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker://lpenz/ghaction-cmake:v0.10
with:
preset: ${{ matrix.preset }}
# Test installation:
install:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker://lpenz/ghaction-cmake:v0.10
with:
preset: install
Note that the file above splits static analyzers from sanitizers, but they can actually be in the same matrix job, as the rest of the parameters is the same.
This github action is actually a docker image that can be used locally or even in travis-ci. To do that, first download the image from docker hub:
docker pull lpenz/ghaction-cmake:v0.10
Then, run a container in the project's directory, for instance:
docker run --rm -t -u "$UID" -w "$PWD" -v "${PWD}:${PWD}" -e INPUT_PRESET=valgrind lpenz/ghaction-cmake:v0.10
It's worth pointing out that action parameters are passed as
upper case environment variables, prefixed with INPUT_.
The following .travis.yml runs the same thing in travis-ci:
---
language: generic
jobs:
include:
- install: docker pull lpenz/ghaction-cmake:v0.10
- script: docker run --rm -t -u "$UID" -w "$PWD" -v "${PWD}:${PWD}" -e INPUT_PRESET=valgrind lpenz/ghaction-cmake:v0.10
Content type
Image
Digest
sha256:8bc67e9a5…
Size
349.9 MB
Last updated
over 1 year ago
docker pull lpenz/ghaction-cmake:0.23.0-debian-bookworm-arm64