Compile and test a C++ application

Using Codefresh pipelines

Codefresh can work with any C/C++ application very easily as both gcc and g++ are already offered in Dockerhub. There is also another example available with C and make.

The example C++ project

You can see the example project at https://github.com/codefresh-contrib/cpp-sample-app. The repository contains a C++ starter project with a CMakeLists.txt file:

  • cmake . creates the makefiles.
  • make test runs unit tests
  • make compiles the code

The project is also using the boost testing libraries.

Cmake, g++ and Docker

Creating a CI/CD pipeline for C is very easy, because Codefresh can run any gcc image that you wish. Gcc docker images already contain the make utility but not the the cmake one. Therefore we will first create a Dockerfile that has g++, cmake and the boost libraries. You can follow the same pattern for other development tools that you use.

Here is the Dockerfile:

Dockerfile

FROM gcc:9.2

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y cmake libgtest-dev libboost-test-dev && rm -rf /var/lib/apt/lists/* 

CMD ["cmake"]

This docker build does the following:

  1. Starts from the GCC image
  2. Installs cmake and boost
  3. Sets cmake as the default command

Create a CI pipeline for C++ applications

We can now use the custom Docker image in order to compile/test the C++ application:

Compiling a C++ application in a pipeline

Compiling a C++ application in a pipeline

Here is the full pipeline that compiles the application after checking out the code.

codefresh.yml

version: '1.0'
stages:
  - checkout
  - prepare
  - build
steps:
  main_clone:
    title: Cloning main repository...
    stage: checkout
    type: git-clone
    repo: 'codefresh-contrib/cpp-sample-app'
    revision: master
    git: github
  build_dev_image:
    title: Building Dev Image
    stage: prepare
    type: build
    image_name: cmake
    working_directory: ./dev/
    tag: 'latest'
    dockerfile: Dockerfile
  create_makefiles:
    title: Create Makefiles
    stage: prepare
    image: ${{build_dev_image}}
    commands:
      - cmake .
  compile_my_sources:
    title: Compile
    stage: build
    image: ${{build_dev_image}}
    commands:
      - make
  run_my_tests:
    title: Test
    stage: build
    image: ${{build_dev_image}}
    commands:
      - make test     

This pipeline:

  1. clones the source code
  2. Creates a development docker image that has g++, cmake and boost
  3. Runs cmake on the source code to create the make files
  4. Compiles the source code
  5. Runs unit tests

You can add additional tools in the pipeline by extending the Dockerfile mentioned in the previous section. You can also change the version of Gcc/g++ by starting from a different public or private Docker image.

C example
Codefresh YAML for pipeline definitions
Steps in pipelines
Creating pipelines
How Codefresh pipelines work