> ## Documentation Index
> Fetch the complete documentation index at: https://docs.insaion.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Simulation tests in CI

> Run the INSAION agent next to a headless simulation in a CI job, so every run reports what it consumed and leaves its MCAP recording in INSAION. Covers the CI device, its credentials, the automatic upload on stop, a GitHub Actions workflow, and what to watch out for.

Run the INSAION agent next to your simulation in CI, and every test run leaves two things in INSAION:

* **What it consumed:** CPU, memory, disk and network of the runner and of your processes, on the device's charts for the time of the run.
* **What happened:** the run's MCAP recording, uploaded when the job stops the agent, ready to open in the viewer.

A CI runner's disk is gone when the job ends, so the agent has to upload the recording before it exits. **Automatic upload when a recording ends** does that: when the job stops the agent, it seals its last data, uploads the run's recording and only then exits.

## Before you start

* Agent **1.4.0 or later**, for the automatic upload when a recording ends.
* A plan that includes **automatic uploads** (Starter and up). See [Billing](https://docs.insaion.com/settings/billing.md).
* A CI runner that can run Docker, with outbound access on port 443 (see [Network and disk requirements](https://docs.insaion.com/agent/overview.md#network-and-disk-requirements)).
* A simulation that publishes on ROS 2 on the runner, for example Gazebo started headless.

The examples use GitHub Actions and the ROS 2 Jazzy image. Other CI systems work the same way: write two files, start a container, run your tests, stop the container.

## How it works

1. **One device for CI.** You register it once and store its two credentials as CI secrets. Every run uses the same device, so runs are comparable on the same charts and recordings timeline. No device is created per run.
2. **Each run** writes the credentials into a folder, starts the agent container with that folder as its state, and runs the simulation. The agent records the topics you chose for the device, and starts a new recording each time it starts, so **each run is one recording**.
3. **When the job stops the agent** (`docker stop`), the agent ends the recording, stores its last data, and asks INSAION what to upload. It uploads that part of the recording, waits until the upload is done, and exits.

## 1. Register the CI device

Register the device once, from any machine with Docker. It does not need to be the CI runner.

1. In INSAION, open **Devices → Add Device**, choose **Docker** and pick an enrollment key. Name the device after what it is, for example `ci-sim`.

2. Copy the generated `docker run` command, and change the state volume to a folder you can read afterwards, for example `-v "$PWD/ci-sim:/var/lib/insaion-agent"`.

3. Run it and wait until the **Agent status** block shows the device online.

4. Stop and remove the container:

   ```bash theme={null}
   docker rm -f insaion-agent
   ```

5. Read the two credentials the agent saved:

   ```bash theme={null}
   sudo cat ci-sim/device_id.txt
   sudo cat ci-sim/device_token.txt
   ```

6. Store them as CI secrets, for example `INSAION_DEVICE_ID` and `INSAION_DEVICE_TOKEN` in GitHub (**Settings → Secrets and variables → Actions**).

7. Delete the local folder, so the credentials only live in your CI secrets:

   ```bash theme={null}
   sudo rm -r ci-sim
   ```

Keep the device token secret: anyone with it can send data as this device.

## 2. Configure the device in INSAION

Open the device and its **Data Collection** tab (see [Device configuration](https://docs.insaion.com/devices/device-config.md)):

1. Tick **Record** for the topics you want in each run's recording, for example `/tf`, `/odom`, `/scan` and your test topics. Topics appear in the table once the agent has seen them, so do this after a first run, or prepare it in a [template](https://docs.insaion.com/settings/templates.md).
2. In **Rolling buffer settings → Uploads**, switch on **Automatic upload when a recording ends**, and set:
   * **Only the last**: leave it empty to upload the whole run, or set it to keep only the end of long runs, for example the last 10 minutes.
   * **Wait for it on stop, up to**: how long the agent may take to finish the upload when the job stops it. 5 minutes by default. Set it longer than the upload of a run takes on your runners.
   * **Pipelines**: which recording sets to upload. All of them by default.
3. Save.

![Data Collection tab in INSAION](https://mintcdn.com/insaion/T_W4mC75iXknQG65/assets/agent/automatic-upload-when-recording-ends.png?fit=max\&auto=format\&n=T_W4mC75iXknQG65\&q=85\&s=f52bd84bd89ddb0760b73cc4237cef98)

To set up several CI devices the same way, save these settings as a [template](https://docs.insaion.com/settings/templates.md) and apply it to each device. The automatic upload setting is part of the template.

## 3. Add the agent to your workflow

```yaml theme={null}
name: Simulation tests

on: [push]

# One run at a time per CI device: two agents with the same credentials
# disconnect each other.
concurrency:
  group: insaion-ci-sim
  cancel-in-progress: false

jobs:
  sim:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - name: Write the INSAION device credentials
        run: |
          mkdir -p "$RUNNER_TEMP/insaion"
          printf %s "${{ secrets.INSAION_DEVICE_ID }}" > "$RUNNER_TEMP/insaion/device_id.txt"
          printf %s "${{ secrets.INSAION_DEVICE_TOKEN }}" > "$RUNNER_TEMP/insaion/device_token.txt"
          chmod 600 "$RUNNER_TEMP/insaion/"*

      - name: Start the INSAION agent
        run: |
          docker run -d \
            --name insaion-agent \
            --network host \
            --pid host \
            -v /dev/shm:/dev/shm \
            -v /proc:/host/proc:ro \
            -v /sys:/host/sys:ro \
            -v /etc:/host/etc:ro \
            -v "$RUNNER_TEMP/insaion:/var/lib/insaion-agent" \
            -e INSAION_PROCSTAT_PATTERN='gz|ros2|python3' \
            ghcr.io/insaion/agent:latest-jazzy

      - name: Run the simulation tests
        run: ./ci/run_sim_tests.sh

      - name: Stop the agent and upload the recording
        if: always()
        run: |
          docker stop -t 360 insaion-agent
          docker logs insaion-agent 2>&1 | grep ExitUpload || true
          exit "$(docker inspect -f '{{.State.ExitCode}}' insaion-agent)"
```

What each part does:

* **Credentials:** the agent reads its identity from `device_id.txt` and `device_token.txt` in its state folder. Writing them from secrets at the start of each run is all it needs. No enrollment key is used in CI.
* **`--network host` and `/dev/shm`:** the agent joins the simulation's ROS 2 graph on the runner. Use the same `ROS_DOMAIN_ID` and, if you set one, the same `RMW_IMPLEMENTATION` in the simulation and in the agent (`-e ROS_DOMAIN_ID=...`).
* **`--pid host` and the `/proc`, `/sys`, `/etc` mounts:** the agent measures the runner, not only its own container.
* **`INSAION_PROCSTAT_PATTERN`:** optional. A regular expression of process names to measure one by one, so the charts show what your simulation and nodes cost. Leave it out for host-wide metrics only.
* **`docker stop -t 360`:** sends the agent the stop signal and gives it 360 seconds before Docker kills it. Keep this longer than **Wait for it on stop, up to** (300 seconds by default), so the agent always gets to finish the upload.
* **`if: always()`:** the recording is uploaded even when the tests fail, which is when you need it most.
* **The last line** makes the step fail when the upload did not finish. Remove it if a lost recording should not fail the job.

Pin the image to a version for reproducible runs, for example `ghcr.io/insaion/agent:v1.4.0-jazzy`. Use the image for your ROS 2 distribution: `humble`, `jazzy`, `kilted` or `lyrical`.

## What the agent logs on stop

The stop step prints the agent's `[ExitUpload]` lines:

```text theme={null}
[ExitUpload] Ending the recording...
[ExitUpload] Uploading the recording (batch 5a2f...), waiting up to 300 s...
[ExitUpload] Recording uploaded (batch 5a2f...).
```

| Exit status | Meaning |
| - | - |
| `0` | The recording was uploaded, or there was nothing to upload (for example, it is already in the cloud). |
| `75` | The upload did not finish in time or failed, or INSAION could not be reached. Part of the recording may be missing. |

Other lines you may see:

* No `[ExitUpload]` lines at all: **Automatic upload when a recording ends** is off for this device, or the agent is older than 1.4.0.
* `The recording is not uploaded: not_entitled`: your plan does not include automatic uploads.
* `The recording is not uploaded: quota_exceeded`: your organization's storage is full. See [Storage and uploads](https://docs.insaion.com/devices/device-recordings.md#storage-and-uploads).

## Find a run in INSAION

* **Recording:** open the device's **Recordings** tab. Each run is its own recording block on the timeline, green once uploaded. Open it in the viewer from there or from the [Recordings](https://docs.insaion.com/recordings/overview.md) page. See [Device recordings](https://docs.insaion.com/devices/device-recordings.md).
* **Cost:** open the device's monitoring dashboard and set the time range to the run. The runner's and your processes' CPU, memory, disk and network are there for the minutes of the run.

Note the time a job ran: runs are found by time on the device's timeline and charts.

## Good to know

* **One job at a time per device.** Two agents with the same credentials disconnect each other. Keep the `concurrency` group, and for parallel jobs, such as a test matrix, register one CI device per parallel slot and give each slot its own secrets.
* **Self-hosted runners next to robots.** With `--network host`, the simulation's ROS 2 graph reaches every machine on the runner's network that uses the same `ROS_DOMAIN_ID`. Robots there then subscribe to the simulation's topics, which can slow its large messages down to a crawl, and record them. Keep the graph on the runner: set `ROS_AUTOMATIC_DISCOVERY_RANGE=LOCALHOST` (Jazzy and later) or `ROS_LOCALHOST_ONLY=1` (Humble) in the simulation and in the agent, or use a domain ID no robot uses. GitHub-hosted runners are not affected.
* **Do not rotate the CI device's credentials.** The token in your secrets stays valid from run to run, because each run writes it fresh. If someone selects **Rotate credentials** on the device, the next run rotates the token on the runner, the new token is lost with the runner, and the run after that is refused. If the token must be replaced, for example because it leaked, delete the device, register a new one and update both secrets.
* **The device goes offline after every run.** Leave it out of alarms that fire when data is missing, for example with a tag such as `env=ci` on the device and a scope that excludes it. See [Tags](https://docs.insaion.com/devices/tags.md) and [Create an alarm](https://docs.insaion.com/alarms/create-alarm.md).
* **Storage.** Each uploaded run counts towards your organization's storage. Use **Only the last** and **Pipelines** to keep only what you need.
* **Runs that are killed.** If the job is cancelled or the runner dies before the stop step, the agent never gets the stop signal and that run's recording is not uploaded.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.