通过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文件,双击运行即可,效果如下:

相关推荐
特立独行的猫A31 分钟前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
吃我一窝jio3 小时前
Python与Go线程模型深度对比
java·开发语言·python
eggwyw4 小时前
C++ union 的一种妙用法
开发语言·c++
zwd20054 小时前
Manim 怎么放大画面的某一部分?
python·动画·可视化·manim·数学动画
屿华AI5 小时前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计
python·测试工具·汽车
RobinDevNotes5 小时前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
starzy19906 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
如夏倾城6 小时前
VRRP(虚拟路由器冗余协议Virtual Router Redundancy Protocol)
网络·python·网络协议·安全·智能路由器·负载均衡·信息与通信
IvanCodes7 小时前
LangChain(04):Runnable 与 LCEL 的执行与组合机制
python·langchain·agent
特创数字科技7 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python