|
| 1 | +name: CI/CD - Python Package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - "**.py" |
| 7 | + - "**.toml" |
| 8 | + - "**.yml" |
| 9 | + pull_request: |
| 10 | + paths: |
| 11 | + - "**.py" |
| 12 | + - "**.toml" |
| 13 | + - "**.yml" |
| 14 | + release: |
| 15 | + types: |
| 16 | + - published |
| 17 | + workflow_dispatch: |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | +jobs: |
| 23 | + test: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + container: |
| 26 | + image: ghcr.io/xrobot-org/docker-image-stm32:main |
| 27 | + options: --user 0 |
| 28 | + |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Install Python Package |
| 33 | + run: | |
| 34 | + pip install . |
| 35 | +
|
| 36 | + - name: Compiler Version Check |
| 37 | + run: | |
| 38 | + arm-none-eabi-gcc --version |
| 39 | + clang --version |
| 40 | +
|
| 41 | + - name: Get Test Project |
| 42 | + uses: actions/checkout@v4 |
| 43 | + with: |
| 44 | + repository: xrobot-org/XRobot_stm32_test |
| 45 | + path: test-project |
| 46 | + |
| 47 | + - name: Run Tests |
| 48 | + run: | |
| 49 | + cd test-project && ./test.sh |
| 50 | +
|
| 51 | + build: |
| 52 | + name: Build & Test (${{ matrix.os }} - ${{ matrix.arch }}) |
| 53 | + runs-on: ${{ matrix.os }} |
| 54 | + needs: test |
| 55 | + |
| 56 | + strategy: |
| 57 | + matrix: |
| 58 | + os: [ubuntu-latest, windows-latest] |
| 59 | + arch: [x86_64, arm64] |
| 60 | + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] |
| 61 | + exclude: |
| 62 | + - os: windows-latest |
| 63 | + arch: arm64 |
| 64 | + |
| 65 | + steps: |
| 66 | + - name: Checkout Repository |
| 67 | + uses: actions/checkout@v4 |
| 68 | + |
| 69 | + - name: Set up Python |
| 70 | + uses: actions/setup-python@v5 |
| 71 | + with: |
| 72 | + python-version: ${{ matrix.python-version }} |
| 73 | + cache: "pip" |
| 74 | + |
| 75 | + - name: Install dependencies (Linux/macOS) |
| 76 | + if: runner.os != 'Windows' |
| 77 | + run: | |
| 78 | + python -m pip install --upgrade pip |
| 79 | + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi |
| 80 | + pip install pytest flake8 black build twine |
| 81 | +
|
| 82 | + - name: Install dependencies (Windows) |
| 83 | + if: runner.os == 'Windows' |
| 84 | + run: | |
| 85 | + python -m pip install --upgrade pip |
| 86 | + if (Test-Path "requirements.txt") { pip install -r requirements.txt } |
| 87 | + pip install pytest flake8 black build twine |
| 88 | +
|
| 89 | + - name: Build Package |
| 90 | + run: | |
| 91 | + python -m build |
| 92 | +
|
| 93 | + - name: Check Package Metadata |
| 94 | + run: | |
| 95 | + twine check dist/* |
| 96 | +
|
| 97 | + - name: Rename built package files (Linux/macOS) |
| 98 | + if: runner.os != 'Windows' |
| 99 | + run: | |
| 100 | + for file in dist/*.whl dist/*.tar.gz; do |
| 101 | + if [[ -f "$file" ]]; then |
| 102 | + new_name=$(echo "$file" | sed -E "s/^dist\/xrobot-([0-9]+\.[0-9]+\.[0-9]+)-py([0-9]+)-none-any\.(whl|tar.gz)$/dist\/xrobot-\1-py\2-none-any.\3/") |
| 103 | + if [[ "$file" != "$new_name" ]]; then |
| 104 | + mv "$file" "$new_name" |
| 105 | + fi |
| 106 | + fi |
| 107 | + done |
| 108 | + ls -lh dist/ |
| 109 | +
|
| 110 | + - name: Rename built package files (Windows) |
| 111 | + if: runner.os == 'Windows' |
| 112 | + shell: pwsh |
| 113 | + run: | |
| 114 | + Get-ChildItem -Path dist/* -Include *.whl, *.tar.gz | ForEach-Object { |
| 115 | + $newName = $_.Name -replace '^xrobot-([0-9]+\.[0-9]+\.[0-9]+)-py([0-9]+)-none-any\.(whl|tar.gz)', 'xrobot-$1-py$2-none-any.$3' |
| 116 | + if ($_.Name -ne $newName) { |
| 117 | + Rename-Item -Path $_.FullName -NewName $newName |
| 118 | + } |
| 119 | + } |
| 120 | + Get-ChildItem -Path dist/ |
| 121 | +
|
| 122 | + - name: Upload Built Package |
| 123 | + uses: actions/upload-artifact@v4 |
| 124 | + with: |
| 125 | + name: python-package-${{ matrix.os }}-${{ matrix.arch }}-py${{ matrix.python-version }} |
| 126 | + path: dist/* |
| 127 | + |
| 128 | + publish: |
| 129 | + name: Publish to PyPI |
| 130 | + runs-on: ubuntu-latest |
| 131 | + needs: build |
| 132 | + if: github.event_name == 'release' |
| 133 | + |
| 134 | + permissions: |
| 135 | + id-token: write |
| 136 | + |
| 137 | + environment: |
| 138 | + name: XRobot |
| 139 | + url: https://pypi.org/project/xrobot |
| 140 | + |
| 141 | + steps: |
| 142 | + - name: Download built packages |
| 143 | + uses: actions/download-artifact@v4 |
| 144 | + with: |
| 145 | + path: dist/ |
| 146 | + |
| 147 | + - name: Move built packages to root dist/ |
| 148 | + run: | |
| 149 | + find dist/ -mindepth 2 -type f -exec mv {} dist/ \; |
| 150 | + rm -rf dist/python-package-* # 清理空文件夹 |
| 151 | + ls -lh dist/ |
| 152 | +
|
| 153 | + - name: Publish to PyPI |
| 154 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 155 | + with: |
| 156 | + packages-dir: dist/ |
| 157 | + username: XRobot |
| 158 | + password: ${{ secrets.TOKEN }} |
0 commit comments