美文网首页CI/CD
2018-07-19 通过命令触发jenkins的job几种方法

2018-07-19 通过命令触发jenkins的job几种方法

作者: Seizens_Swift | 来源:发表于2018-07-19 23:34 被阅读135次

    1. 通过python jenkins API触发

    首先需要安装jienkins模块

    sudo pip install python-jenkins  //安装
    

    然后先登录,然后触发,分为带参数构建和不带参数构建

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import jenkins
    import datetime
    class Job(object):
        def __init__(self, jenkins_master, jenkins_user, jenkins_passwd, jenkins_job=None):
            self.jenkins_master = jenkins_master  ##master地址
            self.jenkins_job = jenkins_job   ## jenkins_job名字
            self.login_user = jenkins_user  ## 登陆账号
            self.login_passwd = jenkins_passwd  ## 登陆密码
            self.jenkins_object = None   ## 登陆状态,防止每次都需要登陆
    
        ## 登陆jenkins 只需要登陆一次即可
        def get_object(self):
            if not self.jenkins_object:
                self.jenkins_object = jenkins.Jenkins(self.jenkins_master, self.login_user, self.login_passwd)
            return self.jenkins_object
    
        ## 触发一个job
        def build_job(self, job_name=None, parameters=None):
            token=datetime.datetime.now().strftime('%Y%m%d_%H%M%S%f')
            if job_name is None:
                job_name = self.jenkins_job
            if parameters:
                return self.get_object().build_job(job_name, parameters=parameters, token=token)  ##带参数构建触发,参数必须是字典类型
            return self.get_object().build_job(job_name, token=token)  ## 无参数构建触发
    

    2. curl调用启动jenkins

    • 1.无参数构建任务
    curl -X POST ${jenkins_master}/job/${job_name}/build --user ${jenkins_user}:${jenkins_passwd}
    
      1. 含参数构建使用默认参数
    curl -X POST ${jenkins_master}/job/${jenkins_jobname}/buildWithParameters --user ${jenkins_user}:${jenkins_passwd}
    
      1. 设置参数构建

    方法一

    curl -X POST ${jenkins_master}/job/{jenkins_jobname}/buildWithParameters -d port=80 --user ${jenkins_user}:${jenkins_passwd}
    

    方法二

    curl -X POST ${jenkins_master}/job/{jenkins_jobname}/buildWithParameters -d port=80 --data-urlencode json='"{\"parameter\": [{\"name\": \"port\", \"value\": \"80\"}]}”' --user ${jenkins_user}:${jenkins_passwd}
    
      1. 多参数构建
    curl -X POST ${jenkins_master}/job/{jenkins_jobname}/buildWithParameters -d param1=value1&param2=value --user ${jenkins_user}:${jenkins_passwd}
    

    3. 通过jenkins CLI

    java -jar jenkins-cli.jar  -s http://localhost:1080/ build JOB [-c] [-f] [-p] [-r N] [-s] [-v] [-w] 
     JOB : Name of the job to build
     -c  : Check for SCM changes before starting the build, and if there's no
           change, exit without doing a build
     -f  : Follow the build progress. Like -s only interrupts are not passed
           through to the build.
     -p  : Specify the build parameters in the key=value format.
     -s  : Wait until the completion/abortion of the command. Interrupts are passed
           through to the build.
     -v  : Prints out the console output of the build. Use with -s
     -w  : Wait until the start of the command
    
    

    相关文章

      网友评论

        本文标题:2018-07-19 通过命令触发jenkins的job几种方法

        本文链接:https://www.haomeiwen.com/subject/hcmxmftx.html