安装python-jenkins包
pip install python-jenkins
查询所有job后端代码:
class JenkinsListView(View):
def get(self, request):
jtitle = '未登录:Jenkins'
if request.session.get('j_login', None):
jip_port = request.session['jip_port']
jtitle = '已登录:' + jip_port
return render(request, 'jenkins/jenkinslist.html', {'jtitle': jtitle})
def post(self, request):
jtitle = '未登录:Jenkins'
if request.session.get('j_login', None):
jip_port = request.session['jip_port']
jtitle = '已登录:' + jip_port
juname = request.session['juname']
jpswd = request.session['jpswd']
try:
server = jenkins.Jenkins(jip_port, juname, jpswd)
jobs = server.get_all_jobs()
li = []
for job in jobs:
li.append([])
li[jobs.index(job)].append((job['_class']))
li[jobs.index(job)].append((job['name']))
li[jobs.index(job)].append((job['url']))
li[jobs.index(job)].append((job['color']))
li[jobs.index(job)].append((job['fullname']))
except Exception as e:
return render(request, 'jenkins/jenkinslist.html', {'jtitle': jtitle, 'msglist': '查不到job信息'})
return render(request, 'jenkins/jenkinslist.html', {'jtitle': jtitle, 'li': li})
return render(request, 'jenkins/jenkinslist.html', {'jtitle': jtitle, 'msgquery':'未登录'})
前端页面代码:
<form action="/yw/jenkins_list/" method="post">
<button type="submit" class="btn btn-success">查询所有job</button>
<span style="color: red" >{{ msglist }}</span>
</form>
<div class="table-responsive">
<table class="table no-margin">
<thead>
<tr>
<th>Job 类型</th>
<th>Job 名称</th>
<th>Job url</th>
<th>Job 状态</th>
<th>Job 全名</th>
</tr>
</thead>
<tbody>
{% for job in li %}
<tr>
<td>{{ job.0 }}</td>
<td>{{ job.1 }}</td>
<td>{{ job.2 }}</td>
<td>{{ job.3 }}</td>
<td>{{ job.4 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
页面显示效果