name: Build Pip package

on:
  workflow_dispatch:
  push:
    branches:
      - '**'
    tags:
      - 'v*'

env:
  PYTHON_VERSION: "3.13"

jobs:
  build:
    permissions:
      id-token: write

    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v4
        with:
          enable-cache: true

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          architecture: x64

      - name: APT install archivebox dev + run dependencies
        uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: ripgrep build-essential python3-dev python3-setuptools libssl-dev libldap2-dev libsasl2-dev zlib1g-dev libatomic1 gnupg2 curl wget python3-ldap python3-msgpack python3-mutagen python3-regex python3-pycryptodome procps
          version: 1.0

      - name: UV install archivebox dev + run sub-dependencies
        run: uv sync --frozen --all-extras --no-install-project --no-install-workspace

      - name: UV build archivebox and archivebox/pkgs/* packages
        run: |
          uv build --all

      - name: Publish new package wheels and sdists to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        # ignore when publish to PyPI fails due to duplicate tag
        continue-on-error: true
        with:
          password: ${{ secrets.PYPI_PAT_SECRET }}

      - name: UV install archivebox and archivebox/pkgs/* locally for tests
        run: uv sync --frozen --all-extras

      - name: UV run archivebox init + archivebox version
        run: |
          mkdir -p data && cd data
          uv run archivebox init \
          && uv run archivebox version
          # && uv run archivebox add 'https://example.com' \
          # && uv run archivebox status \
          # || (echo "UV Failed to run archivebox!" && exit 1)

