Python實現(xiàn)Linux系統(tǒng)上CI/CD工作流的方法詳解
完整實現(xiàn)代碼
#!/usr/bin/env python3 import subprocess import os import sys import time import logging import argparse import shlex from pathlib import Path logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", handlers=[logging.FileHandler("ci_cd.log"), logging.StreamHandler()] ) logger = logging.getLogger("CI/CD") class CICD: def __init__(self, repo_path, branch="main"): self.repo_path = Path(repo_path).resolve() self.branch = branch self.last_commit = None self._check_dependencies(["git", "docker"]) def _check_dependencies(self, deps): for cmd in deps: try: subprocess.run([cmd, "--version"], check=True, stdout=subprocess.DEVNULL) except Exception: raise RuntimeError(f"缺少依賴: {cmd}") def check_updates(self): try: # 檢查遠程更新 subprocess.run(["git", "-C", str(self.repo_path), "fetch"], check=True) # 比較本地與遠程差異 diff = subprocess.run( ["git", "-C", str(self.repo_path), "diff", "--shortstat", f"origin/{self.branch}"], stdout=subprocess.PIPE ) if not diff.stdout.strip(): return False # 拉取最新代碼 subprocess.run( ["git", "-C", str(self.repo_path), "pull", "origin", self.branch], check=True ) # 獲取最新提交ID new_commit = subprocess.check_output( ["git", "-C", str(self.repo_path), "rev-parse", "HEAD"] ).decode().strip() if new_commit != self.last_commit: self.last_commit = new_commit return True return False except subprocess.CalledProcessError as e: logger.error(f"代碼更新失敗: {str(e)}") raise def run_tests(self): try: subprocess.run(["pytest", "tests/"], cwd=self.repo_path, check=True) return True except subprocess.CalledProcessError: logger.error("測試失敗") return False def build(self): try: subprocess.run( ["docker", "build", "-t", "myapp:latest", "."], cwd=self.repo_path, check=True ) return True except subprocess.CalledProcessError: logger.error("構(gòu)建失敗") return False def deploy(self): try: # 停止舊容器 subprocess.run(["docker", "stop", "myapp"], check=False) # 啟動新容器 subprocess.run( ["docker", "run", "--rm", "-d", "--name", "myapp", "-p", "8000:8000", "myapp:latest"], check=True ) return True except subprocess.CalledProcessError: logger.error("部署失敗") return False def run_pipeline(self): if not self.check_updates(): logger.info("無代碼更新") return True logger.info(f"檢測到新提交: {self.last_commit[:8]}") return self.run_tests() and self.build() and self.deploy() def main(): parser = argparse.ArgumentParser(description="CI/CD 流水線") parser.add_argument("--repo", required=True, help="倉庫路徑") parser.add_argument("--branch", default="main", help="監(jiān)控分支") parser.add_argument("--daemon", action="store_true", help="守護模式") parser.add_argument("--interval", type=int, default=60, help="檢查間隔") args = parser.parse_args() try: ci = CICD(args.repo, args.branch) if args.daemon: while True: ci.run_pipeline() time.sleep(args.interval) else: success = ci.run_pipeline() sys.exit(0 if success else 1) except Exception as e: logger.error(f"流程異常: {str(e)}") sys.exit(1) if __name__ == "__main__": main()
使用說明
安裝依賴:
pip install pytest docker
運行方式:
# 單次運行模式 python ci_cd.py --repo /path/to/repo --branch main # 守護進程模式(每5分鐘檢查一次) python ci_cd.py --repo ~/myapp --daemon --interval 300
自定義命令參數(shù):
# 使用自定義測試命令 python ci_cd.py --repo ~/project \ --test-cmd "npm run test" \ --build-cmd "docker build -t myapp:v1 ." \ --deploy-cmd "kubectl apply -f deploy.yaml"
Git Hook 集成(可選): 在 .git/hooks/post-receive
中添加:
#!/bin/sh python /path/to/ci_cd.py --repo $(pwd)
擴展功能建議
通知功能:
# 添加至 CICD 類 def send_notification(self, message): import requests requests.post( "https://api.alert.com/notify", json={"text": f"[CI/CD] {message}"} ) # 在部署成功后調(diào)用 self.send_notification(f"部署成功: {self.last_commit[:8]}")
健康檢查:
def health_check(self): import requests try: resp = requests.get("http://localhost:8000/health", timeout=5) return resp.status_code == 200 except Exception: return False
配置文件支持: 創(chuàng)建 config.yaml
:
repo: ~/myapp branch: dev test_cmd: "npm test" build_cmd: "docker build -t myapp:latest ."
關(guān)鍵注意事項
1.權(quán)限管理:
確保運行用戶具有 Docker 執(zhí)行權(quán)限
建議將用戶加入 docker
用戶組:
sudo usermod -aG docker $USER
2.安全建議:
- 通過環(huán)境變量管理敏感信息(如 API 密鑰)
- 使用
--env-file
傳遞 Docker 環(huán)境變量
3.錯誤處理:
- 重要操作建議添加重試機制
- 部署失敗時可自動回滾到上一個版本
4.性能優(yōu)化:
- 使用 Docker 緩存加速構(gòu)建
- 并行執(zhí)行測試任務(wù)
到此這篇關(guān)于Python實現(xiàn)Linux系統(tǒng)上CI/CD工作流的方法詳解的文章就介紹到這了,更多相關(guān)Python實現(xiàn)CI/CD工作流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3中l(wèi)ambda表達式與函數(shù)式編程講解
今天小編就為大家分享一篇關(guān)于Python3中l(wèi)ambda表達式與函數(shù)式編程講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01python登錄WeChat 實現(xiàn)自動回復(fù)實例詳解
在本篇內(nèi)容里小編給大家整理的是關(guān)于python登錄WeChat 實現(xiàn)自動回復(fù)的相關(guān)實例內(nèi)容以及知識點總結(jié),有興趣的朋友們參考下。2019-05-05基于Python實現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲實例
這篇文章主要介紹了基于Python實現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲,實例分析了Python實現(xiàn)網(wǎng)絡(luò)爬蟲的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04