Beispiel #1
0
def code_release(pk, deploy):
    """
    后台执行上线任务(后台jenkins构建任务)
    :param deploy: Deploy实例(申请上线会往数据库里插一条记录,传过来的就是这条记录)
    :return:
    """
    # jenkins = JenkinsApi()
    # number = jenkins.get_next_build_number(deploy['name'])
    # # Tag是自己在jenkins命名的tag变量名
    # jenkins.build_job(deploy['name'], parameters={'Tag': deploy['version']})
    # sleep(20)
    # console_output = jenkins.get_build_console_output(deploy['name'], number)
    # deploy["console_output"] = console_output
    # # deploy.save()

    jenkins = JenkinsApi()
    number = jenkins.get_next_build_number(deploy['name'])
    # Tag是自己在jenkins命名的tag变量名
    jenkins.build_job(deploy['name'], parameters={'Tag': deploy['version']})
    time.sleep(15)
    console_output = jenkins.get_build_console_output(deploy['name'], number)
    deploy['console_output'] = console_output
    Deploy.objects.filter(pk=pk).update(**deploy)
    # return deploy
    return '[{}] Project release completed.......'.format(deploy['name'])
Beispiel #2
0
def code_release(deploy):
    """
    后台执行上线任务(后台jenkins构建任务)
    :param deploy: Deploy实例(申请上线会往数据库里插一条记录,传过来的就是这条记录)
    :return:
    """
    jenkins = JenkinsApi()
    number = jenkins.get_next_build_number(deploy.name)
    jenkins.build_job(deploy.name, parameters={'tag': deploy.version})
    sleep(30)
    console_output = jenkins.get_build_console_output(deploy.name, number)
    deploy.console_output = console_output
    deploy.save()
    return '[{}] Project release completed.......'.format(deploy.name)
Beispiel #3
0
 def update(self, request, *args, **kwargs):
     pk = int(kwargs.get("pk"))
     data = (request.data).dict()
     print(data)
     print(data['name'])
     print(data['version'])
     jenkins = JenkinsApi()
     number = jenkins.get_next_build_number(data['name'])
     jenkins.build_job(data['name'], parameters={'tag': data['version']})
     sleep(10)
     console_output = jenkins.get_build_console_output(data['name'], number)
     data['console_output'] = console_output
     print(data)
     Deploy.objects.filter(pk=pk).update(**data)
     return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #4
0
    def get(self,request,jid):

        if request.is_ajax():
            ret = {
                'status':False,
                'msg':''
            }
            job_obj = JenkinsJob.objects.filter(id=jid).first()
            bid = request.GET.get('bid','')
            if job_obj and bid:
                jk_handler = JenkinsApi(jsever_obj=job_obj.jenkins_server)
                ret = jk_handler.get_build_console_output(job_obj.job_name,bid)
            else:
                ret['msg'] = 'get job failed !'
            return JsonResponse(ret)
Beispiel #5
0
def release_code(pk, project_name, number):
    jenkins = JenkinsApi()
    start_time = time()
    data = {}
    sleep(10)
    while True:
        if (jenkins.get_build_info(project_name, number)) == "SUCCESS":
            data["release_status"] = 1
            # print(data)
            # Deploy.objects.filter(pk=pk).update(**data)
            break
        elif (jenkins.get_build_info(project_name, number)) == "ABORTED":
            data["release_status"] = 2
            # print(data)
            # Deploy.objects.filter(pk=pk).update(**data)
            break
        elif (jenkins.get_build_info(
                project_name,
                number)) == "FAILURE" or (time() - start_time) > 600:
            jenkins.stop_build(project_name, number)
            data["release_status"] = 3
            # print(data)
            # Deploy.objects.filter(pk=pk).update(**data)
            break
        else:
            console_output = jenkins.get_build_console_output(
                project_name, number)
            data['console_output'] = console_output
            Deploy.objects.filter(pk=pk).update(**data)
    console_output = jenkins.get_build_console_output(project_name, number)
    data['console_output'] = console_output
    # data['status'] = 4
    Deploy.objects.filter(pk=pk).update(**data)
    return project_name

    # return '[{}] Project release completed.......'.format(project_name)


#     return Response(serializer.data)

# @app.task(name='release')
# def release_code(pk,deploy):
#     jenkins = JenkinsApi()
#     start_time = time()
#     number = jenkins.get_next_build_number(deploy["project_name"])
#     data = {}
#     sleep(15)
#     while True:
#        if (jenkins.get_build_info(deploy["project_name"], number)) == "SUCCESS":
#            release_status = 1
#            deploy['release_status'] = release_status
#            print(data)
#            Deploy.objects.filter(pk=pk).update(**deploy)
#
#            break
#        elif (jenkins.get_build_info(deploy["project_name"], number)) == "ABORTED":
#            release_status = 3
#            deploy['release_status'] = release_status
#            print(data)
#            Deploy.objects.filter(pk=pk).update(**deploy)
#            # deploy.save()
#            break
#        # elif (jenkins.get_build_info(project_name, number)) == "FAILURE" or (time() - start_time) > 300:
#        elif (jenkins.get_build_info(deploy["project_name"], number)) == "FAILURE":
#            release_status = 2
#            deploy['release_status'] = release_status
#            print(data)
#            Deploy.objects.filter(pk=pk).update(**deploy)
#            # deploy.save()
#            break
# return '[{}] Project release completed.......'.format(project_name)
# return '[{}] Project release completed.......'.format(deploy.name)