How I Added Continuous Integration (CI) to a C++ Project
Published: 2023-11-17 17:24:38

### Managing Project Complexity with CI Pipelines
For this week in my Open Source Class we looked into adding Continuous Integration pipelines to our projects. Last week I added unit tests using the Google Test testing framework. However, at this point, testing will only run if me and other developers remember to run them. What if someone forgets to run the tests before pushing new code or merging a pull request, or what if new code breaks those tests? 

### Continuous Integration
Continuous Integration, or CI, is one method of ensuring that new or modified code does not break our tests. With CI, a project can be automatically built and tested whenever something is pushed to a repo or when a pull request is made. This is done continually and helps monitor the effect of code changes.



### GitHub Actions - GitHub's CI service
CI requires dedicated machines to checkout our code, built it and run tests. We can either setup our own machines, or we can provision a CI cloud provider to do this for us. GitHub actually provides its own CI service, [GitHub Actions](https://github.com/features/actions), that lets you automate workflows to GitHub repo events, like making a pull request or merging to a main branch.

### Lab Exercise - Add a CI Workflow
For this lab exercise I used GitHub actions to add a CI workflow. Based on the language(s) used in your repo, GitHub will suggest possible workflows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l3jix8kwje9kkct3xia.png)

#### Workflow Templates - MSBuild-based projects (for Visual Studio)
I used a workflow template for MSBuild-based projects. When doing this lab exercise I referred to this [video by TheCherno](https://www.youtube.com/watch?v=gCfBxN4m5gw), who used the same template for his [Hazel Game Engine](https://github.com/TheCherno/Hazel/tree/master) developed [using Visual Studio](https://github.com/TheCherno/Hazel/blob/master/README.md#getting-started). The [Microsoft Build Engine](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild?view=vs-2022), MSBuild, is what Visual Studio uses to load and build managed projects. Since I'm also developing my project using Visual Studio, this template seemed like the best option.

### Fine-Tuning my First CI Workflow
After adding the workflow template, I had a couple hiccups that I had to solve.

#### MSBUILD : Error MSB1050 - Cause
After adding the template, my [first build failed](https://github.com/rjwignar/ctil/actions/runs/6855651991) due to the following error:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5z62hn4t46tw8ks7imi.png)

```
MSBUILD : error MSB1050: Specify which project or solution file
to use because the folder "." contains more than
one project or solution file.
```
This error made sense. The [root of my project](https://github.com/rjwignar/ctil) has multiple solution (.sln) and project (.vcxproj) files.

#### MSBUILD : Error MSB1050 - Solution
I solved this issue by [specifying the project file](https://github.com/rjwignar/ctil/actions/runs/6856206794/workflow#L45) containing my solution:

#### Cannot Locate Executable (.exe) - Cause
I wanted to add a job to my workflow that builds and runs the Google Test solution, `testLibrary`, but [couldn't locate the executable](https://github.com/rjwignar/ctil/actions/runs/6856691210):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iijvbqz0k7e3m7uw6vl0.png)

#### Cannot Locate Executable (.exe) - Solution
I knew running msbuild would create an executable that would run my tests, but I didn't know where to locate it. I found this [stackoverflow post](https://stackoverflow.com/questions/15754302/run-a-solution-from-visual-studio-console-without-opening-the-ide/15754394#15754394) that taught me how to locate the executable.
In short:
- msbuild generates a folder containing the executable file.
- by running `ls` in the workflow, I was able to locate the `testLibrary.exe` executable and [run it from my workflow](https://github.com/rjwignar/ctil/actions/runs/6856813517/workflow#L58):

#### Final Workflow file
In the end, my [final workflow file](https://github.com/rjwignar/ctil/blob/099d8941d253bfb51328426518c4c946c6f52d62/.github/workflows/msbuild.yml) was similar to the original template but with a few jobs added to suit my needs.

### Adding Tests to a Classmate's project
For this lab exercise I had the opportunity to add unit tests to a classmate's project and experience their CI workflow. For this exercise I worked on [go-go-web](https://github.com/kliu57/go-go-web) by [kliu57](https://github.com/kliu57). Go-Go Web is written in Python and uses the [pytest testing framework](https://docs.pytest.org/en/7.4.x/). This was my first time writing tests for pytest, but I found the [pytest docs](https://docs.pytest.org/en/7.1.x/contents.html) helpful. However, more helpful was the information provided in the [associated issue](https://github.com/kliu57/go-go-web/issues/24) and the tests already written, which helped me write the test I needed in the author's preferences.

### My Thoughts on adding CI to a C++ project
Before starting this exercise, I was intimidated by this task. I was aware of the value of adding it, but I was scared adding CI would take hours. Adding CI itself didn't take very long (although debugging my workflow did). However, I've now added a valuable tool that will always provide value to my project's development and save time, and I think I'll have a better time adding CI in the future.

### Future Changes
Two optional objectives for this exercise were to add my linter to my CI workflow and to set up a [Dev Container](https://code.visualstudio.com/docs/devcontainers/create-dev-container) in [GitHub Codespaces](https://docs.github.com/en/codespaces) that would make it simple for new developers to work on my project.

I chose to skip these changes for now due to class commitments. However, I plan to add these in the future.