Jenkins 作业从数据库中获取数据并在 jenkins 中显示
Jenkins job to fetch data from database and display in jenkins
在更高的层次上——
使用 Jenkins(通过创建作业)是否可以从数据库中获取数据并在该作业中显示提取的数据或作为该作业的工件附加?
考虑了一个选项 -
创建一个构建步骤为 'execution of Python script' 的作业。此脚本连接到数据库并获取数据。
c = sqlite3.connect('db_path.db')
cur = c.cursor()
cur.execute("SELECT column_name from table_name LIMIT 11")
test = cur.fetchall()
并使用模板或类似的东西以 html 格式转储结果
<table>
{% for row in test %}
<tr>
<th> {{ row[0] }}</th>
</tr>
{% endfor %}
</table>
但这里的问题是,我如何在 Jenkins 作业中显示此 html 文件或作为工件附加,考虑到此作业将多次用于各种作业。
我可以使用 HTML Publisher Plugin,但限制是在我的情况下 html 名称不能是静态的。我不认为这对我有用,我不知道如何 link html 到 python fetchall().
谁能帮我解决这个问题。是否有任何 other/better 选项来执行此 activity ?
问题 1:如何建立从 fetchAll() 到 html 页面的连接?
问题2:我在HTML Publisher Plugin看到index.html是用来发布的html页面。此页面将成为标准模板吗?或者每次我 运行 一份配置不同的工作时,这个页面会有所不同吗?
我不知道该怎么做!!! :(
您可以存档每个 运行 的 html 报告,即使它是同时 运行。如果工作是动态的,您还可以为工作添加描述,这样每个 运行 对您使用 Description plugin 都有意义。在作业配置中,您可以定义要保留的实例数量、工件数量等。
总的来说,您的流程对于您想做的事情来说还不错,限制在哪里?
要在 Python 中将数据填充到 HTML 中,您可以使用 Mako.Templates。
快速示例:
from mako.template import Template
from mako.lookup import TemplateLookup
from mako.runtime import Context
def fill_template(template, data):
lookup = TemplateLookup(directories=["templates"])
mytemplate = Template(filename=template, lookup=lookup)
buf = io.StringIO()
ctx = Context(buf, data=data)
mytemplate.render_context(ctx)
htm = buf.getvalue()
print(htm)
file = open("Report/index.html", "wb")
file.write(bytes(htm.encode('utf-8')))
file.close()
return 0
fdata = {'test':data}
fill_template("templates/digest.html", fdata)
模板文件:
<table style="border-bottom:solid black 1pt; cellspacing:5%">
% for test in sorted(data['test']):
<tr>
.... Do something with test data (depends on how you store it) ...
</tr>
% endfor
</table>
在更高的层次上—— 使用 Jenkins(通过创建作业)是否可以从数据库中获取数据并在该作业中显示提取的数据或作为该作业的工件附加?
考虑了一个选项 - 创建一个构建步骤为 'execution of Python script' 的作业。此脚本连接到数据库并获取数据。
c = sqlite3.connect('db_path.db')
cur = c.cursor()
cur.execute("SELECT column_name from table_name LIMIT 11")
test = cur.fetchall()
并使用模板或类似的东西以 html 格式转储结果
<table>
{% for row in test %}
<tr>
<th> {{ row[0] }}</th>
</tr>
{% endfor %}
</table>
但这里的问题是,我如何在 Jenkins 作业中显示此 html 文件或作为工件附加,考虑到此作业将多次用于各种作业。 我可以使用 HTML Publisher Plugin,但限制是在我的情况下 html 名称不能是静态的。我不认为这对我有用,我不知道如何 link html 到 python fetchall().
谁能帮我解决这个问题。是否有任何 other/better 选项来执行此 activity ?
问题 1:如何建立从 fetchAll() 到 html 页面的连接?
问题2:我在HTML Publisher Plugin看到index.html是用来发布的html页面。此页面将成为标准模板吗?或者每次我 运行 一份配置不同的工作时,这个页面会有所不同吗?
我不知道该怎么做!!! :(
您可以存档每个 运行 的 html 报告,即使它是同时 运行。如果工作是动态的,您还可以为工作添加描述,这样每个 运行 对您使用 Description plugin 都有意义。在作业配置中,您可以定义要保留的实例数量、工件数量等。
总的来说,您的流程对于您想做的事情来说还不错,限制在哪里?
要在 Python 中将数据填充到 HTML 中,您可以使用 Mako.Templates。
快速示例:
from mako.template import Template
from mako.lookup import TemplateLookup
from mako.runtime import Context
def fill_template(template, data):
lookup = TemplateLookup(directories=["templates"])
mytemplate = Template(filename=template, lookup=lookup)
buf = io.StringIO()
ctx = Context(buf, data=data)
mytemplate.render_context(ctx)
htm = buf.getvalue()
print(htm)
file = open("Report/index.html", "wb")
file.write(bytes(htm.encode('utf-8')))
file.close()
return 0
fdata = {'test':data}
fill_template("templates/digest.html", fdata)
模板文件:
<table style="border-bottom:solid black 1pt; cellspacing:5%">
% for test in sorted(data['test']):
<tr>
.... Do something with test data (depends on how you store it) ...
</tr>
% endfor
</table>