Files
ESP32-S3-LCD-1.3/.gitea/workflows/firmware.yml
T

164 lines
7.3 KiB
YAML

name: Firmware
# Inst. Cluster firmware, built by PlatformIO on every push. Same scheme as
# Main CTRL (ESP32-S3-Touch-LCD-5B/.gitea/workflows/firmware.yml):
#
# - push to master -> rolling PRE-release `main-latest` (firmware.bin + .elf),
# unsigned, no manifest. Bench / USB flashing only; the
# device reads /releases/latest, which skips pre-releases.
# - push tag v* -> the OTA release: firmware.bin + firmware.elf +
# manifest.txt (size, sha256, ECDSA P-256 signature over
# "patrol-b|<tag>|<sha256>"). Inst. Cluster pulls this when
# A's Updates screen (or `ota install` on B) asks for it.
#
# Signing key: secret BMW_NISSAN_FIRMWARE_SIGNING_KEY (PEM, EC P-256), the same
# key as Main CTRL's; the target string keeps the two apart. Its public half
# is compiled in (include/config/ota_config.h) and kept in
# tools/ota/firmware_signing_pub.pem for the self-check below.
#
# This project is on the stock espressif32@6 platform (Arduino 2.0.17), so the
# upstream PlatformIO core is fine here - the pioarduino fork is only needed
# on A. A master push skips itself when the commit already carries a v* tag or
# only changes docs.
on:
push:
branches: [master]
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
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
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 16 ] || { echo "::error::version too long for link_status_t::version (15 chars max)"; 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-b-${{ hashFiles('platformio.ini') }}
- name: Install PlatformIO
if: env.SKIP != 'true'
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
uv tool install platformio
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/esp32_s3_lcd_1_3/firmware.bin dist/
cp .pio/build/esp32_s3_lcd_1_3/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-b|${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
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-b\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="Inst. Cluster ${TAG}"
BODY="Signed OTA release. Installed from Main CTRL's Updates screen (parked) or with \`ota install\` on Inst. Cluster's console."
else
TAG="main-latest"
PRE=true
NAME="Inst. Cluster main build"
BODY="Unsigned build of master @ ${GITHUB_SHA}. Rolling - overwritten each push. Not installable over OTA; flash over USB for bench tests."
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}"