TExcelTAD
Home › C/C++ Packages: Conan 2, vcpkg & CPM.cmake › Package Managers in CI

Package Managers in CI

A package manager earns its keep in CI: every job starts on a clean machine, and without caching it would rebuild every dependency from source on every push. Good CI setups have three properties: pinned inputs (lockfile or baseline), cached binaries, and one script that developers can run locally too.

One build script for everyone

Put the steps in a script in the repository and call it from CI, instead of spreading commands across YAML. Developers then reproduce CI failures with the same command. A Conan version, run here for real:

bash · try it yourself
mkdir -p app && cd app
cat > conanfile.txt <<'TXT'
[requires]
fmt/[>=12 <13]
[test_requires]
catch2/3.16.0
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
TXT
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(app CXX)
find_package(fmt REQUIRED CONFIG)
find_package(Catch2 3 REQUIRED CONFIG)
add_executable(tests tests.cpp)
target_link_libraries(tests PRIVATE fmt::fmt Catch2::Catch2WithMain)
enable_testing()
add_test(NAME tests COMMAND tests --rng-seed 1)
TXT
cat > tests.cpp <<'SRC'
#include <catch2/catch_test_macros.hpp>
#include <fmt/format.h>
TEST_CASE("pads") { REQUIRE(fmt::format("{:>4}", 7) == "   7"); }
SRC
conan lock create . > /dev/null 2>&1          # done once by a developer, committed

cat > ci.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
# 1. dependencies: pinned by the lockfile, missing binaries built (and uploaded by a later step)
conan install . --lockfile=conan.lock --build=missing -c tools.build:jobs=1 \
      -c tools.cmake.cmaketoolchain:generator=Ninja --format=json > deps.json 2> install.log
grep -E 'Using lockfile|Install finished' install.log
# 2. build and test
cmake --preset conan-release > /dev/null
cmake --build --preset conan-release
ctest --preset conan-release --output-on-failure
# 3. record what was used
python3 -c "import json; g=json.load(open('deps.json'))['graph']['nodes']; print('used:', ', '.join(sorted(n['ref'].split('#')[0] for i, n in g.items() if i != '0' and n['binary'] != 'Skip')))"
SH
bash ci.sh 2>&1 | grep -vE '^ *Start|Total Test time'
Using lockfile: '/home/learner/app/conan.lock'
Install finished successfully
[1/2] Building CXX object CMakeFiles/tests.dir/tests.cpp.o
[2/2] Linking CXX executable tests
Test project /home/learner/app/build/Release
1/1 Test #1: tests ............................   Passed    0.00 sec

100% tests passed out of 1

used: catch2/3.16.0, fmt/12.1.0

--format=json sends the graph to standard output (here deps.json) while the human-readable log goes to standard error (here install.log, so CI can archive it); the last step turns it into a one-line record of exactly what the build used (note that cmake, a tool Catch2 would need only to build itself, was skipped). Keep files like this as build artifacts: they answer "which fmt did release 1.4.2 ship with?" months later.

Conan in GitHub Actions

yaml
name: ci
on: [push, pull_request]
jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-24.04, windows-2025, macos-15]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - run: pip install conan==2.33.0
      - run: conan profile detect
      - run: conan remote add company ${{ vars.CONAN_URL }} --index 0
      - run: conan remote login company ci -p "${{ secrets.CONAN_PASSWORD }}"
      - run: bash ci.sh
      - name: Upload binaries built in this job (main branch only)
        if: github.ref == 'refs/heads/main'
        run: |
          conan list --graph=deps.json --graph-binaries=build --format=json > built.json
          conan upload --list=built.json -r company -c

The private remote is the binary cache: the first job that builds a missing binary uploads it, and every later job downloads it. Uploading only from main keeps pull requests from untrusted branches out of the shared cache. Without a server, actions/cache on ~/.conan2/p keyed by hashFiles('conan.lock') plus the OS works too, though less efficiently.

vcpkg in GitHub Actions

yaml
    env:
      VCPKG_BINARY_SOURCES: "clear;nuget,https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json,readwrite"
    steps:
      - uses: actions/checkout@v4
      - name: Set up NuGet credentials for the binary cache
        shell: bash
        run: |
          mono `vcpkg fetch nuget | tail -n 1` sources add -Name github -Source \
            "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" \
            -UserName "${{ github.actor }}" -Password "${{ secrets.GITHUB_TOKEN }}"
      - run: cmake --preset vcpkg && cmake --build --preset vcpkg && ctest --preset vcpkg

Points to get right:

  • Pin vcpkg itself: check out the vcpkg repository at your builtin-baseline commit (or a release tag), or use it as a Git submodule. The runner's preinstalled vcpkg moves with the runner image.
  • The binary cache is mandatory: without it, every job rebuilds every port. files on a shared disk, NuGet feeds (GitHub Packages, Azure Artifacts), or an object store all work.
  • Windows runners need no mono; on Linux and macOS, install it or use another provider.

CPM in CI

Cache the source cache directory and, ideally, the compiler output:

yaml
      - uses: actions/cache@v4
        with:
          path: ~/.cache/CPM
          key: cpm-${{ hashFiles('**/CMakeLists.txt', 'cmake/*.cmake') }}
      - run: cmake -B build -DCPM_SOURCE_CACHE=$HOME/.cache/CPM -DCMAKE_CXX_COMPILER_LAUNCHER=ccache

CPM can't avoid compiling dependencies, so a compiler cache such as ccache is what makes CPM projects fast in CI.

Checklist

Item Conan vcpkg CPM
Pin the tool pip install conan==X vcpkg commit/tag CPM_DOWNLOAD_VERSION + hash
Pin the dependencies conan.lock builtin-baseline Git tags (or package-lock.cmake)
Pin the configuration committed profiles (-pr:h, -pr:b) committed triplets presets
Cache private remote (or ~/.conan2/p) binary sources source cache + ccache
Fail on drift --lockfile (strict by default) baseline in the manifest —

Test yourself

Why upload new binaries only from the main branch?

  1. Uploads are slow
  2. So builds of untrusted pull-request branches can't put binaries into the shared cache
  3. Conan forbids uploads from branches
  4. To save disk space

What should CI do with a vcpkg baseline?

  1. Ignore it
  2. Use a vcpkg checkout that contains that commit, so the baseline resolves the same everywhere
  3. Update it on every run
  4. Delete vcpkg.json

What makes CPM-based projects fast in CI?

  1. Binary packages from CPM
  2. A cached CPM source cache plus a compiler cache like ccache
  3. Running CMake twice
  4. Nothing