|
| 1 | +# Modify from https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/ |
| 2 | +# The tasks defined in this workflow are strictly follow the steps in README.md |
| 3 | + |
| 4 | +# workflow name |
| 5 | +name: 📤 Publish and Release 🗳 |
| 6 | + |
| 7 | +# trigger event, receive a single event or list of events, i.e. [push, pull_request] |
| 8 | +on: |
| 9 | + push: |
| 10 | + tags: |
| 11 | + - "v*.*.*" # only trigger when push a tag with pattern "v*.*.*" |
| 12 | + |
| 13 | +env: |
| 14 | + FORCE_COLOR: 1 # force color output |
| 15 | + |
| 16 | +# Declare task list |
| 17 | +jobs: |
| 18 | + Build-distribution-packages: # task 1: build |
| 19 | + name: 📦 Build distribution # task 1 description |
| 20 | + runs-on: ubuntu-latest # operating system |
| 21 | + |
| 22 | + steps: # Declare task steps |
| 23 | + - name: Fetch repository # Step 1: pull the repository to the runner |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Install Python # Step 2: install python |
| 27 | + uses: actions/setup-python@v5 |
| 28 | + with: |
| 29 | + python-version: "3.x" # Step 2 parameters: python version |
| 30 | + |
| 31 | + - name: Install packages used for building # Step 3: Install dependencies used for package building |
| 32 | + run: | # concrete commands, `|` is used to indicate a multi-line command |
| 33 | + python3 -m pip install setuptools build wheel isort ruff --user |
| 34 | + python3 -m pip install twine check-manifest --user |
| 35 | + |
| 36 | + - name: Build distribution packages # Step 4: Build Python distribution packages. |
| 37 | + run: python3 -m build -v -n . |
| 38 | + |
| 39 | + - name: Execute validation script # Step 5: validate metadate of the packages |
| 40 | + run: | |
| 41 | + chmod +x ./check_meta.sh |
| 42 | + bash ./check_meta.sh |
| 43 | +
|
| 44 | + - name: Check MANIFEST.in # Step 6: update MANIFEST.in |
| 45 | + run: | |
| 46 | + check-manifest -u -v |
| 47 | +
|
| 48 | + - name: Store the building results # Step 7: Upload building results to the Github server, in order to expose to other job. |
| 49 | + uses: actions/upload-artifact@v4 |
| 50 | + with: |
| 51 | + name: build-results |
| 52 | + path: dist/ |
| 53 | + if-no-files-found: error |
| 54 | + |
| 55 | + Publish-PyPI: # task 2: publish to PyPI |
| 56 | + name: 📤 Publish to PyPI |
| 57 | + runs-on: ubuntu-latest |
| 58 | + |
| 59 | + needs: Build-distribution-packages # wait for task 1 to finish |
| 60 | + |
| 61 | + environment: |
| 62 | + name: pypi |
| 63 | + url: https://pypi.org/p/<package-name> # Replace <package-name> with your PyPI project name |
| 64 | + |
| 65 | + permissions: |
| 66 | + id-token: write # IMPORTANT: mandatory for trusted publishing |
| 67 | + |
| 68 | + steps: |
| 69 | + - name: Download all the dists |
| 70 | + uses: actions/download-artifact@v4 |
| 71 | + with: |
| 72 | + name: build-results # should be the same as the name in task 1 |
| 73 | + path: dist/ |
| 74 | + |
| 75 | + - name: Upload to PyPI |
| 76 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 77 | + |
| 78 | + Publish-TestPyPI: # task 3: publish to TestPyPI |
| 79 | + name: 📤 Publish to TestPyPI |
| 80 | + runs-on: ubuntu-latest |
| 81 | + |
| 82 | + needs: Build-distribution-packages # wait for task 1 to finish |
| 83 | + |
| 84 | + environment: |
| 85 | + name: testpypi |
| 86 | + url: https://test.pypi.org/p/<package-name> # Replace <package-name> with your PyPI project name |
| 87 | + |
| 88 | + permissions: |
| 89 | + id-token: write # IMPORTANT: mandatory for trusted publishing |
| 90 | + |
| 91 | + steps: |
| 92 | + - name: Download all the dists |
| 93 | + uses: actions/download-artifact@v4 |
| 94 | + with: |
| 95 | + name: build-results # should be the same as the name in task 1 |
| 96 | + path: dist/ |
| 97 | + |
| 98 | + - name: Upload to TestPyPI |
| 99 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 100 | + with: |
| 101 | + repository-url: https://test.pypi.org/legacy/ |
| 102 | + |
| 103 | + Github-Release: # task 4: create a Github Release |
| 104 | + name: 🗳 Create a Release |
| 105 | + runs-on: ubuntu-latest |
| 106 | + |
| 107 | + needs: Build-distribution-packages # wait for task 3 to finish |
| 108 | + |
| 109 | + permissions: |
| 110 | + contents: write # IMPORTANT: mandatory for making GitHub Releases |
| 111 | + |
| 112 | + steps: |
| 113 | + - name: Fetch repository |
| 114 | + uses: actions/checkout@v4 |
| 115 | + |
| 116 | + - name: Generate Changelog # this step is to get the first entry divided by '---' in CHANGELOG.md |
| 117 | + run: | |
| 118 | + awk -v RS="---" 'NR==1{gsub(/\n+$/, ""); print}' CHANGELOG.md > ${{ github.workspace }}-RELEASE.txt |
| 119 | + cat ${{ github.workspace }}-RELEASE.txt |
| 120 | + |
| 121 | + - name: Download all the dists |
| 122 | + uses: actions/download-artifact@v4 |
| 123 | + with: |
| 124 | + name: build-results |
| 125 | + path: dist/ |
| 126 | + |
| 127 | + - name: Create GitHub Release |
| 128 | + env: |
| 129 | + GITHUB_TOKEN: ${{ github.token }} |
| 130 | + run: >- # multi-line command |
| 131 | + gh release create |
| 132 | + '${{ github.ref_name }}' |
| 133 | + --repo '${{ github.repository }}' |
| 134 | + --notes "$(cat ${{ github.workspace }}-RELEASE.txt)" |
| 135 | +
|
| 136 | + - name: Upload artifact to GitHub Release |
| 137 | + env: |
| 138 | + GITHUB_TOKEN: ${{ github.token }} |
| 139 | + # Upload to GitHub Release using the `gh` CLI. |
| 140 | + # `dist/` contains the built packages |
| 141 | + run: >- |
| 142 | + gh release upload |
| 143 | + '${{ github.ref_name }}' dist/** |
| 144 | + --repo '${{ github.repository }}' |
| 145 | + |
0 commit comments