name: Release Trimesh

on:
  push:
    branches:
      - main
      - release-candidate

permissions:
  contents: read

jobs:
  formatting:
    name: Check Code Formatting
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Set up Python
      uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
      with:
        python-version: "3.13"
    - name: Install Formatting
      run: |
        pip install ruff
    - name: Check Formatting
      run: ruff check .
  tests:
    name: Run Unit Tests
    needs: formatting
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
        os: [ubuntu-latest, macos-latest, macos-15-intel, windows-latest]
        exclude:
            # windows runners have gotten flaky
          - os: windows-latest
            python-version: 3.8
          - os: windows-latest
            python-version: 3.9
          - os: macos-13
            python-version: 3.9
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
      with:
        python-version: ${{ matrix.python-version }}
    - name: Test a minimal install
      run: |
        pip install --no-cache-dir .
        python tests/test_minimal.py
    - name: Install Trimesh
      run: pip install --no-cache-dir .[easy,test]
    - name: Run Pytest
      run: pytest tests/

  pypi:
    name: Release To PyPi
    needs: [tests, containers, corpus]
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Set up Python
      uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
      with:
        python-version: '3.13'
    - name: Install publishing dependencies
      run: pip install build
    - name: Build
      run: pyproject-build --outdir dist .
    - name: Publish
      uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0

  containers:
    name: Build Docker Images
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
    - name: Log In to Docker Hub
      env:
        DH_PASS: ${{ secrets.DH_PASS }}
      run: echo "$DH_PASS" | docker login --username mikedh --password-stdin
    - name: Checkout trimesh
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Build Images And Docs
      env:
        CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
      run: |
        make tests # build docker images and run unit tests
        make publish-docker # push images to docker hub
        make docs # build sphinx docs
    - name: Configure Pages
      uses: actions/configure-pages@v5
    - name: Upload Pages artifact
      uses: actions/upload-pages-artifact@v3
      with:
        path: ./html
    - name: Deploy to GitHub Pages
      id: deployment
      uses: actions/deploy-pages@v4
    - name: Logout of registries
      if: always()
      run: |
        docker logout

  corpus:
    runs-on: ubuntu-latest
    name: Check Corpus Loading
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Trimesh DiskCache
      id: cache-resolvers
      uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
      with:
        path: ~/.trimesh-cache
        key: trimesh-cache
    - name: Set up Python 3.10
      uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
      with:
         python-version: "3.10"
    - name: Install Trimesh
      run: pip install .[easy,test]
    - name: Run Corpus Check
      run: python tests/corpus.py -run

  release:
    permissions:
      contents: write  # for creating releases
    name: Create GitHub Release
    needs: [pypi]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Tag Version
        id: set_tag
        run: |
          export VER=$(python trimesh/version.py)
          echo "tag_name=${VER}" >> "$GITHUB_OUTPUT"
      - name: Create Release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "${{ steps.set_tag.outputs.tag_name }}" \
            --title "Release ${{ steps.set_tag.outputs.tag_name }}" \
            --generate-notes
