十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Python项目CI/CD实践:从零搭建自动化流水线

Python项目CI/CD实践:从零搭建自动化流水线 1. Python项目CI/CD实践指南在Python开发领域持续集成和持续部署(CI/CD)已经成为现代软件工程的标配。我经历过从手动打包部署到全自动化流水线的完整演进过程实测这套体系能让团队效率提升300%以上。本文将分享如何为零基础的Python项目搭建完整的CI/CD工作流涵盖工具选型、配置细节和实战避坑指南。2. 基础环境搭建2.1 版本控制策略Git是目前CI/CD的基础设施推荐采用Git Flow分支模型main分支生产环境代码develop分支集成测试环境代码feature/*分支功能开发分支重要提示务必配置.gitignore文件排除__pycache__/等Python临时文件2.2 虚拟环境配置建议使用poetry管理项目依赖# 初始化项目 poetry init # 添加依赖 poetry add flask pytest # 生成requirements.txt poetry export -f requirements.txt --output requirements.txt3. CI流水线构建3.1 单元测试自动化在项目根目录添加.pytest.ini配置文件[pytest] testpaths tests python_files test_*.py addopts -v --covsrc --cov-reportxmlGitHub Actions配置示例(.github/workflows/test.yml)name: Python CI on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkoutv3 - name: Set up Python uses: actions/setup-pythonv4 with: python-version: 3.10 - name: Install dependencies run: | pip install poetry poetry install - name: Run tests run: poetry run pytest - name: Upload coverage uses: codecov/codecov-actionv33.2 代码质量检查推荐组合工具flake8基础语法检查black代码格式化mypy静态类型检查GitHub Actions追加步骤- name: Lint with flake8 run: | pip install flake8 black mypy flake8 src --count --show-source --statistics black --check src mypy src4. CD部署流水线4.1 打包与发布使用twine打包并发布到PyPI- name: Build and publish if: github.event_name release github.event.action published run: | pip install twine python setup.py sdist bdist_wheel twine upload dist/* env: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}4.2 容器化部署Dockerfile示例FROM python:3.10-slim WORKDIR /app COPY pyproject.toml poetry.lock ./ RUN pip install poetry \ poetry config virtualenvs.create false \ poetry install --no-dev COPY . . CMD [python, src/main.py]GitHub Actions容器构建- name: Build and push Docker image uses: docker/build-push-actionv4 with: push: true tags: | ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:latest ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:${{ github.sha }} secrets: | username${{ secrets.DOCKER_HUB_USERNAME }} password${{ secrets.DOCKER_HUB_TOKEN }}5. 高级优化技巧5.1 缓存加速优化后的poetry安装步骤- name: Cache Poetry virtualenv uses: actions/cachev3 id: cache with: path: | ~/.cache/pypoetry/virtualenvs ~/.cache/pip key: ${{ runner.os }}-python-${{ hashFiles(poetry.lock) }} - name: Install dependencies run: | poetry config virtualenvs.in-project true poetry install --no-root5.2 矩阵测试多版本Python兼容性测试strategy: matrix: python-version: [3.8, 3.9, 3.10]6. 常见问题排查6.1 依赖冲突典型症状CI环境与本地环境测试结果不一致 解决方案删除poetry.lock后重新生成使用poetry add packageversion精确指定版本检查setup.py中install_requires是否与pyproject.toml一致6.2 环境变量管理安全实践永远不要将敏感信息硬编码在代码中使用GitHub Secrets管理凭据测试环境使用.env.sample模板# config.py示例 import os from dotenv import load_dotenv load_dotenv() DB_URL os.getenv(DB_URL, sqlite:///local.db)7. 监控与反馈7.1 测试覆盖率报告在README.md添加徽章[![codecov](https://codecov.io/gh/yourname/yourrepo/branch/main/graph/badge.svg)](https://codecov.io/gh/yourname/yourrepo)7.2 构建状态通知Slack通知配置示例- name: Slack Notification uses: rtCamp/action-slack-notifyv2 if: always() env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} SLACK_COLOR: ${{ job.status success good || danger }} SLACK_TITLE: Build ${{ job.status }} SLACK_MESSAGE: ${{ github.workflow }} triggered by ${{ github.actor }}
返回列表