Can I optimize this script?

I am new to CircleCI, and I have a script that installs CMake, build my C++ project, runs an executable, and then runs a bash script. Everything works as intended, but I would like to know if this can be optimized more, or if there are any better practices I should be following?

Thanks,

version: 2.1

jobs:
  test:
    docker:
      - image: cimg/base:edge-22.04
    steps:
      - checkout
      # Install CMake
      - run:
          name: Install CMake
          command: |
            sudo apt-get update
            sudo apt-get install -y cmake
      # Create build directory and build the project
      - run:
          name: Build the project
          command: |
            mkdir -p build
            cd build
            cmake ..
            cmake --build . --config Release
      # Run unit tests 
      - run:
          name: Running unit tests   
          command: |
            build/night-tests
      # Run integration tests
      - run:
          name: Running integration tests
          command: |
            cd tests
            bash test_night.sh ../build/night

workflows:
  version: 2
  tests:
    jobs:
      - test

Looks good to me overall.

I would make sure that your bash scripts fail in cases that it should (i.e., it has -e and -o pipefail set, unless you’re specifically exiting in all possible failure scenarios). Or, depending on how complicated the script is, it may be better and more readable to use a Makefile or embed the test commands directly in the Circle config.

I would also suggest seeing if you can get junit test reporting setup (see this link for some info), and then store the test results with store_test_results (docs here).

Also, recommend installing the CircleCI CLI locally so that you can run circleci config validate before you push up changes. There are also some pre-commit hooks that can do some validation of the config.

Thanks for the feedback Will, I’ll look into those

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.