name: Python Package Release # Controparte Python di npm-publish.yml: sul tag v* verifica, costruisce e pubblica un # pacchetto (wheel + sdist) sul registry PyPI di Gitea dell'organizzazione PZeta_Touch. # Da usare per le librerie Python (es. il fork python-chatterbox), che non hanno un # Dockerfile e quindi non possono passare da python-docker-release.yml. # # Uso tipico in .gitea/workflows/release.yml del repository: # # jobs: # package-release: # uses: https://gitea.pzetatouch.it/devops/shared-actions/.gitea/workflows/python-package-release.yml@v1 # secrets: inherit # auto-release: # needs: [package-release] # uses: https://gitea.pzetatouch.it/devops/shared-actions/.gitea/workflows/python-auto-release.yml@v1 # secrets: inherit # # Secrets (di organizzazione, gli stessi del login al registry in python-docker-release.yml): # - G_USER utente Gitea proprietario del token # - NPM_TOKEN token Gitea con scope package (nonostante il nome, vale per tutti i # registry dei pacchetti Gitea: npm, container, PyPI) # # Installazione del pacchetto pubblicato: # pip install --index-url https://gitea.pzetatouch.it/api/packages/PZeta_Touch/pypi/simple/ \ # --extra-index-url https://pypi.org/simple on: workflow_call: inputs: python-version: type: string default: '3.11' install-extras: description: 'Extras pip da installare (es: dev, test). Vuoto: requirements-dev.txt / requirements.txt' type: string default: 'dev' working-directory: description: 'Directory di lavoro se il progetto non è in root' type: string default: '.' jobs: python-package-release: runs-on: ubuntu-latest # 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: 30 defaults: run: working-directory: ${{ inputs.working-directory }} env: # Endpoint PyPI di Gitea per l'owner PZeta_Touch (upload e API dei pacchetti). PYPI_UPLOAD_URL: https://gitea.pzetatouch.it/api/packages/PZeta_Touch/pypi PACKAGES_API_URL: https://gitea.pzetatouch.it/api/v1/packages/PZeta_Touch/pypi 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" # Controlli economici PRIMA dell'installazione delle dipendenze (che per i progetti # ML scarica gigabyte): se il tag non corrisponde o la versione esiste già, il job # fallisce in pochi secondi invece che dopo l'intera suite. - name: Leggi nome e versione da pyproject.toml id: meta run: | python - <<'EOF' >> "$GITHUB_OUTPUT" import pathlib, re, tomllib project = tomllib.loads(pathlib.Path("pyproject.toml").read_text())["project"] # Gitea normalizza i nomi PyPI come PEP 503 (minuscolo, separatori -> '-'). print("name=" + re.sub(r"[-_.]+", "-", project["name"]).lower()) print("version=" + project["version"]) EOF # Il tag è il trigger del rilascio, la versione nel pyproject è ciò che finisce nei # metadati del pacchetto: se divergono si pubblicherebbe sotto un numero diverso da # quello taggato. - name: Verifica tag = v + versione env: VERSION: ${{ steps.meta.outputs.version }} run: | TAG="${GITHUB_REF_NAME:-${GITHUB_REF#refs/tags/}}" if [ "$TAG" != "v$VERSION" ]; then echo "::error::Il tag '$TAG' non corrisponde alla versione del pyproject.toml ($VERSION): atteso 'v$VERSION'. Allinea [project].version e ritagga." exit 1 fi echo "Tag $TAG coerente con la versione $VERSION" # Un registry PyPI non ammette di ripubblicare una versione: meglio fermarsi con un # messaggio esplicito che con il 409 di twine a fine job. Qualsiasi risposta diversa # da 200/404 (token scaduto, registry giù) blocca: non si pubblica alla cieca. - name: Verifica che la versione non sia già pubblicata timeout-minutes: 5 env: NAME: ${{ steps.meta.outputs.name }} VERSION: ${{ steps.meta.outputs.version }} G_USER: ${{ secrets.G_USER }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | if [ -z "$G_USER" ] || [ -z "$NPM_TOKEN" ]; then echo "::error::Secrets G_USER e/o NPM_TOKEN non disponibili: serve 'secrets: inherit' nel workflow chiamante." exit 1 fi STATUS=$(curl -s -o /dev/null -w '%{http_code}' -u "$G_USER:$NPM_TOKEN" \ "$PACKAGES_API_URL/$NAME/$VERSION") case "$STATUS" in 404) echo "$NAME $VERSION non ancora pubblicato: si procede" ;; 200) echo "::error::$NAME $VERSION è già pubblicato sul registry PyPI di Gitea. Una versione pubblicata non si sovrascrive: incrementa [project].version e crea un nuovo tag." exit 1 ;; *) echo "::error::Impossibile verificare $NAME $VERSION sul registry (HTTP $STATUS)." exit 1 ;; esac - 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- # Stesso schema di python-quality-gates.yml: install-extras vuoto lascia al progetto # il controllo dell'installazione tramite requirements-dev.txt (es. per forzare le # wheel CPU di torch con --extra-index-url, non esprimibile in pyproject.toml). - 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 pip install build twine - 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 # Build isolata (PEP 517): il backend dichiarato in [build-system] viene installato in # un ambiente temporaneo, quindi l'artefatto non dipende da cosa c'è nel venv del job. - name: Build (wheel + sdist) timeout-minutes: 10 run: | rm -rf dist python -m build ls -l dist - name: Verifica metadati (twine check) run: twine check --strict dist/* - name: Publish sul registry PyPI di Gitea timeout-minutes: 10 env: TWINE_REPOSITORY_URL: ${{ env.PYPI_UPLOAD_URL }} TWINE_USERNAME: ${{ secrets.G_USER }} TWINE_PASSWORD: ${{ secrets.NPM_TOKEN }} run: twine upload --non-interactive dist/*