Release Python Package to PyPI #176
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Release Python Package to PyPI | |
| on: | |
| workflow_dispatch: | |
| release: | |
| types: [published] | |
| jobs: | |
| build_deps_windows: | |
| name: Build Windows Dependencies | |
| runs-on: windows-latest | |
| steps: | |
| - uses: actions/checkout@v5 | |
| with: | |
| lfs: true | |
| - name: Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| cache: 'pip' | |
| - name: Install Meson using Python | |
| run: | | |
| python -m pip install --upgrade pip meson | |
| - name: Set branch name | |
| run: echo "BRANCH_NAME=${{ runner.os }}_${{ runner.arch }}_build_cache" >> $GITHUB_ENV | |
| shell: bash | |
| - name: Configure and build C++ API via CMake | |
| shell: pwsh | |
| run: | | |
| $ErrorActionPreference = "Stop" | |
| git clone -b "$env:BRANCH_NAME" --single-branch https://github.com/PycraftDeveloper/PMMA.git build_cache | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "Cache branch missing, creating..." | |
| git checkout --orphan $env:BRANCH_NAME | |
| git lfs install | |
| git rm -rf . | |
| git config --global user.email "github-actions@github.com" | |
| git config --global user.name "GitHub Actions" | |
| git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/PycraftDeveloper/PMMA.git | |
| git commit --allow-empty -m "Initial commit" | |
| git push -u origin $env:BRANCH_NAME | |
| cd .. | |
| Remove-Item -Path "$env:GITHUB_WORKSPACE\PMMA" -Recurse -Force -ErrorAction SilentlyContinue | |
| git clone https://github.com/${{ github.repository }} /PMMA | |
| cd /PMMA | |
| git lfs install | |
| git clone -b $env:BRANCH_NAME --single-branch https://github.com/PycraftDeveloper/PMMA.git build_cache | |
| } | |
| python "build_tools/build_deps.py" -in_github_workflow | |
| cd build_cache | |
| git clean -fdx | |
| cd .. | |
| Get-ChildItem -Path "build_tools" -Recurse | ForEach-Object { | |
| $destination = $_.FullName -replace [regex]::Escape("build_tools"), "build_cache" | |
| if (!(Test-Path $destination)) { | |
| Copy-Item -Path $_.FullName -Destination $destination -Recurse | |
| } | |
| } | |
| cd build_cache | |
| git add -A | |
| git config --global user.email "github-actions@github.com" | |
| git config --global user.name "GitHub Actions" | |
| git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/PycraftDeveloper/PMMA.git | |
| git commit -m "Update Build Cache" | |
| git push -u origin $env:BRANCH_NAME | |
| build_deps_linux: | |
| name: Build AnyLinux Dependencies | |
| runs-on: ubuntu-latest | |
| env: | |
| DOCKER_IMAGE: docker.io/pycraftdev/pmma-manylinux:latest | |
| steps: | |
| - name: Pre-pull Docker image (cached by GitHub’s runner) | |
| run: docker pull $DOCKER_IMAGE | |
| - name: Set branch name | |
| run: echo "BRANCH_NAME=${{ runner.os }}_${{ runner.arch }}_build_cache" >> $GITHUB_ENV | |
| shell: bash | |
| - name: Build Linux wheel inside Docker | |
| run: | | |
| docker run --rm \ | |
| -e TERM=xterm \ | |
| -e BRANCH_NAME=$BRANCH_NAME \ | |
| -v "${{ github.workspace }}/dist:/output_dist" \ | |
| $DOCKER_IMAGE \ | |
| /bin/bash -c ' | |
| set -e | |
| git clone https://github.com/${{ github.repository }} /PMMA | |
| cd /PMMA | |
| if git clone -b "$BRANCH_NAME" --single-branch https://github.com/PycraftDeveloper/PMMA.git build_cache; then | |
| echo "Cache branch exists, cloned successfully." | |
| else | |
| echo "Cache branch missing, creating..." | |
| git checkout --orphan "$BRANCH_NAME" | |
| git lfs install | |
| git rm -rf . | |
| git config --global user.email "github-actions@github.com" | |
| git config --global user.name "GitHub Actions" | |
| git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/PycraftDeveloper/PMMA.git | |
| git commit --allow-empty -m "Initial commit" | |
| git push -u origin "$BRANCH_NAME" | |
| cd .. | |
| rm -rf /PMMA | |
| git clone https://github.com/${{ github.repository }} /PMMA | |
| cd /PMMA | |
| git lfs install | |
| git clone -b "$BRANCH_NAME" --single-branch https://github.com/PycraftDeveloper/PMMA.git build_cache | |
| fi | |
| python3 "build_tools/build_deps.py" -in_github_workflow | |
| cd build_cache | |
| git clean -fdx | |
| cd .. | |
| cp -r build_tools/* build_cache/ | |
| cd build_cache | |
| git add -A | |
| git config --global user.email "github-actions@github.com" | |
| git config --global user.name "GitHub Actions" | |
| git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/PycraftDeveloper/PMMA.git | |
| git commit -m "Update Build Cache" | |
| git push -u origin "$BRANCH_NAME" | |
| ' | |
| build_linux: # How do I set the environment variable here? | |
| name: Build Linux wheels | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| python-tag: ["cp38-cp38", "cp39-cp39", "cp310-cp310", "cp311-cp311"] | |
| env: | |
| DOCKER_IMAGE: docker.io/pycraftdev/pmma-manylinux:latest | |
| needs: [build_deps_linux] | |
| steps: | |
| - name: Ensure dist directory exists | |
| run: mkdir -p dist | |
| - name: Pre-pull Docker image (cached by GitHub’s runner) | |
| run: docker pull $DOCKER_IMAGE | |
| - name: Set branch name | |
| run: echo "BRANCH_NAME=${{ runner.os }}_${{ runner.arch }}_build_cache" >> $GITHUB_ENV | |
| shell: bash | |
| - name: Build Linux wheel inside Docker | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| docker run --rm \ | |
| -e TERM=xterm \ | |
| -e BRANCH_NAME=$BRANCH_NAME \ | |
| -e PYTHON_TAG=${{ matrix.python-tag }} \ | |
| -v "${{ github.workspace }}/dist:/output_dist" \ | |
| $DOCKER_IMAGE \ | |
| /bin/bash -c ' | |
| set -e | |
| tag=$PYTHON_TAG | |
| dir="/tmp/build_$tag" | |
| git clone https://github.com/${{ github.repository }} "$dir" | |
| cd "$dir" | |
| git clone -b "$BRANCH_NAME" --single-branch https://github.com/PycraftDeveloper/PMMA.git build_cache | |
| PYTHON_BIN="/opt/python/$tag/bin/python" | |
| python3 "build_tools/workflow_integration.py" -in_github_workflow | |
| $PYTHON_BIN "build_tools/build_pmma.py" -in_github_workflow | |
| original=$(ls dist/*.whl) | |
| renamed=$(echo "$original" | sed "s/linux/manylinux_2_34/") | |
| mv "$original" "$renamed" | |
| cp dist/* /output_dist/ | |
| ' | |
| - name: Upload wheel | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: wheel-linux-${{ matrix.python-tag }} | |
| path: dist/*.whl | |
| build_windows: | |
| strategy: | |
| matrix: | |
| include: | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.8' | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.9' | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.10' | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.11' | |
| runs-on: ${{ matrix.runs-on }} | |
| needs: [build_deps_windows] | |
| steps: | |
| - uses: actions/checkout@v5 | |
| with: | |
| lfs: true | |
| # Python with built-in pip cache | |
| - name: Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| cache: 'pip' | |
| # Install Python build deps (reuses pip cache above) | |
| - name: Install Python build tools | |
| run: | | |
| python -m pip install --upgrade pip setuptools wheel cython numpy | |
| - name: Set branch name | |
| run: echo "BRANCH_NAME=${{ runner.os }}_${{ runner.arch }}_build_cache" >> $GITHUB_ENV | |
| shell: bash | |
| - name: Configure and build C++ API via CMake | |
| run: | | |
| git clone -b $env:BRANCH_NAME --single-branch https://github.com/PycraftDeveloper/PMMA.git build_cache | |
| python "build_tools/workflow_integration.py" -in_github_workflow | |
| python "build_tools/build_pmma.py" -in_github_workflow | |
| - name: Upload wheel | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: wheel-${{ matrix.os }}-${{ matrix.python-version }} | |
| path: dist/*.whl | |
| # ────────────────────────────────────── | |
| # 3. sdist and publish | |
| # ────────────────────────────────────── | |
| sdist: | |
| name: Build Source Distribution | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v5 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.10' | |
| - run: | | |
| pip install setuptools wheel cython numpy | |
| python setup.py sdist | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: sdist | |
| path: dist/*.tar.gz | |
| publish: | |
| name: Publish to PyPI + GitHub Release | |
| runs-on: ubuntu-latest | |
| needs: [build_windows, sdist, build_linux] | |
| steps: | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: ./artifacts | |
| - name: Move distribution files | |
| run: | | |
| mkdir -p dist | |
| find artifacts -type f \( -name "*.whl" -o -name "*.tar.gz" \) -exec mv {} dist/ \; | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@v1.12.4 | |
| with: | |
| user: __token__ | |
| password: ${{ secrets.PYPI_API_TOKEN }} | |
| packages_dir: dist | |
| - name: Upload to GitHub Release | |
| if: github.event_name == 'release' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| UPLOAD_URL: ${{ github.event.release.upload_url }} | |
| run: | | |
| for f in dist/*; do | |
| echo "Uploading $f ..." | |
| curl -X POST \ | |
| -H "Authorization: token $GITHUB_TOKEN" \ | |
| -H "Content-Type: application/octet-stream" \ | |
| --data-binary @"$f" \ | |
| "${UPLOAD_URL%\{*}?name=$(basename "$f")" | |
| done |