1. 通過python jenkins API觸發(fā)
首先需要安裝jienkins模塊
sudo pip install python-jenkins //安裝
然后先登錄,然后觸發(fā),分為帶參數構建和不帶參數構建
#!/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 ## 登陸狀態(tài),防止每次都需要登陸
## 登陸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
## 觸發(fā)一個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) ##帶參數構建觸發(fā),參數必須是字典類型
return self.get_object().build_job(job_name, token=token) ## 無參數構建觸發(fā)
2. curl調用啟動jenkins
- 1.無參數構建任務
curl -X POST ${jenkins_master}/job/${job_name}/build --user ${jenkins_user}:${jenkins_passwd}
- 含參數構建使用默認參數
curl -X POST ${jenkins_master}/job/${jenkins_jobname}/buildWithParameters --user ${jenkins_user}:${jenkins_passwd}
- 設置參數構建
方法一
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}
- 多參數構建
curl -X POST ${jenkins_master}/job/{jenkins_jobname}/buildWithParameters -d param1=value1¶m2=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