OpenWrt CI/CD 做法

OpenWrt 支援 CI/CD(持續整合 / 持續部署)通常是為了自動化以下幾個流程:

  1. 自動編譯 firmware(例如針對特定開發板自動打包 .bin)

  2. 自動測試 kernel module、網路套件或客製化應用(例如 LuCI plugin)

  3. 自動部署 firmware / 套件到測試設備或模擬器(如 QEMU)

  4. 自動推送至 OTA server 或映像檔分發系統(如 sysupgrade 伺服器)


一、CI/CD 支援架構概觀

以下是典型的 OpenWrt CI/CD 流程架構:

[ Git Repo (GitHub/GitLab) ]
↓ push / merge
[ CI/CD Runner (GitHub Actions / GitLab CI / Jenkins / Drone) ]
↓ build
[ OpenWrt SDK or full buildroot ]
↓ output
[ .bin, .ipk, rootfs... ]
↓ (optional)
[ Deploy: flash to test device / push to server ]

二、實作範例:GitHub Actions 自動編譯 OpenWrt

# .github/workflows/build-openwrt.yml
name: Build OpenWrt

on:
push:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Clone OpenWrt
uses: actions/checkout@v2

- name: Install dependencies
run: |
sudo apt update
sudo apt install -y build-essential libncurses5-dev gawk git gettext unzip file libssl-dev wget python3

- name: Download feeds
run: |
./scripts/feeds update -a
./scripts/feeds install -a

- name: Configure
run: make defconfig

- name: Build
run: make -j$(nproc)

- name: Upload firmware
uses: actions/upload-artifact@v3
with:
name: openwrt-firmware
path: bin/targets


三、常見組合與工具建議

項目 工具選擇 說明
Git Repo GitHub / GitLab 原始碼託管
CI Engine GitHub Actions / GitLab CI / Jenkins / Drone 自動化執行
編譯工具 OpenWrt SDK / buildroot 可使用 openwrt-sdk-*.tar.xz
測試工具 QEMU / pytest + ssh / netperf 自動測試 firmware
OTA 部署 sysupgrade + scp / HTTP OTA Server 自動部署映像檔

四、自動化測試範例(SSH login test)

- name: Deploy to QEMU
run: |
qemu-system-mips -M malta -kernel openwrt.bin \
-nographic -append "console=ttyS0" \
-net nic -net user,hostfwd=tcp::2222-:22 &

- name: Wait and run test
run: |
sleep 60
ssh -p 2222 root@localhost “uci get network.lan.ipaddr”