KiCad 10 gives a script three ways in. kicad-cli runs ERC, DRC and the fabrication exports, and KiCad's Docker image is built for it. The pcbnew Python module also builds boards, until KiCad 11 removes it. The IPC API's library, kicad-python, needs KiCad running. We run the first two on every job, with no window.
What kicad-cli does, and what it leaves to Python
kicad-cli is KiCad's command line. KiCad 10's reference lists every command with its options, and this article does not repeat them. In short:
- Checks:
sch ercreads the schematic, andpcb drcthe board. - Outputs:
pcb exportwrites Gerbers, drill and position files, IPC-2581, STEP and other 3D models;sch exportwrites the netlist, the BOM, PDFs and SVGs. - Batches:
jobset runruns a predefined jobset.
What it does not do is build a board. No command places a footprint or routes a track, and its export list has no Specctra file, the format our autorouter, Freerouting, reads. That part needs Python.
Our worker calls kicad-cli all through a job. Our code writes the schematic as text in KiCad 10's format. sch upgrade saves it again as KiCad writes it, sch erc checks it, and sch export netlist gives KiCad's own netlist of it, which has to join the same pins as the board's. The routed board then goes through pcb drc, with the fab's rules written beside it where kicad-cli reads them, and pcb export gerbers, drill and pos write the files a fab and an assembler take.
Which exit codes and reports can a script trust?
The manual sets one rule: with --exit-code-violations, ERC and DRC exit with 0 when they find no violation and 5 when they find any. This is the DRC command each of our boards goes through:
kicad-cli pcb drc --format json \
--output drc.json \
--severity-error --severity-warning \
--exit-code-violations board.kicad_pcb
A 5 says only that some violation was found. So our worker takes the exit code as whether the check ran, and the verdict from the report, in three steps:
- 0 or 5, with the report on disk: the check ran. Go on to the report.
- Any other code, or no report: kicad-cli failed. The step fails with the tool's error, never as a board with violations.
- The verdict is in the JSON. Each violation carries its severity, and DRC lists unconnected items apart from the violations. Errors and unconnected items fail the board; warnings are counted and reported.
ERC's report lists its violations sheet by sheet, and the job counts them the same way. Both reports also carry the moment they were written, so our worker fixes that field before it stores one: two runs on one board then give one report, byte for byte.
How KiCad runs with no window in a pinned Docker image, and what our worker reads back from kicad-cli and pcbnew, drawn from KiCad's documentation.
The pcbnew module: what it still does, and until when
import pcbnew loads KiCad's SWIG bindings to the PCB editor. KiCad's developer documentation marks them deprecated as of KiCad 9.0 and plans their removal in KiCad 11.0. They cover the PCB editor alone, as KiCad supports no Python in its other parts, and they change between major versions.
They still do what kicad-cli cannot, and they run stand-alone, with no PCB editor open, so a container needs no window for them. Our worker uses them to build a board from its netlist: the outline, the footprints placed, each pad on its net. pcbnew.ExportSpecctraDSN then writes the design file for Freerouting, pcbnew.ImportSpecctraSES applies the routes it returns, and a last pass measures the routed board's pads and copper for the review.
In KiCad's image, the module comes with the system's Python. Our worker's virtual environment is created with the system's site-packages, so import pcbnew works beside the worker's own packages, and the image's build imports it to prove it.
One process for each pcbnew operation
The SWIG bindings keep global state, and one bad call can take the interpreter down with it. So our long-running worker never imports pcbnew. Each operation runs as a Python process of its own, which:
- sees only a path, a home in the job's folder and a UTF-8 locale;
- seeds KiCad's UUID generator with
pcbnew.KIID.SeedGeneratorbefore it adds anything, so the same input gives the same board file; - does one thing, prints one JSON object and exits: 0 for success, 3 for a known failure with its code, anything else a crash.
A process start costs well under a second. Every pcbnew call lives in one module, so the move to KiCad 11 changes one file.
What the IPC API and kicad-python need
kicad-python holds the official Python bindings for KiCad's IPC API, installed with pip install kicad-python. The API talks to KiCad from another process, so KiCad has to be there: version 9.0 or later, running, with the API server turned on in its preferences, on the Plugins page. Its README puts it plainly: it is "not possible to use kicad-python to manipulate KiCad design files without KiCad running."
In KiCad 10, a running KiCad means the editor and its window, and KiCad's Docker page says the GUI is not supported in Docker. KiCad 11 can start the server with no window, as kicad-cli api-server, and kicad-python can start that for you.
So in KiCad 10 the IPC API is for scripts beside an open editor, and pcbnew for a server. Our image has no IPC API client yet.
Running KiCad in Docker
KiCad's Docker page points to its official images, kicad/kicad on Docker Hub with a mirror on GitHub, meant for kicad-cli. The images' README explains the tags: a major.minor tag such as 10.0 moves to the latest patch release whenever it is pulled, while a major.minor.patch tag stays locked to its release. Stable images are kept indefinitely, at best effort.
So pin the patch. Our image is built on kicad/kicad:10.0.6, and a new patch enters only as a change of its own. The build has to pass a smoke check that runs kicad-cli version and imports pcbnew, and it counts KiCad 10.0.6's 15,450 footprints, failing on any other number. Our golden tests then compare the outputs with stored ones byte for byte, so whatever a new release writes differently shows as a reviewed diff.
Give each run a clean environment, too. Ours hands kicad-cli a path, a home inside the job's own folder and a UTF-8 locale, and nothing else. KiCad keeps its settings, cache and data under that home, and they go when the job's folder does.
The file-ownership trap on CI runners
KiCad's Dockerfile creates the image's user, kicad, with user ID 1000. On the hosted Linux runners our CI used, a folder mounted from the runner belonged to the runner's own user, with another ID, so the image's user could not write its output there unless the ownership was set.
Our image tests run the container as the host's own user instead: Docker's --user with the runner's user and group IDs, and HOME set to /tmp, so KiCad has somewhere to keep its settings. We checked that KiCad's output is the same bytes under any user ID. With the export time fixed in each file, two runs on the same image give identical bytes: KiCad AI covers what else the same board twice takes, and KiCad fabrication files the settings behind each file.
Limits
This covers KiCad 10. KiCad 11 removes the pcbnew module and serves the IPC API without a window, so read the documentation of the version you run. Scripts that drive KiCad's window are left out: action plugins, the IPC API beside an open editor, a GUI in a container. What we report of KiCad's own output comes from 10.0.6 in our image; its later patch releases are untested here. A project KiCad writes and checks without a window is files, not a working board; the roadmap says where PCB Agent's own boards stand.
Sources
- KiCad Command-Line Interface, KiCad 10.0 documentation
- PCB Python Bindings, KiCad developer documentation
- kicad-python: README, KiCad on GitLab
- Container / Docker Images, KiCad
- KiCad Docker images: README, KiCad on GitLab
- Dockerfile for the KiCad 10.0 image, KiCad on GitLab


