python如何操作jenkins
时间: 2023-11-16 09:49:41 浏览: 151
Python中可以使用Jenkins API来操作Jenkins。Jenkins API是一组RESTful接口,可以通过HTTP请求来访问Jenkins服务器。以下是基本的步骤:
1. 安装jenkinsapi模块
可以使用pip安装jenkinsapi模块:
```python
pip install jenkinsapi
```
2. 连接Jenkins服务器
```python
from jenkinsapi.jenkins import Jenkins
jenkins_url = 'http://localhost:8080'
jenkins_username = 'admin'
jenkins_password = 'admin'
jenkins_server = Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password)
```
3. 获取Jenkins服务器信息
```python
print(jenkins_server.version)
print(jenkins_server.jobs)
```
4. 创建/删除/构建Job
```python
# 创建Job
jenkins_server.create_job('test_job', job_xml)
# 删除Job
jenkins_server.delete_job('test_job')
# 构建Job
job = jenkins_server.get_job('test_job')
job.invoke()
```
5. 获取/设置Job配置
```python
job = jenkins_server.get_job('test_job')
config_xml = job.get_config()
job.update_config(config_xml)
```
6. 获取/设置Job构建信息
```python
job = jenkins_server.get_job('test_job')
build = job.get_last_build()
# 获取构建信息
print(build.get_number())
print(build.get_status())
# 设置构建信息
build.set_description('This is a test build')
```
以上是基本的操作,更多操作可以查看jenkinsapi文档。
阅读全文