GitLab CI pipelines
You can start by having a quick overview of the official introduction to pipelines and jobs documentation.
We are going to see how to make a pipeline as follows :
graph LR;
subgraph build
B(Build);
end
subgraph test
C(Unit tests);
D(Warnings tests);
end
B-->C
B-->D
Artifacts
In order to use compilation build results in another test target, build results have to be saved as an artifact.
Let's modify the .gitlab-ci.yml
file as follows :
stages:
- build
- test
job_build:
stage: build
tags:
- linux
script:
- cd cxx
- rm -rf build
- mkdir build
- cd build
- cmake -DCMAKE_BUILD_TYPE=Debug ..
- make 2>&1 | tee build.log
artifacts:
expire_in: 2 hrs
paths:
- cxx/build/
- the
test
stage is added -
make
command outputs in the standard output, but also in abuild.log
file -
cxx/build
folder is saved as an artifact -
ctest --output-on-failure
to launch the unitary tests has been removed (it will have to be done on the following exercise).
cxx/build/
folder is present in the
.gitignore
file.
Exercise
Add a job_unit_test
as a test
stage that runs the unitary tests of the
project from the job_build
artefact.
job_build
.
Commit and push the .gitlab-ci.yml
file and check the job_unit_test
Tips and tricks
You can use GitLab CI linter
to validate your .gitlab-ci.yml
file before pushing it.
Allow failure
Some tests require to be passed (like unitary tests). We would like to have a
state passed with warnings
on some other tests.
We are going to use
allow_failure
in order to reach this state for compilation warnings.
Exercise
The command grep "warning:" build.log
returns 0 (SUCCESS) when a warning is
found in build.log
file and 1 (FAILURE) when none was found. This is the
exact contrary of what we want to do with GitLab (simulate the -Werror
mecanism of the compiler g++
). This is why we are going to use the following script:
grep "warning:" build.log; if [ $? -eq 1 ]; then exit 0; else exit 1; fi
as a new test
stage that will be called job_warnings_test
in the
.gitlab-ci.yml
file.
job_build
.
Commit and push the .gitlab-ci.yml
file and check the job_warnings_test
fails, but is allowed to fail. This corresponds to the state passed with warnings
We will fix it later.
Warnings and tests fix
googletest
provides a macro for this:
EXPECT_NEAR(expected, actual, absolute_error)
Exercise
Fix the warning and the test failure in tests/TestSphere.cpp
using this
function. 1e-5
is a reasonable absolute error value.
Commit and push the tests/TestSphere.cpp
file and check the compilation and
tests are now passed with warnings (because a warning in bench.cpp is
remaining).
Home | C++ Home | << C++ Previous - Compilation warnings | >> C++ Next - Code coverage