postgresqljenkins

Run postgres query in jenkins after build


I installed jenkins and postgres on same centos7 server.

I also installed and configured "database" and "PostgreSQL Database Plugin" as shown in this image:

enter image description here

I want to insert data in my database jenkinsdb (the table i want to work on is "builds") after build is succesfull so i can track history of builds , deployments etc.

How can i run query to my database from jenkins ?


Solution

  • Create a script file, let's say build_complete.sh, with the postgresql commands:

    #!/bin/bash
    
    #Updated command that solves the bug. Courtesy: YoussefBoudaya's comment.
    "export PGPASSWORD='postgres'; sudo -u postgres -H -- psql -d jenkinsdb -c "SELECT * FROM builds" postgres"
    

    Please confirm psql path from server, it will be similar to /usr/lib/postgresql/10/bin/psql. Add execute script step at the end of your pipeline and simple run your script.

    A similar solution can be read here.