Build a C++ app with Jenkins

This tutorial shows you how to use Jenkins to build a simple C++ application, specifically a Casino Number Guessing Game. The application generates a random number based on the selected difficulty level, and the player wins a prize if they guess the correct number.

If you are a C++ developer who is new to CI/CD concepts, or you might be familiar with these concepts but don’t know how to implement building your application using Jenkins, then this tutorial is for you.

Duration: This tutorial takes 20-40 minutes to complete (assuming you’ve already met the prerequisites below). The exact duration will depend on the speed of your machine and whether you’ve already installed docker and docker compose.

You can stop this tutorial at any point in time and continue from where you left off.

Prerequisites

For this tutorial, you will require:

  • A macOS, Linux, Windows, or Chromebook (with Linux) machine with:

    • 2 GB of RAM

    • 2 GB of drive space for Jenkins

  • The following software installed:

Fork and clone the sample repository

Get the Casino Number Guessing Game C++ application from GitHub by forking the sample repository of the application’s source code into your own GitHub account and then cloning this fork locally.

  1. Ensure you are signed in to your GitHub account. If you don’t yet have a GitHub account, sign up for free on the GitHub website.

  2. Fork the casino-number-guessing-game repository on GitHub into your local GitHub account. If you need help with this process, refer to the repository forking instructions on the GitHub website for more information.

  3. Clone your forked casino-number-guessing-game repository from GitHub to your local machine. To begin this process, do either of the following (where <your-username> is the name of your user account on your operating system):

    • If you have the GitHub Desktop app installed on your machine:

      1. In GitHub, select the green Clone or download button on your forked repository, then select Open in Desktop.

      2. In GitHub Desktop, before selecting Clone on the Clone a Repository dialog box, confirm the Local Path for your operating system:

        • macOS is /Users/<your-username>/Documents/GitHub/casino-number-guessing-game

        • Linux is /home/<your-username>/GitHub/casino-number-guessing-game

        • Windows is C:\Users\<your-username>\Documents\GitHub\casino-number-guessing-game

    • Otherwise:

      1. Open up a terminal/command line prompt and cd to the appropriate directory on:

        • macOS - /Users/<your-username>/Documents/GitHub/

        • Linux - /home/<your-username>/GitHub/

        • Windows - C:\Users\<your-username>\Documents\GitHub\ (Use a Git bash command line window as opposed to the usual Microsoft command prompt)

      2. Run the following command to clone your forked repo, replacing YOUR-GITHUB-ACCOUNT-NAME with the name of your GitHub account:

        git clone https://github.com/YOUR-GITHUB-ACCOUNT-NAME/casino-number-guessing-game

Start your Jenkins controller

  1. Obtain the latest Jenkins deployment, customized for this tutorial, by cloning the quickstart-tutorials repository.

  2. After cloning, navigate to the quickstart-tutorials directory and execute the command

    docker compose --profile cpp up -d

    to run the example.

  3. Once the containers are running successfully (you can verify this with docker compose ps), the controller can be accessed at http://localhost:8080.

If you are unable to install docker compose on your machine for any reason, you can still run the example in the cloud for free thanks to GitPod. GitPod is free for 50 hours per month. You need to link it to your GitHub account so you can run the example in the cloud. Utilize this link to open a new browser tab with a GitPod workspace where you’ll be able to start the Jenkins controller and run the rest of the tutorial.

Now, log in using the admin username and admin password.

Create your Pipeline project in Jenkins

  1. In Jenkins, select New Item under Dashboard > at the top left.

  2. Enter your new Pipeline project name, such as casino-number-guessing-game, in Enter an item name.

  3. Scroll down if necessary and select Pipeline, then select OK at the end of the page.

  4. (Optional) Enter a Pipeline Description.

  5. Select Pipeline on the left pane.

  6. Select Definition and then choose the Pipeline script from SCM option. This option instructs Jenkins to obtain your Pipeline from the source control management (SCM), which is your forked Git repository.

  7. Choose Git from the options in SCM.

  8. Enter the URL of your repository in Repositories/Repository URL. This URL can be found when selecting the green Code button in the main page of your GitHub repo.

  9. In Branches to build, enter */main

  10. In Script Path, set the script path to Jenkinsfile.

  11. Select Save at the end of the page. You’re now ready to create a Jenkinsfile to check into your locally cloned Git repository.

Create your initial Pipeline as a Jenkinsfile

You’re now ready to create your Pipeline that will automate building your C++ application in Jenkins. Your Pipeline is created as a Jenkinsfile, which is committed to your locally cloned Git repository (casino-number-guessing-game).

This is the foundation of "Pipeline-as-Code", which treats the continuous delivery pipeline as a part of the application to be versioned and reviewed like any other code. Read more about Pipeline and what a Jenkinsfile is in the Pipeline and Using a Jenkinsfile sections of the User Handbook.

First, create an initial Pipeline to build your C++ application. Add a "Build" stage to the Pipeline that begins orchestrating this whole process.

  1. As you forked our sample repo, you already have an empty Jenkinsfile.

  2. Copy the following Declarative Pipeline code and paste it into your empty Jenkinsfile:

    pipeline {
        agent any
        stages {
            stage('Build') { (1)
                steps {
                    sh 'rm -rf build'
                    sh 'cmake -B build -S .' (2)
                    sh 'cmake --build build'
                }
            }
        }
    }
    1 Defines a stage (directive) called Build that appears on the Jenkins UI.
    2 The second sh step executes the cmake command to build your C++ application.
  3. Save your edited Jenkinsfile and commit it to your local casino-number-guessing-game Git repository. Within the casino-number-guessing-game directory, run the commands:
    git add Jenkinsfile
    then
    git commit -m "Add initial Jenkinsfile"
    and finally
    git push to push your changes to your forked repository on GitHub, so it can be picked up by Jenkins.

  4. Now select Build Now on the left pane of your Pipeline project in Jenkins. After making a clone of your local casino-number-guessing-game Git repository itself, Jenkins:

  5. Initially queues the project to be run on the agent.

  6. Runs the Build stage defined in the Jenkinsfile on the agent.

During this time, CMake configures the necessary build files for your C++ application, builds, and links your application. You can now select #1 to see the details of the build. On the job status page, you can see how much time the build spent waiting in the queue and how much time it took to run.

Build details

In the left navigation pane, you can select Pipeline Overview to see the stages of the Pipeline.

Pipeline overview

Selecting the Build stage will provide more information about the stage, including the output of the cmake command if you select the green cmake section.

Build stage

You can now select casino-number-guessing-game (if that’s the name you chose for your pipeline) on the top left to return to your pipeline main page.

Add a test stage to your Pipeline

  1. Go back to your text editor/IDE and ensure your Jenkinsfile is open.

  2. Copy and paste the following Declarative Pipeline syntax immediately under the Build stage:

            stage('Test') {
                steps {
                    sh './build/casino_game'
                    sh './build/test_game'
                }
            }

    so that you end up with:

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh 'rm -rf build'
                    sh 'cmake -B build -S .'
                    sh 'cmake --build build'
                }
            }
            stage('Test') { (1)
                steps {
                    sh './build/casino_game' (2)
                    sh './build/test_game'
                }
            }
        }
    }
    1 Defines a stage (directive) called Test that appears on the Jenkins UI.
    2 This sh step runs the game and unit tests to ensure the application works as expected.
  3. Save your edited Jenkinsfile and commit it to your local casino-number-guessing-game Git repository. Within the casino-number-guessing-game directory, run the commands:
    git add Jenkinsfile
    then
    git commit -m "Add 'Test' stage"
    and finally
    git push to push your changes to your forked repository on GitHub, so it can be picked up by Jenkins.

  4. In Jenkins, go back to your Dashboard if necessary, then select casino-number-guessing-game and launch another build thanks to Build Now.

  5. After a while, a new column titled Test will appear in the Stage View.

Test stage

You can select #2, or on the number representing your last build on the left, under Build History to see the details of the build.

Test stage

You can now select Pipeline Overview to see the stages of the Pipeline.

Pipeline overview with Test stage

The newly added "Test" stage is visible here. Selecting the "Test" stage checkmark displays the stage output.

Test stage output

Viewing the results

During the Test stage, the casino_game executable runs and displays output similar to the following in the Jenkins console:

Welcome to the Casino Number Guessing Game!
Running in non-interactive mode. Using default values.
Generated random number: 8 (1)
Generated random number: 4
Generated number: 4 (2)
Wrong guess! Try again. (3)
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Wrong guess! Try again.
Congratulations! You guessed the number! (4)
You won $5! (5)
Total winnings: $5 (6)

This output confirms that the application is working as expected. Here’s what each part of the output means: <1> Generated random number: This line shows the random number generated by the game. In non-interactive mode, the game generates multiple random numbers to simulate guesses until it finds the correct one. <2> Generated number: This is the secret number the game is trying to guess. <3> Wrong guess! Try again.: This indicates that the game made an incorrect guess. In non-interactive mode, the game continues guessing until it finds the correct number. <4> Congratulations! You guessed the number!: This indicates that the game successfully guessed the correct number. <5> You won $5!: This shows the prize awarded based on the number of attempts and the difficulty level. <6> Total winnings: $5: This displays the total winnings accumulated during the game.

The test_game executable also runs during the Test stage and display output similar to the following:

Generated random number: 8
Generated random number: 4
Generated random number: 3
All tests passed!

This output confirms that the unit tests for the game logic have passed successfully.

For reference, this is an example of how the output renders:

Test stage output

Add a final delivery stage to your Pipeline

  1. Go back to your text editor/IDE and ensure your Jenkinsfile is open.

  2. Copy and paste the following Declarative Pipeline syntax immediately under the Test stage of your Jenkinsfile:

            stage('Deliver') {
                steps {
                    sh 'tar -czf casino_game.tar.gz build/casino_game'
                    archiveArtifacts artifacts: 'casino_game.tar.gz', fingerprint: true
                }
            }

    so that you will end up with:

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh 'rm -rf build'
                    sh 'cmake -B build -S .'
                    sh 'cmake --build build'
                }
            }
            stage('Test') {
                steps {
                    sh './build/casino_game'
                    sh './build/test_game'
                }
            }
            stage('Deliver') { (1)
                steps {
                    sh 'tar -czf casino_game.tar.gz build/casino_game' (2)
                    archiveArtifacts artifacts: 'casino_game.tar.gz', fingerprint: true (3)
                }
            }
        }
    }
    1 Defines a new stage called Deliver that appears on the Jenkins UI.
    2 This sh step packages the executable into a .tar.gz file.
    3 This archiveArtifacts step archives the .tar.gz file as a build artifact.
  3. Save your edited Jenkinsfile and commit it to your local casino-number-guessing-game Git repository. Within the casino-number-guessing-game directory, run the commands:
    git add Jenkinsfile
    then
    git commit -m "Add 'Deliver' stage"
    and finally
    git push to push your changes to your forked repository on GitHub, so it can be picked up by Jenkins.

  4. In Jenkins, sign in if necessary, go back to the Dashboard, and then navigate to casino-number-guessing-game. Alternatively, you can go directly to casino-number-guessing-game depending on where you’re starting from.

  5. Select Build Now on the left. After a while, a new column titled Deliver will appear in the Stage View.

    Deliver stage

  6. Select #3, or on the number representing your last build on the left, under Build History.

    Deliver stage

  7. Select Pipeline Overview to see the stages of the Pipeline.

    Pipeline overview with Deliver stage

  8. Select the Deliver stage. You will then see a green part displaying tar -czf casino_game.tar.gz build/casino_game, which represents the successful execution of the tar command.

    Deliver stage output

  9. You can see the full stage view by clicking on the "Full Stage View" in the left menu.

    Deliver stage output

Wrapping up

Well done! You’ve just used Jenkins to build a C++ application!

The "Build", "Test", and "Deliver" stages you created above are the basis for building more complex C applications in Jenkins, as well as C applications that integrate with other technology stacks.

Because Jenkins is extremely extensible, it can be modified and configured to handle practically any aspect of build orchestration and automation.

To learn more about what Jenkins can do, check out:

Cleaning Up Your Environment

After completing the tutorial, it’s important to clean up your environment to prevent interference with other tutorials you might try later.

To stop the containers and remove associated volumes:

docker compose --profile cpp down -v --remove-orphans

This command ensures a clean slate for your next project.

The --remove-orphans option instructs Docker Compose to remove any containers that are not defined in the docker-compose.yml file but are labeled as belonging to the project. This helps in cleaning up any services that might have been started independently but are considered part of the project.


Was this page helpful?

Please submit your feedback about this page through this quick form.

Alternatively, if you don't wish to complete the quick form, you can simply indicate if you found this page helpful?

    


See existing feedback here.