|
| 1 | +name: python |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: main |
| 6 | + paths: |
| 7 | + - 'python/**' |
| 8 | + - '.github/workflows/python.yml' |
| 9 | + pull_request: |
| 10 | + paths: |
| 11 | + - 'python/**' |
| 12 | + - '.github/workflows/python.yml' |
| 13 | + |
| 14 | +env: |
| 15 | + PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} |
| 16 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 17 | + |
| 18 | +defaults: |
| 19 | + run: |
| 20 | + working-directory: python |
| 21 | + |
| 22 | +jobs: |
| 23 | + findChangedPythonFiles: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + outputs: |
| 26 | + isPythonFilesChanged: ${{ steps.setIsPythonFilesChangedOutput.outputs.isPythonFilesChanged }} |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v3 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + - name: Get changed files |
| 32 | + id: changed-files |
| 33 | + uses: tj-actions/changed-files@v46 |
| 34 | + - name: Set output isPythonFilesChanged |
| 35 | + id: setIsPythonFilesChangedOutput |
| 36 | + run: | |
| 37 | + isPythonFilesChanged='false' |
| 38 | + echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}" |
| 39 | + for changedFile in ${{ steps.changed-files.outputs.all_changed_files }}; do |
| 40 | + if [[ $changedFile == python/*.py ]] || [[ $changedFile == python/pyproject.toml ]] || [[ $changedFile == python/setup.py ]] || [[ $changedFile == python/* ]] || [[ $changedFile == .github/workflows/python.yml ]]; then |
| 41 | + echo "isPythonFilesChanged='true'" |
| 42 | + isPythonFilesChanged='true' |
| 43 | + break |
| 44 | + fi |
| 45 | + done |
| 46 | + echo "isPythonFilesChanged=${isPythonFilesChanged}" >> $GITHUB_OUTPUT |
| 47 | + echo "isPythonFilesChanged: ${isPythonFilesChanged}" |
| 48 | +
|
| 49 | + test: |
| 50 | + needs: [findChangedPythonFiles] |
| 51 | + if: ${{ needs.findChangedPythonFiles.outputs.isPythonFilesChanged == 'true' }} |
| 52 | + runs-on: ubuntu-latest |
| 53 | + strategy: |
| 54 | + matrix: |
| 55 | + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@v3 |
| 58 | + with: |
| 59 | + submodules: true |
| 60 | + - name: Set up Python ${{ matrix.python-version }} |
| 61 | + uses: actions/setup-python@v4 |
| 62 | + with: |
| 63 | + python-version: ${{ matrix.python-version }} |
| 64 | + - name: Install dependencies |
| 65 | + run: | |
| 66 | + python -m pip install --upgrade pip |
| 67 | + pip install pytest build twine |
| 68 | + - name: Build package |
| 69 | + run: python -m build |
| 70 | + - name: Run tests |
| 71 | + run: pytest -v |
| 72 | + |
| 73 | + publishToPyPI: |
| 74 | + needs: [test, findChangedPythonFiles] |
| 75 | + if: ${{ needs.findChangedPythonFiles.outputs.isPythonFilesChanged == 'true' && github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 76 | + runs-on: ubuntu-latest |
| 77 | + steps: |
| 78 | + - uses: actions/checkout@v3 |
| 79 | + with: |
| 80 | + submodules: true |
| 81 | + - name: Set up Python |
| 82 | + uses: actions/setup-python@v4 |
| 83 | + with: |
| 84 | + python-version: '3.11' |
| 85 | + - name: Install dependencies |
| 86 | + run: | |
| 87 | + python -m pip install --upgrade pip |
| 88 | + pip install build twine |
| 89 | + - name: Build package |
| 90 | + run: python -m build |
| 91 | + - name: Check if version already published |
| 92 | + id: version-check |
| 93 | + run: | |
| 94 | + PACKAGE_VERSION=$(python -c "import tomli; f = open('pyproject.toml', 'rb'); data = tomli.load(f); print(data['project']['version'])" 2>/dev/null || grep "^version" pyproject.toml | sed 's/version = "\(.*\)"/\1/') |
| 95 | + PACKAGE_NAME=$(python -c "import tomli; f = open('pyproject.toml', 'rb'); data = tomli.load(f); print(data['project']['name'])" 2>/dev/null || grep "^name" pyproject.toml | sed 's/name = "\(.*\)"/\1/') |
| 96 | + echo "Package: $PACKAGE_NAME@$PACKAGE_VERSION" |
| 97 | +
|
| 98 | + # Check if version exists on PyPI |
| 99 | + if pip index versions $PACKAGE_NAME 2>/dev/null | grep -q "$PACKAGE_VERSION"; then |
| 100 | + echo "Version $PACKAGE_VERSION already exists on PyPI" |
| 101 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 102 | + else |
| 103 | + echo "Version $PACKAGE_VERSION does not exist on PyPI" |
| 104 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 105 | + fi |
| 106 | + - name: Publish to PyPI |
| 107 | + if: steps.version-check.outputs.should_publish == 'true' |
| 108 | + env: |
| 109 | + TWINE_USERNAME: __token__ |
| 110 | + TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} |
| 111 | + run: | |
| 112 | + twine upload dist/* |
| 113 | +
|
| 114 | + publishRelease: |
| 115 | + runs-on: ubuntu-latest |
| 116 | + needs: [publishToPyPI] |
| 117 | + if: ${{ needs.findChangedPythonFiles.outputs.isPythonFilesChanged == 'true' && needs.publishToPyPI.result == 'success' && github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 118 | + steps: |
| 119 | + - uses: actions/checkout@v3 |
| 120 | + with: |
| 121 | + submodules: true |
| 122 | + - name: Set up Python |
| 123 | + uses: actions/setup-python@v4 |
| 124 | + with: |
| 125 | + python-version: '3.11' |
| 126 | + - name: Check if GitHub release already exists |
| 127 | + id: release-check |
| 128 | + run: | |
| 129 | + PACKAGE_VERSION=$(grep "^version" pyproject.toml | sed 's/version = "\(.*\)"/\1/') |
| 130 | + TAG_NAME="python_$PACKAGE_VERSION" |
| 131 | + echo "Checking if release $TAG_NAME already exists" |
| 132 | +
|
| 133 | + # Check if release exists |
| 134 | + if gh release view "$TAG_NAME" >/dev/null 2>&1; then |
| 135 | + echo "Release $TAG_NAME already exists" |
| 136 | + echo "should_create_release=false" >> $GITHUB_OUTPUT |
| 137 | + else |
| 138 | + echo "Release $TAG_NAME does not exist" |
| 139 | + echo "should_create_release=true" >> $GITHUB_OUTPUT |
| 140 | + fi |
| 141 | + env: |
| 142 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 143 | + - name: Create GitHub release |
| 144 | + if: steps.release-check.outputs.should_create_release == 'true' |
| 145 | + run: | |
| 146 | + PACKAGE_VERSION=$(grep "^version" pyproject.toml | sed 's/version = "\(.*\)"/\1/') |
| 147 | + PACKAGE_NAME=$(grep "^name" pyproject.toml | sed 's/name = "\(.*\)"/\1/') |
| 148 | +
|
| 149 | + # Create release |
| 150 | + gh release create "${PACKAGE_VERSION}_python" \ |
| 151 | + --title "[Python] $PACKAGE_VERSION" \ |
| 152 | + --notes "https://pypi.org/project/$PACKAGE_NAME/" |
| 153 | + env: |
| 154 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments