通过Python脚本+Jekins实现项目重启

文章目录

一、需求

微服务项目,服务很多,重启服务,基本都是通过Jekins进行发布,过程差不多如下:

1、登录后台,选择环境和服务,如下图:

2、点击"Build_with_Parameters",弹出窗口,选择分支,然后点击"开始构建",如下图:

通过上面的操作,就会进入构建队列排队,如果系统没有正在构建的,很快就可以在构建执行状态中看到,受到Maven项目配置的执行者数量的限制,如下图,公司限制的是同一时间只能有3个在构建中

所以,当需要重启很多个服务时,需要每个都去重复上面的步骤,选环境-》选服务-》Build_with_Parameters-》选分支-》开始构建,贼麻烦,很影响效率,这也是我写这篇文章的原因,下面和大家分享下我的解决方案

二、分析

其实上面最终就是触发构建动作,就是选择了分支后,点击开始构建,这里应该是调用了相关API,然后传参就是分支,顺着这个思路,我们去看看调用了什么接口,发现页面看不到调用了什么接口,这就可能需要去找官方文档了,看看有没有什么说明,使用过Python的同学应该知道,里面提供了强大的类库,其中就包括了jenkinsapi,可以直接使用

三、实现

公共代码

python 复制代码
# jekins地址和请求参数
jenkins_url = 'http://xxx.xx.xxx.xxx:端口号'
parameters = {'BRANCH': 'origin/dev-test-common'}


def build_job_with_parameters(jenkins_url, job_name, parameters):
    jenkins = Jenkins(jenkins_url, username='forlan', password='forlanxxx')
    job = jenkins[job_name]
    return job.invoke(build_params=parameters)

单个服务版本

1、写死某个服务

python 复制代码
def baseWay():
    job_name = 'Test-forlan'
    print(build_job_with_parameters(jenkins_url, job_name, parameters))

2、支持服务单选

python 复制代码
options = [
    "Test-forlan1",
    "Test-forlan2",
    "Test-forlan3"
]


def oneChoiceWay():
    while True:
        for i, option in enumerate(options):
            print(f"{i}: {option}")
        choice = int(input("Enter your choice : "))

        if choice >= 0:
            print(options[choice], "重启中")
            print(build_job_with_parameters(jenkins_url, options[choice], parameters))
        elif choice == -1:
            break
        else:
            print("Invalid choice. Please try again.")
        print("----------------------------------\n")

    print("Loop exited.")

多服务版本

python 复制代码
options = [
    "Test-forlan1",
    "Test-forlan2",
    "Test-forlan3"
]


def moreChoiceWay():
    exitFlag = False
    while True:
        for i, option in enumerate(options):
            print(f"{i}: {option}")
        choiceStr = input("Enter your choices(split by ',') : ")
        choices = choiceStr.split(',')

        for choice in choices:
            choice = int(choice)
            if choice >= 0:
                print(build_job_with_parameters(jenkins_url, options[choice], parameters))
            elif choice == -1:
                exitFlag = True
                break
            else:
                print("Invalid choice. Please try again.")
        print("----------------------------------\n")

        if (exitFlag):
            break

    print("Loop exited.")

最终实现效果

bash 复制代码
0: Test-forlan1
1: Test-forlan2
2: Test-forlan3
Enter your choices(split by ',') : 1,2
Test-forlan2 重启中
Test-forlan3 重启中
----------------------------------

0: Test-forlan1
1: Test-forlan2
2: Test-forlan3
Enter your choices(split by ',') : -1
----------------------------------

Loop exited.

将Python程序转换为桌面可执行文件(.exe)

安装pyinstaller库

复制代码
pip install pyinstaller

安装完毕后,使用以下命令将Python程序转为exe文件:

复制代码
pyinstaller jekins.py

这将在当前目录下生成dist文件夹,并在其中包含可执行的exe文件。完成后,可以在桌面上找到生成的exe文件,双击运行即可,效果如下:

相关推荐
曲幽11 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户83562907805116 小时前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞18 小时前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派20 小时前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪1 天前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户0332126663671 天前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫1 天前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派1 天前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风1 天前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽1 天前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic