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

相关推荐
凌波粒几秒前
Java 8 “新”特性详解:Lambda、函数式接口、Stream、Optional 与方法引用
java·开发语言·idea
m0_7436239226 分钟前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python
拾贰_C38 分钟前
【Google | Gemini | API | POST】怎么使用Google 的Gemini API (原生版)
开发语言·lua
FreakStudio1 小时前
无硬件学LVGL—定时器篇:基于Web模拟器+MicroPython速通GUI开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
gCode Teacher 格码致知1 小时前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest
t***5442 小时前
如何在Dev-C++中选择Clang编译器
开发语言·c++
橙子199110162 小时前
Java 基础相关
java·开发语言
不要秃头的小孩2 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
科雷软件测试2 小时前
使用python+Midscene.js AI驱动打造企业级WEB自动化解决方案
前端·javascript·python