简体   繁体   中英

Jenkins job to fetch data from database and display in jenkins

On a higher level - Using Jenkins (by creating a Job) is it possible to fetch data from database and display extracted data in that job or attach as artifact for that job ?

One options which was considered - Create a job with build step being 'execution of Python script'. This scripts connects to database and fetch data.

c = sqlite3.connect('db_path.db')
cur = c.cursor()
cur.execute("SELECT column_name from table_name LIMIT 11")
test = cur.fetchall()

And dump the results in html format using a template or something like that

<table>
  {% for row in test %}
  <tr>
    <th> {{ row[0] }}</th>
  </tr> 
  {% endfor %}
</table>

But the question here is, how do I display this html file in Jenkins job or attach as artifact, considering this job will be used multiple times for various jobs. I could use HTML Publisher Plugin, but the limitation is that the in my case the html name cannot be static. And I don't think this will work in my case and I don't know how to link html to python fetchall().

Can anyone please help me with this. Is there any other/better option to perform this activity ?

Question 1: How could I establish connection from fetchAll() to the html page ?

Question 2: I saw in HTML Publisher Plugin that index.html is the html page used to publish. Will this page be a standard template ? or everytime I run a job with different configurations will this page be different ?

I don't know how to do it !!! :(

You can archive the html report for each run even as it runs concurrently. If the job is dynamic you can also add a description for the job so each run will make sense to you using the Description plugin . In the job configuration you can define how many instances you'd like to keep, how many with artifacts, etc.

In general your process sounds ok for what you want to do, where is the limitation?

To fill data into HTML in Python you can use Mako.Templates .

Quick example:

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)

Template file:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM