PlatformIO on GitHub Actions with cache accelerate
Source: Dev.to
Sample on official documentation
Official documentation suggests an example workflow of using PlatformIO on GitHub Actions.
name: PlatformIO CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Build PlatformIO Project
run: pio run
Setup PlatformIO CLI pio, then run the build with pio run. PlatformIO CLI is written in Python, so Python is installed first. Caching both Python packages and PlatformIO files reduces workflow duration when download speeds are unstable or the PlatformIO package is large.
Improving the workflow
The workflow above unpacks packages each run, which takes time. Caching the unpacked files can eliminate the unpack step. The .platformio/ directory in the home folder contains download cache files and unpacked packages.
- uses: actions/cache@v4
with:
path: |
~/.cache/pip
# ~/.platformio/.cache
~/.platformio/
key: ${{ runner.os }}-pio
Note: Packages will not be updated until the cache is refreshed.
Experiment
The following experiment compares three workflow patterns: No cache, Download cached, and Unpack cached.
- Measured steps: cache restoring, PlatformIO installation, and building.
- Average of 5 runs (first run ignored because the cache is not hit).
- Target task: build (no unit tests).
- Source code:
int main()containing onlywhile(1)andreturn 0;. - Build focuses on the NUCLEO‑F303K8 HAL library; other settings are defaults.
Results
| Run | No cache (s) | Download cached (s) | Unpack cached (s) |
|---|---|---|---|
| 1 | 32 | 18 | 12 |
| 2 | 34 | 22 | 13 |
| 3 | 32 | 17 | 11 |
| 4 | 35 | 17 | 12 |
| 5 | 31 | 19 | 10 |
| Average | 32.8 | 18.6 | 11.6 |
Download caching reduces the duration by about 14 seconds, and unpack caching saves an additional 7 seconds.