|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + # 仅在 push 时触发测试 |
| 5 | + push: |
| 6 | + |
| 7 | +jobs: |
| 8 | + test: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + strategy: |
| 11 | + matrix: |
| 12 | + python-version: ['3.10'] |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 # 获取完整历史用于增量覆盖率检查 |
| 19 | + |
| 20 | + - name: Set up Python ${{ matrix.python-version }} |
| 21 | + uses: actions/setup-python@v5 |
| 22 | + with: |
| 23 | + python-version: ${{ matrix.python-version }} |
| 24 | + |
| 25 | + - name: Install dependencies |
| 26 | + run: | |
| 27 | + make setup PYTHON_VERSION=${{ matrix.python-version }} |
| 28 | +
|
| 29 | + - name: Run type check (mypy) |
| 30 | + run: | |
| 31 | + make mypy-check |
| 32 | +
|
| 33 | + - name: Run unit tests |
| 34 | + run: | |
| 35 | + make test-unit |
| 36 | +
|
| 37 | + - name: Run coverage |
| 38 | + run: | |
| 39 | + make coverage |
| 40 | +
|
| 41 | + # 检测文件更改并决定是否构建测试包 |
| 42 | + - name: Check for changes in agentrun directory |
| 43 | + id: changes |
| 44 | + run: | |
| 45 | + echo "Checking if agentrun directory has changes..." |
| 46 | + # 获取最近两次提交之间的差异;如果没有父提交,则将所有跟踪文件视为已更改 |
| 47 | + if git rev-parse HEAD^ >/dev/null 2>&1; then |
| 48 | + git diff --name-only HEAD^ HEAD > changed_files.txt |
| 49 | + else |
| 50 | + echo "No parent commit; treating all tracked files as changed." |
| 51 | + git ls-files > changed_files.txt |
| 52 | + fi |
| 53 | + echo "Changed files:" |
| 54 | + cat changed_files.txt || echo "No changed files detected" |
| 55 | + |
| 56 | + # 检查是否有任何以 agentrun/ 开头的文件 |
| 57 | + if grep -q "^agentrun/" changed_files.txt 2>/dev/null; then |
| 58 | + echo "agentrun directory has changes" |
| 59 | + echo "agentrun_changed=true" >> $GITHUB_OUTPUT |
| 60 | + else |
| 61 | + echo "agentrun directory has no changes" |
| 62 | + echo "agentrun_changed=false" >> $GITHUB_OUTPUT |
| 63 | + fi |
| 64 | +
|
| 65 | + # 测试通过后自动构建测试包(仅在 agentrun 目录有变化时触发) |
| 66 | + - name: Get latest version from PyPI and calculate next version |
| 67 | + id: version |
| 68 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 69 | + run: | |
| 70 | + # 从 PyPI 获取 agentrun-inner-test 的最新版本 |
| 71 | + PYPI_RESPONSE=$(curl -s https://pypi.org/pypi/agentrun-inner-test/json 2>/dev/null || echo "") |
| 72 | + |
| 73 | + if [ -z "$PYPI_RESPONSE" ] || echo "$PYPI_RESPONSE" | grep -q "Not Found"; then |
| 74 | + # 如果包不存在,从 0.0.0 开始 |
| 75 | + CURRENT_VERSION="0.0.0" |
| 76 | + echo "Package not found on PyPI, starting from 0.0.0" |
| 77 | + else |
| 78 | + # 从 PyPI 响应中提取最新版本 |
| 79 | + CURRENT_VERSION=$(echo "$PYPI_RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin)['info']['version'])") |
| 80 | + echo "Latest version on PyPI: $CURRENT_VERSION" |
| 81 | + fi |
| 82 | + |
| 83 | + # 解析版本号 |
| 84 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" |
| 85 | + |
| 86 | + # 默认为 patch |
| 87 | + BUMP_TYPE="patch" |
| 88 | + case "$BUMP_TYPE" in |
| 89 | + major) |
| 90 | + MAJOR=$((MAJOR + 1)) |
| 91 | + MINOR=0 |
| 92 | + PATCH=0 |
| 93 | + ;; |
| 94 | + minor) |
| 95 | + MINOR=$((MINOR + 1)) |
| 96 | + PATCH=0 |
| 97 | + ;; |
| 98 | + patch) |
| 99 | + PATCH=$((PATCH + 1)) |
| 100 | + ;; |
| 101 | + esac |
| 102 | + |
| 103 | + NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}" |
| 104 | + NEW_TAG="agentrun-inner-test-v${NEW_VERSION}" |
| 105 | + |
| 106 | + echo "VERSION=${NEW_VERSION}" >> $GITHUB_OUTPUT |
| 107 | + echo "TAG=${NEW_TAG}" >> $GITHUB_OUTPUT |
| 108 | + echo "New version: ${NEW_VERSION}" |
| 109 | + echo "New tag: ${NEW_TAG}" |
| 110 | +
|
| 111 | + - name: Update package name and version in pyproject.toml |
| 112 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 113 | + run: | |
| 114 | + VERSION="${{ steps.version.outputs.VERSION }}" |
| 115 | + # 修改包名为 agentrun-inner-test |
| 116 | + sed -i 's/name = "agentrun-sdk"/name = "agentrun-inner-test"/' pyproject.toml |
| 117 | + # 修改版本号 |
| 118 | + sed -i 's/version = "[^"]*"/version = "'${VERSION}'"/' pyproject.toml |
| 119 | + echo "Updated pyproject.toml:" |
| 120 | + head -10 pyproject.toml |
| 121 | +
|
| 122 | + - name: Update __version__ in __init__.py |
| 123 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 124 | + run: | |
| 125 | + VERSION="${{ steps.version.outputs.VERSION }}" |
| 126 | + if grep -q "__version__" agentrun/__init__.py; then |
| 127 | + sed -i 's/__version__ = "[^"]*"/__version__ = "'${VERSION}'"/' agentrun/__init__.py |
| 128 | + else |
| 129 | + sed -i '1a __version__ = "'${VERSION}'"' agentrun/__init__.py |
| 130 | + fi |
| 131 | + echo "Updated __init__.py version to ${VERSION}" |
| 132 | + grep "__version__" agentrun/__init__.py |
| 133 | +
|
| 134 | + - name: Build package |
| 135 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 136 | + run: | |
| 137 | + python -m pip install --upgrade pip |
| 138 | + pip install build twine |
| 139 | + python -m build |
| 140 | + echo "Package built successfully" |
| 141 | + ls -la dist/ |
| 142 | +
|
| 143 | + - name: Verify package |
| 144 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 145 | + run: | |
| 146 | + python -m twine check dist/* |
| 147 | + echo "Package verification completed" |
| 148 | +
|
| 149 | + - name: Publish to PyPI |
| 150 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 151 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 152 | + with: |
| 153 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 154 | + verify-metadata: false |
| 155 | + |
| 156 | + - name: Create and push tag |
| 157 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 158 | + run: | |
| 159 | + TAG="${{ steps.version.outputs.TAG }}" |
| 160 | + git config --local user.email "[email protected]" |
| 161 | + git config --local user.name "GitHub Action" |
| 162 | + git tag -a "$TAG" -m "Release test package version ${{ steps.version.outputs.VERSION }}" |
| 163 | + git push origin "$TAG" |
| 164 | + echo "Created and pushed tag: $TAG" |
| 165 | +
|
| 166 | + - name: Summary |
| 167 | + if: steps.changes.outputs.agentrun_changed == 'true' |
| 168 | + run: | |
| 169 | + echo "## 🎉 Test Package Released!" >> $GITHUB_STEP_SUMMARY |
| 170 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 171 | + echo "- **Package Name:** agentrun-inner-test" >> $GITHUB_STEP_SUMMARY |
| 172 | + echo "- **Version:** ${{ steps.version.outputs.VERSION }}" >> $GITHUB_STEP_SUMMARY |
| 173 | + echo "- **Tag:** ${{ steps.version.outputs.TAG }}" >> $GITHUB_STEP_SUMMARY |
| 174 | + echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY |
| 175 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 176 | + echo "Install with: \`pip install agentrun-inner-test==${{ steps.version.outputs.VERSION }}\`" >> $GITHUB_STEP_SUMMARY |
| 177 | +
|
0 commit comments