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


    一、需求

    工作中微服务项目很多,重启服务的话,基本都是通过Jekins进行发布,过程差不多如下:
    1、登录后台,选择环境和服务,如下图:
    在这里插入图片描述
    2、点击“Build_with_Parameters”,弹出窗口,选择分支,然后点击“开始构建”,如下图:
    在这里插入图片描述
    通过上面的操作,就会进入构建队列排队,如果系统当前没有正在构建的,就可以立马在构建执行状态中看到,当然,这个同时构建数量,受到Maven项目配置的执行者数量的限制,如下图,同一时间只能有3个在构建中
    在这里插入图片描述

    大家觉得上面这样操作方便?

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

    二、分析

    其实上面最终操作就是开始构建,在这之前,需要进行一些操作,是不是可以理解为选择一些参数,作为传参,然后最终调用了相关API,顺着这个思路,我们去看看调用了什么接口,通过F12发现页面看不到调用了什么接口,这可能需要去找官方文档了,使用过Python的同学应该知道,里面提供了强大的类库,其中就包括了jenkinsapi,可以拿来直接使用,下面我们来看看怎么实现~

    三、实现

    1、编写公共代码

    定义了build_job_with_parameters方法,jekins地址和请求参数里定义了开发分支,我们作为全局变量,只需要传重启的job_name即可,也就是我们的微服务

    # 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、实现单个服务版本

    方式1:直接写死某个服务

    def baseWay():
        job_name = 'Test-forlan'
        print(build_job_with_parameters(jenkins_url, job_name, parameters))
    
    • 1
    • 2
    • 3

    方式2:支持服务的单选,代码如下

    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.")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3、实现多服务版本

    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.")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    最终实现效果

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

    1)安装pyinstaller库

    pip install pyinstaller
    
    • 1

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

    pyinstaller jekins.py
    
    • 1

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

  • 相关阅读:
    基于session推荐的论文阅读
    自定义指令基础
    设计模式(二)| 结构型模式(适配器模式、代理模式等)
    线性dp求解 最长子序列 —— 小题三则
    HCNP Routing&Switching之RSTP保护
    buuctf warmup 超详细
    Linux MMC子系统 - 1.eMMC简介
    UNVEILING THE PITFALLS OF KNOWLEDGE EDITING FOR LARGE LANGUAGE MODELS
    SSH私钥密钥——CTF靶机
    JS条件表达式
  • 原文地址:https://blog.csdn.net/qq_36433289/article/details/133624653