Building Rust on CircleCI

Rust is a fun language that can easily be built on multiple systems.

Using CircleCI you can build on MacOS, Windows, and Linux systems with one straightforward .circleci/config.yml:

version: 2.1

orbs:
  win: circleci/windows@5.0

executors:
  docker:
    docker:
      - image: cimg/rust:1.65
  linux:
    machine: true
  macos:
    macos:
      xcode: 12.5.1

jobs:
  test:
    parameters:
      platform:
        type: executor
    executor: << parameters.platform >>
    steps:
      - checkout
      - restore_cache:
          keys:
            - cargo-v1-{{ arch }}-{{ checksum "Cargo.toml" }}
            - cargo-v1-{{ arch }}
      - run: curl -o rustup https://sh.rustup.rs
      - run: bash rustup -y
      - run: rm ~/.gitconfig
      - run: ~/.cargo/bin/cargo test -v --no-fail-fast
      - run: ~/.cargo/bin/cargo test -v --no-fail-fast --features inline-comment
      - run: ~/.cargo/bin/cargo test -v --no-fail-fast --features case-insensitive
      - run: ~/.cargo/bin/cargo doc --no-deps
      - save_cache:
          key: cargo-v1-{{ arch }}-{{ checksum "Cargo.toml" }}
          paths:
            - ~/.cargo

workflows:
  workflow:
    jobs:
      - test:
          name: test-on-<< matrix.platform >>
          matrix:
            parameters:
              platform: ["docker", "macos", "win/default", "linux"]

There are a few things happening here that are worth noting.

We are:

  1. Using the Windows orb and have to  reference the executor as "win/default".
  2. Defining two Linux environments: a Docker container and a full virtual machine.
  3. Running bash commands like bash rustup -y because on Windows the .exe is implied.
  4. Running cargo commands by referencing the home directory with ~, otherwise the paths become a problem.
  5. Removing the default global gitconfig as we want to check out code via HTTPS instead of SSH from GitHub.

Each job in the workflow will have a name based on the executor name we defined, as shown here: