From f14904feca28cf7ec55fee18c7513689258c06c4 Mon Sep 17 00:00:00 2001 From: Lucas Rayan Date: Tue, 10 Nov 2026 12:32:57 -0300 Subject: [PATCH] =?UTF-8?q?ci:=20cache=20de=20build=20compartilhado=20e=20?= =?UTF-8?q?compila=C3=A7=C3=A3o=20em=20paralelo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .gitea/scripts/compilar.py | 45 +++++++++++++++++++++++++++++++++++ .gitea/workflows/compilar.yml | 24 ++++++------------- 2 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 .gitea/scripts/compilar.py diff --git a/.gitea/scripts/compilar.py b/.gitea/scripts/compilar.py new file mode 100644 index 0000000..441cf49 --- /dev/null +++ b/.gitea/scripts/compilar.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +"""Compila todos os projetos de uma série. Uso: compilar.py [workers] + +Cada worker copia o projeto para uma pasta fixa antes de compilar: com o mesmo +caminho, os objetos do framework batem no cache de build compartilhado +(PLATFORMIO_BUILD_CACHE_DIR) e só recompilam quando o sdkconfig muda. +""" +import glob, os, queue, shutil, subprocess, sys, threading, time + +serie = sys.argv[1] +n = int(sys.argv[2]) if len(sys.argv) > 2 else os.cpu_count() +pio = os.environ.get("PIO", os.path.expanduser("~/pio-venv/bin/pio")) +projetos = sorted(os.path.dirname(p) for p in glob.glob("capitulo-*/*/platformio.ini") + if f"[env:{serie}]" in open(p).read()) +fila = queue.Queue() +for p in projetos: + fila.put(p) +falhas, lock = [], threading.Lock() + + +def worker(i): + base = f"/tmp/obra/{i}" + while True: + try: + proj = fila.get_nowait() + except queue.Empty: + return + shutil.rmtree(base, ignore_errors=True) + shutil.copytree(proj, base) + t = time.time() + r = subprocess.run([pio, "run", "-s", "-d", base, "-e", serie], + capture_output=True, text=True) + with lock: + print(f"{'OK ' if r.returncode == 0 else 'FALHA'} {proj} ({time.time() - t:.0f} s)", flush=True) + if r.returncode: + falhas.append((proj, (r.stdout + r.stderr)[-4000:])) + + +ts = [threading.Thread(target=worker, args=(i,)) for i in range(n)] +[t.start() for t in ts] +[t.join() for t in ts] +for proj, log in falhas: + print(f"\n===== {proj} =====\n{log}") +print(f"\n{len(projetos) - len(falhas)}/{len(projetos)} compilações OK em {serie} ({n} workers)") +sys.exit(1 if falhas else 0) diff --git a/.gitea/workflows/compilar.yml b/.gitea/workflows/compilar.yml index f67d64e..794139b 100644 --- a/.gitea/workflows/compilar.yml +++ b/.gitea/workflows/compilar.yml @@ -16,7 +16,7 @@ jobs: compilar: name: ${{ matrix.serie }} runs-on: ubuntu-latest - timeout-minutes: 300 + timeout-minutes: 360 strategy: fail-fast: false matrix: @@ -25,26 +25,16 @@ jobs: - uses: actions/checkout@v4 - uses: actions/cache@v4 with: - path: ~/.platformio - key: pio-${{ runner.arch }}-${{ matrix.serie }}-${{ hashFiles('**/platformio.ini') }} + path: | + ~/.platformio + ~/pio-cache + key: pio-${{ runner.arch }}-${{ matrix.serie }}-${{ github.sha }} restore-keys: pio-${{ runner.arch }}-${{ matrix.serie }}- - name: Instalar o PlatformIO run: | python3 -m venv ~/pio-venv ~/pio-venv/bin/pip install -q platformio - name: Compilar - env: - SERIE: ${{ matrix.serie }} run: | - falhas=0; total=0 - for ini in $(grep -l "^\[env:${SERIE}\]" capitulo-*/*/platformio.ini | sort); do - d=$(dirname "$ini"); total=$((total+1)) - if ~/pio-venv/bin/pio run -s -d "$d" -e "$SERIE" > log.txt 2>&1; then - echo "OK $d" - else - echo "FALHA $d"; tail -60 log.txt; falhas=$((falhas+1)) - fi - rm -rf "$d/.pio" - done - echo "$((total-falhas))/$total compilações OK em $SERIE" - test "$falhas" -eq 0 + echo "CPUs: $(nproc)" + PLATFORMIO_BUILD_CACHE_DIR=$HOME/pio-cache python3 .gitea/scripts/compilar.py ${{ matrix.serie }} $(nproc)