|
| 1 | +name: Build and publish python package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "*" |
| 7 | + |
| 8 | +# Define permissions needed for the workflow GITHUB_TOKEN |
| 9 | +permissions: |
| 10 | + contents: read # Allow checkout |
| 11 | + packages: write # Allow publishing to GitHub Packages |
| 12 | + |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + strategy: |
| 17 | + matrix: |
| 18 | + operating-system: [macos-latest, ubuntu-latest, windows-latest] |
| 19 | + python-version: [ 3.10, 3.11, 3.12, 3.13 ] |
| 20 | + |
| 21 | + name: Build Python Package (${{ matrix.operating-system }}, Python ${{ matrix.python-version }}) |
| 22 | + runs-on: ${{ matrix.operating-system }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout Code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + ref: ${{ github.ref }} |
| 29 | + |
| 30 | + - name: Set up Python ${{ matrix.python-version }} |
| 31 | + uses: actions/setup-python@v5 |
| 32 | + with: |
| 33 | + python-version: ${{ matrix.python-version }} |
| 34 | + |
| 35 | + - name: Install Hatch |
| 36 | + run: pip install hatch |
| 37 | + |
| 38 | + - name: Build distributions |
| 39 | + run: hatch build # Uses pyproject.toml to build sdist and wheel into dist/ |
| 40 | + |
| 41 | + - name: Upload distributions artifact |
| 42 | + uses: actions/upload-artifact@v4 # Action to save artifacts between jobs |
| 43 | + with: |
| 44 | + name: como-distribution-package-${{ matrix.operating-system }}-${{ matrix.python-version }} # Name for the artifact |
| 45 | + path: dist/ # Path to the directory to upload |
| 46 | + |
| 47 | + publish: |
| 48 | + strategy: |
| 49 | + matrix: |
| 50 | + python-version: [ 3.10, 3.11, 3.12, 3.13 ] |
| 51 | + operating-system: [macos-latest, windows-latest, ubuntu-latest] |
| 52 | + |
| 53 | + name: Publish to GitHub Packages |
| 54 | + runs-on: ubuntu-latest |
| 55 | + needs: build # Depends on the build job succeeding |
| 56 | + |
| 57 | + # IMPORTANT: Only run the publish job when a tag starting with 'v' is pushed |
| 58 | + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') |
| 59 | + |
| 60 | + |
| 61 | + permissions: # Explicit permissions needed for this job |
| 62 | + packages: write # Required to write to GitHub Packages registry |
| 63 | + contents: read # Needed if accessing repo content (e.g., for download artifact) |
| 64 | + |
| 65 | + steps: |
| 66 | + - name: Download distributions artifact |
| 67 | + uses: actions/download-artifact@v4 # Action to retrieve artifacts from previous job. |
| 68 | + with: |
| 69 | + name: como-distribution-package-${{ matrix.operating-system }}-${{ matrix.python-version }} |
| 70 | + path: dist/ |
| 71 | + |
| 72 | + - name: Set up Python 3.11 |
| 73 | + uses: actions/setup-python@v5 |
| 74 | + with: |
| 75 | + python-version: ${{ matrix.python-version }} |
| 76 | + |
| 77 | + - name: Install Twine |
| 78 | + run: pip install twine |
| 79 | + |
| 80 | + - name: Publish package to GitHub Packages |
| 81 | + env: |
| 82 | + # Use __token__ as username and the automatically generated GITHUB_TOKEN as password |
| 83 | + TWINE_USERNAME: __token__ |
| 84 | + TWINE_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + run: | |
| 86 | + echo "Uploading to GitHub Packages for repository: ${{ github.repository }}" |
| 87 | + # Construct the repository URL dynamically using the repository owner |
| 88 | + TWINE_REPOSITORY_URL="https://pypi.pkg.github.com/${{ github.repository_owner }}" |
| 89 | + python -m twine upload --verbose --repository-url ${TWINE_REPOSITORY_URL} dist/* |
| 90 | +
|
| 91 | + |
| 92 | + |
0 commit comments