Files
shared-actions/.gitea/workflows/python-docker-release.yml
T
LucaZanni 3d28471b2c 🐛 fix(ci): ogni workflow condiviso ha un timeout, sul job e sui passi di rete
- timeout-minutes sul job di tutti e 16 i workflow riusabili: 10 per branch-cleanup,
  15-20 per release e manutenzione, 30 per i quality gates e le pubblicazioni,
  45 per le immagini Docker, 60 per la build Android
- timeout-minutes su 95 passi che dipendono dalla rete (checkout, setup-*, cache,
  install, audit, login, push, upload): con il solo timeout sul job il log non dice
  quale passo si è impiantato, con questi sì
- valori letterali e non input: su un job che chiama un workflow riusabile
  timeout-minutes non è una chiave ammessa, quindi il limite può stare solo qui,
  e i valori scelti sono già ampi rispetto alla durata reale dei job

Fixes #23 @1h30m
2026-10-20 22:33:53 +02:00

189 lines
7.8 KiB
YAML

name: Python Docker Release
on:
workflow_call:
inputs:
python-version:
type: string
default: '3.11'
install-extras:
description: 'Extras pip da installare (es: dev, test)'
type: string
default: 'dev'
runner:
type: string
default: 'catthehacker-latest'
working-directory:
description: 'Directory di lavoro se il progetto non è in root'
type: string
default: '.'
extra-index-gitea:
description: >-
Aggiunge il registry PyPI privato di Gitea (PZeta_Touch) come indice extra di pip,
autenticato con i secrets G_USER / NPM_TOKEN (serve 'secrets: inherit').
# Stringa e non boolean: su act_runner i valori boolean passati con with: da un
# workflow chiamante non arrivano al workflow riusabile (resta il default false),
# mentre le stringhe si. Il chiamante scrive extra-index-gitea: 'true'.
type: string
default: 'false'
dockerfile:
description: 'Path al Dockerfile'
type: string
default: './Dockerfile'
jobs:
python-docker-release:
runs-on: ${{ inputs.runner }}
# Il chiamante non puo' dichiararlo: su un job che usa un workflow
# riusabile timeout-minutes non e' una chiave ammessa (devops/shared-actions#23).
timeout-minutes: 45
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- name: Checkout
timeout-minutes: 10
uses: https://gitea.pzetatouch.it/actions/checkout@v7
- name: Setup Python
timeout-minutes: 10
uses: https://gitea.pzetatouch.it/actions/setup-python@v7
with:
python-version: ${{ inputs.python-version || '3.11' }}
# Il toolcache di setup-python è condiviso tra i job dello stesso runner: un
# 'pip install -e .' fatto lì da un altro repository (es. python-wyoming, che
# pubblica il pacchetto 'wyoming') sopravvive alla cancellazione della sua
# workspace e lascia un'installazione editable rotta che pip considera già
# soddisfatta. Ogni job installa quindi in un virtualenv proprio.
- name: Virtualenv isolato
run: |
python -m venv "$RUNNER_TEMP/venv"
echo "$RUNNER_TEMP/venv/bin" >> "$GITHUB_PATH"
echo "VIRTUAL_ENV=$RUNNER_TEMP/venv" >> "$GITHUB_ENV"
# Registry PyPI privato di Gitea (#17). Le credenziali vanno in un file netrc e non
# nell'URL: PIP_EXTRA_INDEX_URL resta senza segreti, pip legge utente e token dal file
# indicato da NETRC, e nulla di sensibile passa per l'env esportato o per i log.
# L'input si legge come variabile d'ambiente e non nel campo `if:` dello step: nel
# workflow_call di act_runner i valori `with:` del chiamante non arrivano al contesto
# `inputs` in quella posizione (stesso problema di owns-auth-roles in quality-gates.yml).
- name: Registry PyPI di Gitea
id: pypi-gitea
env:
EXTRA_INDEX_GITEA: ${{ inputs.extra-index-gitea }}
G_USER: ${{ secrets.G_USER }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
if ! printf "%s" "$EXTRA_INDEX_GITEA" | grep -qix "true"; then
echo "extra-index-gitea non attivo: solo PyPI pubblico."
exit 0
fi
if [ -z "$G_USER" ] || [ -z "$NPM_TOKEN" ]; then
echo "::error::extra-index-gitea richiede i secrets G_USER e NPM_TOKEN: serve 'secrets: inherit' nel workflow chiamante."
exit 1
fi
NETRC_FILE="$RUNNER_TEMP/pip-gitea.netrc"
( umask 077; printf 'machine gitea.pzetatouch.it\nlogin %s\npassword %s\n' "$G_USER" "$NPM_TOKEN" > "$NETRC_FILE" )
echo "NETRC=$NETRC_FILE" >> "$GITHUB_ENV"
echo "PIP_EXTRA_INDEX_URL=https://gitea.pzetatouch.it/api/packages/PZeta_Touch/pypi/simple/" >> "$GITHUB_ENV"
echo "secret_files=pip_netrc=$NETRC_FILE" >> "$GITHUB_OUTPUT"
echo "Registry PyPI di Gitea aggiunto come indice extra (credenziali da netrc)."
- name: Cache pip
timeout-minutes: 10
uses: https://gitea.pzetatouch.it/actions/cache@v6
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ inputs.python-version }}-${{ hashFiles('**/pyproject.toml', '**/requirements*.txt') }}
restore-keys: |
${{ runner.os }}-pip-${{ inputs.python-version }}-
${{ runner.os }}-pip-
- name: Install dependencies
timeout-minutes: 15
run: |
python -m pip install --upgrade pip
if [ -n "${{ inputs.install-extras }}" ]; then
pip install -e ".[${{ inputs.install-extras }}]"
elif [ -f requirements-dev.txt ]; then
pip install -r requirements-dev.txt
elif [ -f requirements.txt ]; then
pip install -r requirements.txt
else
pip install -e "."
fi
- name: Lint (ruff)
run: ruff check .
- name: Format check (ruff)
run: ruff format --check .
- name: Type check (mypy)
run: mypy .
- name: Test (pytest)
timeout-minutes: 20
run: pytest --tb=short -q
- name: Login to container registry
timeout-minutes: 5
uses: https://gitea.pzetatouch.it/actions/login-action@v4
with:
registry: gitea.pzetatouch.it
username: ${{ secrets.G_USER }}
password: ${{ secrets.NPM_TOKEN }}
- name: Determine image tags
id: tags
run: |
APP_NAME=$(python -c "
import tomllib, pathlib
data = tomllib.loads(pathlib.Path('pyproject.toml').read_text())
print(data['project']['name'])
" 2>/dev/null || python3 -c "
import re, pathlib
content = pathlib.Path('pyproject.toml').read_text()
m = re.search(r'^name\s*=\s*[\"\'](.*?)[\"\']', content, re.MULTILINE)
print(m.group(1) if m else 'unknown')
")
VERSION=$(python -c "
import tomllib, pathlib
data = tomllib.loads(pathlib.Path('pyproject.toml').read_text())
print(data['project']['version'])
" 2>/dev/null || python3 -c "
import re, pathlib
content = pathlib.Path('pyproject.toml').read_text()
m = re.search(r'^version\s*=\s*[\"\'](.*?)[\"\']', content, re.MULTILINE)
print(m.group(1) if m else '0.0.0')
")
REGISTRY="gitea.pzetatouch.it/pzeta_touch"
echo "version_tag=${REGISTRY}/${APP_NAME}:${VERSION}" >> $GITHUB_OUTPUT
echo "latest_tag=${REGISTRY}/${APP_NAME}:latest" >> $GITHUB_OUTPUT
echo "version=${VERSION}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
timeout-minutes: 30
uses: https://gitea.pzetatouch.it/actions/build-push-action@v7
with:
push: true
context: ${{ inputs.working-directory }}
file: ${{ inputs.dockerfile }}
tags: |
${{ steps.tags.outputs.version_tag }}
${{ steps.tags.outputs.latest_tag }}
labels: |
org.opencontainers.image.source=${{ gitea.server_url }}/${{ gitea.repository }}
org.opencontainers.image.revision=${{ gitea.sha }}
org.opencontainers.image.version=${{ steps.tags.outputs.version }}
# Con extra-index-gitea il netrc arriva a BuildKit come secret 'pip_netrc' (#17): il
# Dockerfile lo monta solo per la durata del RUN che installa
# RUN --mount=type=secret,id=pip_netrc,target=/root/.netrc pip install ...
# e il token non entra negli strati dell'immagine. Vuoto se l'input non e' attivo.
secret-files: ${{ steps.pypi-gitea.outputs.secret_files }}
- name: Rimozione credenziali registry PyPI
if: always()
run: rm -f "$RUNNER_TEMP/pip-gitea.netrc"