Files
ESP32-S3-Touch-LCD-5B/.gitea/workflows/firmware.yml
T

180 lines
8.5 KiB
YAML

name: Firmware
# Main CTRL firmware, built by PlatformIO on every push. Two outputs:
#
# - push to main -> rolling PRE-release `main-latest` (firmware.bin + .elf),
# unsigned, no manifest. For bench testing / USB flashing.
# The device never installs it: it only reads
# /releases/latest, which skips pre-releases, and it
# needs a signed manifest.txt anyway.
# - push tag v* -> the OTA release: firmware.bin + firmware.elf +
# manifest.txt (size, sha256, ECDSA P-256 signature over
# "patrol-a|<tag>|<sha256>"). This is what `ota install`
# on the controller pulls.
#
# Signing key: repo secret BMW_NISSAN_FIRMWARE_SIGNING_KEY (PEM, EC P-256). Its public
# half is compiled into the firmware (include/config/ota_config.h) and kept in
# tools/ota/firmware_signing_pub.pem, which this job uses to self-check the
# signature before publishing. See wiki/pages/ota-updates.md.
#
# Releases go through the Gitea API with plain curl (actions/upload-artifact
# is unreliable on this runner - same pattern as parking_solution).
#
# A main push skips itself (step "Skip?") when (a) the commit already carries a
# v* tag - the tag build covers it; release with ONE push so the tag exists
# when this runs: git push --follow-tags origin main - or (b) it only
# changes docs (wiki/**, *.md). Done in-job rather than with paths-ignore so a
# tag build can never be skipped by a path filter. Tag builds always run.
on:
push:
branches: [main]
tags: ['v*']
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Skip?
env:
BEFORE: ${{ github.event.before }}
run: |
SKIP=false
if [ "${GITHUB_REF#refs/tags/}" = "${GITHUB_REF}" ] && [ "${GITHUB_EVENT_NAME}" = "push" ]; then
# (a) already tagged v* ? (annotated tags list their commit as <tag>^{})
if git ls-remote --tags origin 'v*' | grep -q "^${GITHUB_SHA}"; then
echo "commit ${GITHUB_SHA:0:7} is tagged - the tag build covers it"; SKIP=true
# (b) docs only? Needs the previous head; a new branch / force push just builds.
elif [ -n "$BEFORE" ] && [ "$BEFORE" != "0000000000000000000000000000000000000000" ] &&
git fetch -q --depth=1 origin "$BEFORE" 2>/dev/null; then
CHANGED=$(git diff --name-only "$BEFORE" "$GITHUB_SHA")
echo "changed files:"; echo "$CHANGED"
if [ -n "$CHANGED" ] && ! echo "$CHANGED" | grep -qvE '^wiki/|\.md$'; then
echo "docs-only change - no firmware build"; SKIP=true
fi
fi
fi
echo "SKIP=${SKIP}" >> "$GITHUB_ENV"
- name: Version
if: env.SKIP != 'true'
run: |
if [ "${GITHUB_REF#refs/tags/}" != "${GITHUB_REF}" ]; then
VERSION="${GITHUB_REF_NAME}"
else
VERSION="main-${GITHUB_SHA:0:7}"
fi
case "$VERSION" in *[!A-Za-z0-9._-]*) echo "::error::bad version '$VERSION'"; exit 1;; esac
[ ${#VERSION} -lt 32 ] || { echo "::error::version too long"; exit 1; }
echo "VERSION=${VERSION}" >> "$GITHUB_ENV"
echo "version: ${VERSION}"
- name: Cache PlatformIO
if: env.SKIP != 'true'
uses: actions/cache@v4
with:
path: |
~/.platformio/packages
~/.platformio/platforms
.pio/libdeps
key: pio-${{ hashFiles('platformio.ini') }}
# pioarduino's fork of the PlatformIO core, NOT upstream: the platform pins
# its own SCons 4.8.1 without the Fortran tools, and since the core rebuild
# (custom_sdkconfig, v0.1.7) the ESP-IDF build path is exercised, where the
# upstream core imports SCons.Tool.FortranCommon and dies
# ("ModuleNotFoundError: No module named 'SCons.Tool.FortranCommon'",
# first seen on the v0.1.7 tag build). The pin mirrors
# platform-espressif32/builder/penv_setup.py :: python_deps["platformio"].
- name: Install PlatformIO (pioarduino core)
if: env.SKIP != 'true'
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
uv tool install "https://github.com/pioarduino/platformio-core/archive/refs/tags/v6.1.18.zip"
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
pio --version
- name: Build
if: env.SKIP != 'true'
run: |
export PLATFORMIO_BUILD_FLAGS="-DFW_VERSION=\\\"${VERSION}\\\""
pio run
mkdir -p dist
cp .pio/build/waveshare_esp32_s3_lcd_5b/firmware.bin dist/
cp .pio/build/waveshare_esp32_s3_lcd_5b/firmware.elf dist/
grep -q "${VERSION}" dist/firmware.bin || { echo "::error::FW_VERSION not baked into the image"; exit 1; }
ls -la dist/
- name: Sign + manifest (tags only)
if: env.SKIP != 'true' && startsWith(github.ref, 'refs/tags/')
env:
BMW_NISSAN_FIRMWARE_SIGNING_KEY: ${{ secrets.BMW_NISSAN_FIRMWARE_SIGNING_KEY }}
run: |
set -e
[ -n "$BMW_NISSAN_FIRMWARE_SIGNING_KEY" ] || { echo "::error::secret BMW_NISSAN_FIRMWARE_SIGNING_KEY is not set"; exit 1; }
SHA=$(sha256sum dist/firmware.bin | cut -d' ' -f1)
SIZE=$(stat -c%s dist/firmware.bin)
printf '%s' "patrol-a|${VERSION}|${SHA}" > stmt.txt
umask 077
printf '%s\n' "$BMW_NISSAN_FIRMWARE_SIGNING_KEY" > key.pem
openssl dgst -sha256 -sign key.pem -out sig.der stmt.txt
rm -f key.pem
# Self-check against the public key the firmware carries.
openssl dgst -sha256 -verify tools/ota/firmware_signing_pub.pem -signature sig.der stmt.txt
SIG=$(od -An -v -tx1 sig.der | tr -d ' \n')
printf 'target=patrol-a\nversion=%s\nsize=%s\nsha256=%s\nsig=%s\n' \
"$VERSION" "$SIZE" "$SHA" "$SIG" > dist/manifest.txt
cat dist/manifest.txt
- name: Publish release
if: env.SKIP != 'true'
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
API: ${{ github.api_url }}
REPO: ${{ github.repository }}
run: |
set -e
auth="Authorization: token ${TOKEN}"
if [ "${GITHUB_REF#refs/tags/}" != "${GITHUB_REF}" ]; then
TAG="${GITHUB_REF_NAME}"
PRE=false
NAME="Main CTRL ${TAG}"
BODY="Signed OTA release. Install on the controller with \`ota install\` (parked, Wi-Fi joined)."
else
TAG="main-latest"
PRE=true
NAME="Main CTRL main build"
BODY="Unsigned build of main @ ${GITHUB_SHA}. Rolling - overwritten each push. Not installable over OTA; flash over USB for bench tests."
# Rolling: drop the previous release and its tag, recreate at this commit.
OLD=$(curl -sS -H "$auth" "${API}/repos/${REPO}/releases/tags/${TAG}" \
| grep -o '"id":[0-9]*' | head -1 | cut -d: -f2 || true)
if [ -n "$OLD" ]; then
curl -sS -X DELETE -H "$auth" "${API}/repos/${REPO}/releases/${OLD}" || true
curl -sS -X DELETE -H "$auth" "${API}/repos/${REPO}/git/refs/tags/${TAG}" || true
fi
fi
REL=$(curl -sS -X POST -H "$auth" -H "Content-Type: application/json" \
-d "{\"tag_name\":\"${TAG}\",\"target_commitish\":\"${GITHUB_SHA}\",\"name\":\"${NAME}\",\"body\":\"${BODY}\",\"draft\":false,\"prerelease\":${PRE}}" \
"${API}/repos/${REPO}/releases" || true)
REL_ID=$(printf '%s' "$REL" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2 || true)
if [ -z "$REL_ID" ]; then
REL_ID=$(curl -sS -H "$auth" "${API}/repos/${REPO}/releases/tags/${TAG}" \
| grep -o '"id":[0-9]*' | head -1 | cut -d: -f2 || true)
fi
[ -n "$REL_ID" ] || { echo "::error::could not create release: $REL"; exit 1; }
echo "release id: ${REL_ID}"
for f in dist/*; do
name=$(basename "$f")
echo "uploading ${name}"
code=$(curl -sS -o /tmp/up.json -w '%{http_code}' -X POST -H "$auth" \
-H "Content-Type: application/octet-stream" --data-binary @"${f}" \
"${API}/repos/${REPO}/releases/${REL_ID}/assets?name=${name}")
[ "$code" = "201" ] || { echo "::error::upload ${name} failed (HTTP ${code}): $(cat /tmp/up.json)"; exit 1; }
done
echo "published ${TAG}"