文章目录
一、需求
微服务项目,服务很多,重启服务,基本都是通过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文件,双击运行即可,效果如下:
