Recently, it was necessary to use the build that triggered the Jenkins task in the background code, and thought that Jenkins had some API class libraries that were already packaged to handle Jenkins-related operations. Here is a brief introduction to my findings.
Linux Curl
The first to find is the wiki:https://wiki.jenkins-ci.org/display/jenkins/remote+access+api of Jenkins ' official website.
On the homepage of the official website, there are methods for triggering the job:
As a personal attempt, the method is to trigger the job build by directly tuning curl to send a POST request via the command line. For the OpenID-managed Jenkins, you need to take the parameter--user User:password, where the user and PASSWORD are not your OpenID login password, but the user ID and API displayed in Jenkins after login Tokens, they are viewed in the following ways:
With OpenID login jenkins-> Click on the user name in the upper right corner, enter the user's personal page, click on the left settings, open the Settings page, API Token,show API Token ...
If you need to parameterize the build job, add--data-urlencode json= ' {"parameter": [{"Name": "Param_name1", "Value": "Param_value1"}, {"Name": " Param_name2 "," Value ":" Param_value2 "}]} '
Obviously, this approach is cumbersome and prone to failure due to malformed formatting, and this approach does not help us to get more information about the job so that we can follow up on the status of the job.
Python-jenkins
Keep looking, and then I found the Python-jenkins API on the Jenkins website, and after reading it, I found that it almost covered most of Jenkins's operations, making it much easier for us to perform some of the operations on Jenkins in the background.
Python-jenkins Official website: https://pypi.python.org/pypi/python-jenkins/
Python-jenkins doc:http://python-jenkins.readthedocs.io/en/latest/index.html
Below is a brief description of how to use Python-jenkins:
1. Installation
sudo pip install Python-jenkins
2. Enter the python command environment or create a new. py file jenkinsapitest.py
Import Jenkins
#定义远程的jenkins the URL of the master server, and the port
Jenkins_server_url= ' xxxx:xxxx '
#定义用户的User Id and API Token, obtained in the same way as above
User_id= ' xxxx '
api_token= ' xxxx '
#实例化jenkins对象, connect the remote Jenkins master server
Server=jenkins. Jenkins (Jenkins_server_url, username=user_id, Password=api_token)
#构建job名为job_name的job (without build parameters)
Server.build_job (job_name)
#String参数化构建job名为job_name的job, the parameter param_dict is in the form of a dictionary, such as: param_dict= {"param1": "Value1", "param2": "Value2"}
Server.build_job (Job_name, parameters=param_dict)
#获取job名为job_name的job的相关信息
Server.get_job_info (job_name)
#获取job名为job_name的job的最后次构建号
Server.get_job_info (job_name) [' Lastbuild '] [' number ']
#判断job名为job_name的job的某次构建的执行结果状态
Server.get_build_info (job_name,build_number) [' Result ']
#判断job名为job_name的job的某次构建是否还在构建中
Server.get_build_info (job_name,build_number) [' Building ']
3. More APIs can refer to Python-jenkins api:http://python-jenkins.readthedocs.io/en/latest/api.html
Python-jenkins API Usage