欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)Linux系統(tǒng)上CI/CD工作流的方法詳解

 更新時間:2025年04月16日 09:03:19   作者:ak啊  
在現(xiàn)代軟件開發(fā)中,持續(xù)集成(CI)和持續(xù)部署(CD)是提高開發(fā)效率、保證代碼質(zhì)量的重要手段,下面我們來看看如何使用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ù)式編程講解

    Python3中l(wèi)ambda表達式與函數(shù)式編程講解

    今天小編就為大家分享一篇關(guān)于Python3中l(wèi)ambda表達式與函數(shù)式編程講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • python登錄WeChat 實現(xiàn)自動回復(fù)實例詳解

    python登錄WeChat 實現(xiàn)自動回復(fù)實例詳解

    在本篇內(nèi)容里小編給大家整理的是關(guān)于python登錄WeChat 實現(xiàn)自動回復(fù)的相關(guān)實例內(nèi)容以及知識點總結(jié),有興趣的朋友們參考下。
    2019-05-05
  • python自動化測試工具Helium使用示例

    python自動化測試工具Helium使用示例

    大家好,本篇文章主要講的是python自動化測試工具Helium使用示例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下哦
    2021-12-12
  • python 求定積分和不定積分示例

    python 求定積分和不定積分示例

    今天小編就為大家分享一篇python 求定積分和不定積分示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 如何在Django中使用聚合的實現(xiàn)示例

    如何在Django中使用聚合的實現(xiàn)示例

    這篇文章主要介紹了如何在Django中使用聚合的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 使用Python破解RAR文件密碼的代碼實例

    使用Python破解RAR文件密碼的代碼實例

    這篇文章主要介紹了使用Python破解RAR文件密碼的代碼實例,rar 壓縮文件資源又不少是被加密的,密碼通常也比較簡單,我們可以通過暴力破解的方式來獲取,通常耗時也比較小,需要的朋友可以參考下
    2023-11-11
  • 基于Python實現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲實例

    基于Python實現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲實例

    這篇文章主要介紹了基于Python實現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲,實例分析了Python實現(xiàn)網(wǎng)絡(luò)爬蟲的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • python里 super類的工作原理詳解

    python里 super類的工作原理詳解

    這篇文章主要介紹了python里 super類的工作原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Python?Opencv實現(xiàn)圖片切割處理

    Python?Opencv實現(xiàn)圖片切割處理

    這篇文章主要為大家詳細介紹了Python?Opencv實現(xiàn)圖片切割處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python+Pygame制作"長沙版"大富翁

    Python+Pygame制作"長沙版"大富翁

    說到童年愛玩的電腦游戲,最國民的莫過于金山打字通,接著是掃雷、紅心大戰(zhàn),而紅極一時的單機游戲當屬《大富翁》。本文將通過Python的Pygame模塊制作"長沙版"的大富翁,需要的可以參考一下
    2022-02-02

最新評論