|
| 1 | +name: Build & Publish package to PyPI |
| 2 | + |
| 3 | +# CI setup instructions: |
| 4 | +# 1. Replace 'assessingsolar/sunlib' and 'sunlib' with your GitHub organization and repository name |
| 5 | +# 2. Create a new environment for additional protection and security in the GitHub UI: |
| 6 | +# Settings > Environments |
| 7 | +# Name: release |
| 8 | +# 3. Setup trusted publishing for the release environment: |
| 9 | +# https://docs.pypi.org/trusted-publishers/ |
| 10 | +# 4. Ensure your main development branch is named 'main'; if not, update the workflow or rename the branch |
| 11 | + |
| 12 | +# This workflow is triggered on pull requests that target the main branch |
| 13 | +# on pushes to the main branch new tags that start with 'v' (for example, 'v1.0.0'). |
| 14 | +# Only the main branch is used for publishing to PyPI, in the second job. |
| 15 | +on: |
| 16 | + pull_request: |
| 17 | + branches: |
| 18 | + - main |
| 19 | + push: |
| 20 | + branches: |
| 21 | + - main |
| 22 | + tags: |
| 23 | + - "v*" |
| 24 | + |
| 25 | +env: |
| 26 | + python-version: "3.12" |
| 27 | + |
| 28 | +jobs: |
| 29 | + build-distribution: |
| 30 | + name: Build distribution |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + # Deep clone to fetch all commits and tags, by setting the fetch-depth to 0 |
| 35 | + with: |
| 36 | + fetch-depth: 0 |
| 37 | + |
| 38 | + - name: Set up Python |
| 39 | + uses: actions/setup-python@v5 |
| 40 | + with: |
| 41 | + python-version: ${{ env.python-version }} |
| 42 | + |
| 43 | + - name: Install build tools |
| 44 | + run: | |
| 45 | + python -m pip install --upgrade pip |
| 46 | + python -m pip install --upgrade setuptools wheel build twine |
| 47 | +
|
| 48 | + - name: Build packages |
| 49 | + # creates the necessary distribution files to /dist |
| 50 | + run: python -m build |
| 51 | + |
| 52 | + - name: Check metadata verification |
| 53 | + # this step ensures the metadata is correct and complete |
| 54 | + # it is a good practice to run this before publishing to PyPI |
| 55 | + run: python -m twine check --strict dist/* |
| 56 | + |
| 57 | + - name: Distribution files & installation sizes |
| 58 | + # this step is useful to get some useful metrics and ensure changes do not break the size of the distribution |
| 59 | + run: | |
| 60 | + echo "Distribution files sizes" |
| 61 | + du -sh dist/* |
| 62 | + python -m pip install dist/*.whl --target /tmp/assessingsolar --quiet --quiet |
| 63 | + echo "Installation size of wheel" |
| 64 | + du -sh /tmp/assessingsolar/sunlib |
| 65 | +
|
| 66 | + - name: Upload artifact with distribution files |
| 67 | + # this step uploads the distribution files to the GitHub artifact store if they are needed later to publish to PyPI |
| 68 | + if: github.repository == 'assessingsolar/sunlib' && github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') |
| 69 | + uses: actions/upload-artifact@v4 |
| 70 | + with: |
| 71 | + name: distro-files |
| 72 | + path: dist/ |
| 73 | + if-no-files-found: error # files are required in the next job |
| 74 | + retention-days: 1 # delete the artifact after 1 day, no need to keep it for too long |
| 75 | + compression-level: 0 # no need to compress the files |
| 76 | + |
| 77 | + publish-distribution: |
| 78 | + name: Upload distribution to PyPI |
| 79 | + runs-on: ubuntu-latest |
| 80 | + needs: build-distribution # first build, then publish |
| 81 | + # only publish distribution to PyPI in tagged commits |
| 82 | + if: github.repository == 'assessingsolar/sunlib' && github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') |
| 83 | + environment: release |
| 84 | + permissions: |
| 85 | + id-token: write # this permission mandatory for trusted publishing |
| 86 | + steps: |
| 87 | + - name: Download artifact with distribution files |
| 88 | + uses: actions/download-artifact@v4 |
| 89 | + with: |
| 90 | + name: distro-files |
| 91 | + path: dist/ |
| 92 | + |
| 93 | + - name: Publish distribution to PyPI |
| 94 | + # this step publishes the distribution files to PyPI by using PyPI trusted publishers |
| 95 | + # https://docs.pypi.org/trusted-publishers/ |
| 96 | + uses: pypa/gh-action-pypi-publish@release/v1 |
0 commit comments