CI Pipeline: Building ROS2 Packages With Colcon

Alex Johnson
-
CI Pipeline: Building ROS2 Packages With Colcon

Continuous Integration (CI) is a cornerstone of modern software development, ensuring that code changes are automatically built, tested, and validated. This process helps catch integration issues early, reduces the risk of introducing bugs, and maintains a high level of code quality. In the context of ROS2 (Robot Operating System 2), setting up a CI pipeline is essential for managing the complexities of robotic software development.

Why Implement CI for ROS2 Packages?

Implementing a CI pipeline for ROS2 packages offers several significant advantages:

  • Early Bug Detection: Automated builds and tests can quickly identify issues introduced by new code changes, preventing them from propagating further into the codebase.
  • Code Quality Assurance: CI pipelines can enforce coding standards and run linters to ensure consistent and high-quality code.
  • Integration Testing: ROS2 packages often depend on each other. CI pipelines can verify that these packages integrate correctly, reducing integration-related failures.
  • Automated Releases: CI pipelines can be configured to automatically release new versions of packages when changes are merged into the main branch.
  • Collaboration: CI pipelines provide a central platform for developers to collaborate and review code changes.

By automating these processes, CI pipelines can significantly improve the efficiency and reliability of ROS2 software development. This not only saves time but also enhances the overall quality of the robotic systems being built.

Setting Up a Basic CI Pipeline for colcon build

The primary goal is to establish a CI pipeline that automatically executes colcon build for your ROS2 package. colcon is a command-line tool used for building, testing, and installing multiple ROS2 packages. Here’s how you can set up a basic CI pipeline using GitHub Actions, a popular CI/CD platform.

Step 1: Create a GitHub Repository

If you haven't already, create a GitHub repository for your ROS2 package. This repository will host your code and CI configuration.

Step 2: Create a GitHub Actions Workflow

In your repository, create a directory named .github/workflows. Inside this directory, create a YAML file (e.g., ros2-ci.yml) that defines your CI workflow.

Step 3: Define the CI Workflow

Here's an example of a basic CI workflow that runs colcon build:

name: ROS2 CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up ROS2
        uses: ros2/github-actions/setup-ros@v1
        with:
          ros_distro: jazzy

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install -y python3-colcon-common-extensions

      - name: Build the package
        run: colcon build --symlink-install

Step 4: Commit and Push the Workflow

Commit the ros2-ci.yml file to your repository and push it to GitHub. This will trigger the CI workflow automatically whenever you push changes to the main branch or create a pull request.

Explanation of the Workflow

  • name: ROS2 CI: Specifies the name of the workflow.
  • on:: Defines the events that trigger the workflow. In this case, it's triggered on push and pull_request events for the main branch.
  • jobs:: Defines the jobs to be executed in the workflow.
  • build:: Defines the build job.
  • runs-on: ubuntu-latest: Specifies the runner environment (Ubuntu) for the job.
  • steps:: Defines the steps to be executed in the job.
    • actions/checkout@v3: Checks out the code from the repository.
    • ros2/github-actions/setup-ros@v1: Sets up ROS2 with the specified distribution (Jazzy in this case).
    • The Install dependencies step updates the package list and installs python3-colcon-common-extensions.
    • The Build the package step runs colcon build with the --symlink-install option, which creates symbolic links instead of copying files, speeding up the build process.

Running Linters in the CI Pipeline

Linters are essential tools for maintaining code quality and consistency. Integrating linters into your CI pipeline can help catch coding style issues and potential bugs early in the development process. Here’s how to integrate linters into your CI pipeline.

Step 1: Add Linter Dependencies

First, you need to install the necessary linter dependencies. In your CI workflow file (ros2-ci.yml), add a step to install the required linters. For example, to use ament_lint_auto, you would add the following:

- name: Install linter dependencies
  run: |
    sudo apt update
    sudo apt install -y ros-jazzy-ament-lint-auto

Step 2: Run Linters

Next, add a step to run the linters. You can use the ament_lint_auto package to automatically run all available linters. Add the following step to your CI workflow:

- name: Run linters
  run: colcon test --event-handlers console_direct+ ament_lint_auto

This command will run all linters configured in your ROS2 package and report any issues found.

Complete Workflow with Linters

Here’s the complete CI workflow file with the linter integration:

name: ROS2 CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up ROS2
        uses: ros2/github-actions/setup-ros@v1
        with:
          ros_distro: jazzy

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install -y python3-colcon-common-extensions

      - name: Install linter dependencies
        run: |
          sudo apt update
          sudo apt install -y ros-jazzy-ament-lint-auto

      - name: Build the package
        run: colcon build --symlink-install

      - name: Run linters
        run: colcon test --event-handlers console_direct+ ament_lint_auto

Ignoring ament_copyright Failures

In some cases, you might encounter failures related to ament_copyright. This linter checks for copyright notices in your source files. If you want to temporarily ignore these failures, you can modify the colcon test command to exclude the ament_copyright test.

Step 1: Exclude ament_copyright from Tests

Modify the Run linters step in your CI workflow to exclude ament_copyright tests. You can achieve this by using the --packages-select-regex option to select only the packages you want to test, excluding the ament_copyright package.

- name: Run linters (excluding ament_copyright)
  run: colcon test --event-handlers console_direct+ ament_lint_auto --packages-select-regex '^(?!.*ament_copyright.*){{content}}#39;

This command uses a regular expression to select all packages except those containing ament_copyright in their name. This will effectively ignore any ament_copyright failures during the CI process.

Why Ignore ament_copyright?

While it’s generally a good practice to include copyright notices in your code, there might be situations where you need to temporarily disable this check. For example:

  • Legacy Code: You might be working with legacy code that doesn’t have proper copyright notices, and updating all files might be a significant effort.
  • Temporary Workaround: You might want to focus on other more critical issues first and address copyright notices later.
  • Specific Requirements: Your project might have specific requirements that make the default ament_copyright checks unsuitable.

However, it’s important to remember that ignoring ament_copyright should be a temporary solution. You should eventually address the copyright issues to ensure compliance and maintain code quality.

Best Practices for ROS2 CI Pipelines

To ensure your ROS2 CI pipeline is effective and efficient, consider the following best practices:

  • Use a Dedicated ROS2 CI Template: Start with a well-maintained ROS2 CI template as a base for your workflow. This can save you time and ensure that you’re following best practices.
  • Cache Dependencies: Use caching to store downloaded dependencies and avoid re-downloading them on every build. This can significantly reduce build times.
  • Parallel Builds: Run multiple builds in parallel to speed up the CI process. This is especially useful for large projects with many packages.
  • Test on Multiple ROS2 Distributions: Test your package on multiple ROS2 distributions (e.g., Foxy, Galactic, Humble, Jazzy) to ensure compatibility.
  • Use Docker Containers: Use Docker containers to create a consistent and reproducible build environment. This can help avoid issues caused by different system configurations.
  • Monitor CI Performance: Monitor the performance of your CI pipeline and identify areas for improvement. This can help you optimize your workflow and reduce build times.

Conclusion

Setting up a CI pipeline for ROS2 packages is crucial for maintaining code quality, detecting bugs early, and ensuring smooth integration. By automating the build and testing process, you can significantly improve the efficiency and reliability of your ROS2 software development. This guide provides a basic framework for setting up a CI pipeline with colcon build and integrating linters, while also addressing how to temporarily ignore ament_copyright failures. Remember to follow best practices and continuously optimize your CI pipeline to achieve the best results.

For more information on ROS2 and best practices, check out the official ROS2 Documentation.

You may also like