python-jenkins 小结

python jenkins的使用

1.登陆 jenkins

使用用户名和jenkins API token登陆到jenkins

class MY_JK(Jenkins):
    """docstring for MY_JK"""
    def __init__(self,url,user_name="myname"):
        if url[-1] != "/":
            url = url + "/"
        host_name = [ x for x in api_tokens.keys() if x in url ]
        if host_name:
            api_token = api_tokens[host_name[0]] 
        else:
            print "plz check if url in api_tokens dict"
            raise NotFoundAPITokenError
        super(MY_JK, self).__init__(url,user_name,api_token)

api_tokens是一个字典,key是url,value是我的api token

2.job 操作

常用的两个函数是 get_job_configreconfig_job
要注意的是job get到的配置文件和reconfig 输入的配置文件都是字符串类型的HTML。

    def save_job_config(self,job_name,path,save_shell=False):
        #save job comfig with xml format
        try:
            if not PT.exists(path):
                os.makedirs(path)
            config_xml = self.get_job_config(job_name)
            file_name = PT.join(path,job_name+".xml")
            save_into_file(file_name,config_xml)
            print "save {0} done".format(job_name)
            if save_shell:
                XML_FCT(file_name).save_xml_shell()
            #process promotions
            try:
                promotes = self.get_promotions(job_name)
                for promote in [ promote[u"name"] for promote in promotes ]:
                    xml_data = self.get_promotion_config(promote,job_name)
                    xml_file = PT.join(path,job_name+"__"+promote+".xml")
                    save_into_file(xml_file,xml_data)
                    print "save {0} config".format(promote)
            except NotFoundException as e:
                pass
        except Exception as e:
            print e

    def _reconfig_job(self,job_name,xml_file):
        print "configing",job_name,"."*30
        context = return_file_context(xml_file)
        if "__"  in job_name:
            print "i guess this is a promotion {0},please use _reconfig_promotion instead of _reconfig_job ".format(job_name)
        else:
            self.reconfig_job(job_name,context)
        print "config done"

    def _create_job(self,job_name,xml_file):
        print "create",job_name,"."*30
        context = return_file_context(xml_file)
        if "__"  in job_name:
            print "i guess this is a promotion {0},please use _create_promotion instead of _create_job ".format(job_name)
        else:
            self.create_job(job_name,context)
            print "create done"

    def _reconfig_promotion(self, name, job_name, xml_file):
        print "configing promotion",job_name,"."*30
        context = return_file_context(xml_file)
        self.reconfig_promotion(name,job_name,context)

    def _create_promotion(self, name, job_name, xml_file):
        print "create promotion",job_name,"."*30
        context = return_file_context(xml_file)
        self.create_promotion(name,job_name,context)

tips:
return_file_context是html直接转成string的函数,与之相反的是save_into_file
promotion的配置在保存到本地的时候特意加了__,以区分job的配置
这里可以看出job的本质是XML文件,通过XML代码块的增减可以实现job的自动化配置与备份
如果更进一步,使用yaml来表达XML的配置的化会更加方便,这就是jenkins-job-builder做的事情

3.view的操作

    def reconfig_view_jobs(self,view_name,view_path):
        jobs = iteration_not_contain(self._get_jobs_by_view(view_name),"sct")
        for job in jobs:
            xml_file = os.path.join(view_path,job+".xml")
            if PT.exists(xml_file):
                self._reconfig_job(job,xml_file)
            else:
                print xml_file,"not existing"

    def save_view_jobs(self,root_path,view_name,save_shell=False,jobs_filter="Test"):
        root_path = PT.join(root_path,view_name)
        jobs = self._get_jobs_by_view(view_name)
        if jobs_filter:
            jobs = [job  for job in jobs if jobs_filter not in job]
        for job in jobs:
            self.save_job_config(job,root_path,save_shell)
        if save_shell:
            move_file(root_path,PT.join(root_path,"shell"),".sh")

tips:
view 操作中没有什么大的坑,唯一要注意的是针对多层嵌套的view, view name 要改成ViewName1/view/ViewName2

总结:个人认为python jenkins使用起来还是蛮舒服的,但是论功能,JenkinsAPI 这个库更全面点。

    原文作者:一只老梨花
    原文地址: https://www.jianshu.com/p/fb943bb9b915
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞