Upload to PyPI and Docker #3
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Upload to PyPI and Docker | |
| on: | |
| release: | |
| types: [created] | |
| workflow_dispatch: | |
| jobs: | |
| upload-to-pypi: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - uses: actions/setup-python@v2 | |
| with: | |
| python-version: 3.11 # 建议用稳定版,3.14为预览版 | |
| - name: Install dependencies | |
| run: | | |
| python3 -m pip install --upgrade pip | |
| python3 -m pip install setuptools wheel twine | |
| - name: Build and upload to PyPI | |
| run: | | |
| python3 setup.py sdist bdist_wheel | |
| python3 -m twine upload dist/* | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.TWINE_TOKEN }} | |
| upload-to-docker: | |
| needs: upload-to-pypi | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v3 | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v4 | |
| with: | |
| images: | | |
| ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.APP_NAME }} | |
| tags: | | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=semver,pattern={{major}} | |
| type=sha | |
| type=raw,value=latest,enable=${{ github.event_name == 'release' }} | |
| type=raw,value=latest,enable=${{ github.event_name == 'workflow_dispatch' }} | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v2 # 必须:提供跨架构模拟支持 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| id: buildx | |
| with: | |
| install: true # 确保安装最新版 Buildx | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push multi-arch image # 核心修改:多架构构建 | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| push: true # 直接推送(已排除PR场景,无需判断) | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| platforms: linux/amd64,linux/arm64 # 关键:指定amd64和arm64架构 | |
| builder: ${{ steps.buildx.outputs.name }} # 使用Buildx构建器 | |
| cache-from: type=gha # 缓存加速构建 | |
| cache-to: type=gha,mode=max |