首页主机资讯CentOS Python持续集成怎么做

CentOS Python持续集成怎么做

时间2025-11-24 12:16:04发布访客分类主机资讯浏览1155
导读:CentOS 上搭建 Python 持续集成 一 方案总览与选型 在 CentOS 上做 Python CI/CD,常见做法有:自建 Jenkins(灵活、可深度定制)、使用 GitLab CI/CD(与 GitLab 无缝集成)、或采用...

CentOS 上搭建 Python 持续集成

一 方案总览与选型

  • CentOS 上做 Python CI/CD,常见做法有:自建 Jenkins(灵活、可深度定制)、使用 GitLab CI/CD(与 GitLab 无缝集成)、或采用 GitHub Actions(托管在 GitHub,Runner 多为 Ubuntu,若需 CentOS 可用自托管 Runner)。核心流程都包含:拉取代码 → 准备环境 → 安装依赖 → 运行测试 → 质量门禁 → 打包与发布。Jenkins 在 CentOS 上部署与插件生态成熟,适合企业内网与复杂流水线;GitLab CI 与 GitHub Actions 以 YAML 声明式配置为主,上手快。

二 方案一 Jenkins 在 CentOS 自建

  • 准备环境
    • 安装 Java 8/11(Jenkins 运行依赖),安装 GitPython 3(建议使用 venv 隔离依赖)。
    • 示例:sudo yum install -y java-1.8.0-openjdk git;python3 -m venv .venv & & source .venv/bin/activate
  • 安装与启动 Jenkins
    • 推荐 RPM 安装:sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
    • 导入 GPG:sudo rpm --import https://pkg.jenkins.io/redhat/jenkins-ci.org.key
    • 安装与启动:sudo yum install -y jenkins;sudo systemctl start jenkins;访问 http://< 服务器IP> :8080
  • 初始化与插件
    • 解锁后安装建议插件:Git pluginPipelinePython PluginWarnings Next GenerationAllure(如需可视化报告)
  • 创建任务与流水线
    • 新建 Pipeline,SCM 选择 Git,分支按需设置;推荐将流程写入仓库根目录的 Jenkinsfile
  • 触发与 Webhook
    • 可选 Poll SCM 轮询;更推荐在 GitLab/Gitee 配置 Webhook 推送事件到 Jenkins(需在 Jenkins 获取项目 Webhook URLSecret
  • 质量与报告
    • 构建后步骤可添加:JUnit/TestNG 报告、Warnings(如 pylint)、Allure Report(需 Allure 插件与命令行工具)
  • 部署示例
    • 打包:python setup.py sdist bdist_wheel
    • 发布:通过 scp/rsync 上传产物并在目标机执行 pip install;或结合 SSH 做滚动升级与回滚脚本

三 方案二 GitLab CI 或 GitHub Actions

  • GitLab CI(仓库内 .gitlab-ci.yml)
    • 示例 stages:build → test → deploy;使用 python:3.x 镜像或自定义 CentOS 镜像;测试阶段运行 pytest,质量门禁可集成 pylint/flake8;部署阶段用 scp/rsync/SSH 发布到目标机
  • GitHub Actions(.github/workflows/ci.yml)
    • 使用托管 ubuntu-latest Runner 执行 Python 流程;如需 CentOS 环境,选择 self-hosted runner 并注册到仓库;流程与 GitLab CI 类似:checkout → setup python → pip install -r requirements.txt → pytest → 条件部署
  • 适用场景
    • 代码托管在 GitLab/GitHub 且希望快速落地时优先;如需在 CentOS 上运行 UI/系统测试或访问内网资源,选择自托管 Runner 更稳妥

四 关键实践与常见问题

  • 环境与依赖
    • 始终在 虚拟环境 中运行(python -m venv .venv),并在 CI 中缓存依赖(如 pip cache);区分系统 Python 与项目 Python,避免污染
  • 测试与质量
    • 使用 pytest 组织用例,结合 pytest-cov 产出覆盖率;静态检查用 pylint/flake8,阈值纳入门禁;UI 自动化可用 Selenium/Playwright,报告推荐 Allure
  • 触发策略
    • 内网/自建平台优先 Webhook 实时触发;公网托管平台可用 push/PR 事件;必要时保留 定时轮询 兜底
  • 报告与可视化
    • 在 Jenkins/GitLab CI 中集成 JUnit XMLWarningsAllure 报告,便于趋势分析与质量看板
  • 安全与网络
    • SSH 密钥妥善管理(Jenkins Credentials/Secrets);Webhook 需保证 GitLab ↔ Jenkins 双向网络可达;最小权限原则配置 Runner 与部署账号
  • 资源与性能
    • Jenkins 常驻内存占用较高,建议服务器内存至少 1GB;合理设置并发构建与测试分片(如 pytest-xdist)提升速度

五 最小可用示例

  • Jenkinsfile(声明式流水线)
    • 说明:拉取代码 → 准备 Python → 安装依赖 → 测试与质量 → 打包 → 仅在 main 分支部署
    • 示例:
      pipeline {
      
        agent any
        environment {
      
          PYTHON = 'python3'
          VENV  = '.venv'
        }
      
        stages {
      
          stage('Checkout') {
      
            steps {
       git branch: 'main', url: 'git@your-gitlab.com:group/project.git' }
      
          }
      
          stage('Setup') {
      
            steps {
      
              sh '''
                ${
      PYTHON}
       -m venv ${
      VENV}
      
                . ${
      VENV}
      /bin/activate
                pip install --upgrade pip
                pip install -r requirements.txt
              '''
            }
      
          }
          
          stage('Test &
       Quality') {
      
            steps {
      
              sh '''
                . ${
      VENV}
      /bin/activate
                pytest --junitxml=reports/pytest.xml --maxfail=1
                pylint --output-format=parseable src/ || true
              '''
            }
      
            post {
      
              always {
      
                junit 'reports/pytest.xml'
                warnings canComputeNew: false, defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '**/pylint*.xml', messagesPattern: '', parserConfigurations: [[parserName: 'PyLint', pattern: '**/pylint*.xml']], unHealthy: ''
              }
      
            }
      
          }
      
          stage('Package') {
      
            steps {
      
              sh '''
                . ${
      VENV}
      /bin/activate
                python setup.py sdist bdist_wheel
              '''
            }
      
          }
      
          stage('Deploy') {
      
            when {
       branch 'main' }
      
            steps {
      
              sh '''
                . ${
      VENV}
          /bin/activate
                pip install scp
                python - <
          <
          'PY'
      

import os, scp, paramiko src=os.path.expanduser(“dist/.whl") dst=“/opt/pkgs/” key=paramiko.RSAKey.from_private_key_file(“/var/lib/jenkins/.ssh/id_rsa”) with paramiko.SSHClient() as c: c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(“prod.example.com”, username=“deploy”, pkey=key) with scp.SCPClient(c.get_transport()) as s: s.put(src, dst) PY ssh deploy@prod.example.com "pip install /opt/pkgs/.whl & & systemctl restart myapp” ‘’’ } } } } ```

  • .gitlab-ci.yml(GitLab CI 示例)
    • 说明:使用官方 python:3.11 镜像;测试后仅在 main 分支部署
    • 示例:
      stages:
        - build
        - test
        - deploy
      
      variables:
        PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
      
      cache:
        paths:
          - .cache/pip
          - .venv
      
      build:
        stage: build
        image: python:3.11
        script:
          - python -m venv .venv
          - . .venv/bin/activate
          - pip install --upgrade pip
          - pip install -r requirements.txt
          - python setup.py sdist bdist_wheel
      
      test:
        stage: test
        image: python:3.11
        script:
          - . .venv/bin/activate
          - pytest --junitxml=reports/pytest.xml --maxfail=1
          - pylint --output-format=parseable src/ || true
        artifacts:
          when: always
          reports:
            junit: reports/pytest.xml
      
      deploy:
        stage: deploy
        image: python:3.11
        script:
          - . .venv/bin/activate
          - pip install scp
          - python - <
          <
          'PY'
      

import os, scp, paramiko src=os.path.expanduser(“dist/.whl") dst=“/opt/pkgs/” key=paramiko.RSAKey.from_private_key_file(“/builds/.ssh/id_rsa”) with paramiko.SSHClient() as c: c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(“prod.example.com”, username=“deploy”, pkey=key) with scp.SCPClient(c.get_transport()) as s: s.put(src, dst) PY - ssh deploy@prod.example.com "pip install /opt/pkgs/.whl & & systemctl restart myapp” only: - main ```

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: CentOS Python持续集成怎么做
本文地址: https://pptw.com/jishu/754327.html
CentOS Python代码如何部署 CentOS Python安装后如何配置pip

游客 回复需填写必要信息