安裝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>
頁面顯示效果