]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
debian_build: record git repository + url in the cidb
authorFabian Holler <fabian.holler@profitbricks.com>
Tue, 4 Mar 2014 09:22:38 +0000 (10:22 +0100)
committerFabian Holler <fabian.holler@profitbricks.com>
Tue, 4 Mar 2014 10:09:26 +0000 (11:09 +0100)
For every package build the source git repository and revision is
recorded in the CIDB. This will allow us to show git diffs for all
packages that changed between 2 liveboot images.

db_add.py
debian_build.py

index 8c579b6c7ed68ece7e4c90771ab45bb6ff9029cd..029bfe169f4f1cf2f4d826f7d79797fd87eeab08 100755 (executable)
--- a/db_add.py
+++ b/db_add.py
@@ -38,17 +38,26 @@ def harmonize_timeformat(time):
             time = "'%s'" % time
     return time
 
-def db_add_build(con, db_id, number, start, end, status):
+def db_add_build(con, db_id, number, git_rev_id, start, end, status):
     cur = con.cursor()
     cur.execute("SAVEPOINT a")
     start = harmonize_timeformat(start)
     end = harmonize_timeformat(end)
     try:
-        cur.execute("INSERT INTO jenkins_build(jenkins_job_id, jenkins_build_number, build_start, build_end, status) VALUES(%s, %s, %s, %s, '%s') RETURNING id" % (db_id, number, start, end, status))
+        cur.execute("INSERT INTO jenkins_build(jenkins_job_id,"
+                    " git_revision_id, jenkins_build_number, build_start,"
+                    " build_end, status) VALUES(%s, %s, %s, %s, %s, '%s')"
+                    " RETURNING id" % (db_id, git_rev_id,  number, start, end,
+                                       status))
         con.commit()
     except psycopg2.DatabaseError as e:
         cur.execute("ROLLBACK TO a")  # have to rollback after failed command
-        logger.debug("INSERT INTO jenkins_build(jenkins_job_id, jenkins_build_number, build_start, build_end, status) VALUES(%s, %s, %s, %s, '%s') RETURNING id" % (db_id, number, start, end, status))
+        logger.debug("INSERT INTO jenkins_build(jenkins_job_id,"
+                    " git_revision_id, jenkins_build_number, build_start,"
+                    " build_end, status) VALUES(%s, %s, %s, %s, '%s')"
+                    " RETURNING id" % (db_id, git_rev_id,  number, start, end,
+                                       status))
+
     return cur.fetchone()[0]
 
 def db_update_build(con, db_id, end, status):
@@ -90,6 +99,33 @@ def db_add_package(con, package):
         #logger.debug("INSERT INTO deb_package(name) VALUES('%s') RETURNING id" % (package))
     return cur.fetchone()[0]
 
+def db_add_git_repo(con, name, url):
+    cur = con.cursor()
+    cur.execute("SAVEPOINT a")
+    try:
+        cur.execute("INSERT INTO git_repository(name, url) VALUES('%s', '%s') RETURNING id" % (name, url))
+        con.commit()
+    except psycopg2.DatabaseError as e:
+    # record already exist
+        cur.execute("ROLLBACK TO a")  # have to rollback after failed command
+        cur.execute("SELECT id FROM git_repository WHERE name='%s' OR URL='%s'"
+                    % (name, url))
+    return cur.fetchone()[0]
+
+def db_add_git_rev(con, git_repo_id, rev):
+    cur = con.cursor()
+    cur.execute("SAVEPOINT a")
+    try:
+        cur.execute("INSERT INTO git_revision(git_repository_id, revision) "
+                    "VALUES('%s', '%s') RETURNING id" % (git_repo_id, rev))
+        con.commit()
+    except psycopg2.DatabaseError as e:
+    # record already exist
+        cur.execute("ROLLBACK TO a")  # have to rollback after failed command
+        cur.execute("SELECT id FROM git_revision WHERE git_repository_id='%s' "
+                    "AND revision='%s'" % (git_repo_id, rev))
+    return cur.fetchone()[0]
+
 def db_add_package_instance(con, package_id, origin_id, build_id, version):
     cur = con.cursor()
     cur.execute("SAVEPOINT a")
@@ -103,15 +139,20 @@ def db_add_package_instance(con, package_id, origin_id, build_id, version):
         exit_error()
     return cur.fetchone()[0]
 
-def add_package_instances(origin, job_name, build_number, changes_file, version, start, end):
+def add_package_instances(origin, job_name, build_number, changes_file,
+                          version, start, end, git_repo_name, git_repo_url,
+                          git_rev):
     con = db_connect()
     logger.debug("save %s to database" %(job_name))
     db_origin_id = db_add_origin(con, origin) 
     logger.debug("INSERT success origin %s" % origin)
+    db_git_repo_id = db_add_git_repo(con, git_repo_name, git_repo_url)
+    db_git_rev_id = db_add_git_rev(con, db_git_repo_id, git_rev)
     db_job_id = db_add_job(con, job_name) 
     logger.debug("INSERT success job %s" % job_name)
     # maybe FIXME: we could add the build already once its started, but for package builds we dont wanna add failures to the db at all
-    db_build_id = db_add_build (con, db_job_id, build_number, start, end, 'successful')
+    db_build_id = db_add_build (con, db_job_id, build_number, db_git_rev_id,
+                                start, end, 'successful')
     logger.debug("INSERT success build number %s" % build_number)
     logger.debug("processing %s" %(changes_file))
     cmd = ["dcmd", '--deb', changes_file]
@@ -192,4 +233,3 @@ def finish_liveboot_build(build_id, end, status):
     con.commit()
     logger.info("CIDB update for liveboot OK.")
     return
-
index 402f67fcb558f0312af2389f4ffa341d39b6443b..cbe8f5da0b7dc68c1413c9e84385cfdc4aee8539 100755 (executable)
@@ -666,6 +666,7 @@ export {builder_env} FORCE_SHELL=TRUE
         ))
     else:
         package_instances = []
+        git_repo_name = os.path.basename(ENV['GIT_REPO_PATH'])
         try:
             package_instances = add_package_instances(
                 "profitbricks",
@@ -675,6 +676,9 @@ export {builder_env} FORCE_SHELL=TRUE
                 new_version,
                 BUILD_START,
                 BUILD_END,
+                git_repo_name,
+                ENV['GIT_REPO_PATH'],
+                ENV['GIT_COMMIT']
             )
             figlet('CIDB OK')