Operations 6 min read

Step-by-Step Guide to Integrating Jenkins with InfluxDB and Grafana for Build Metrics

This tutorial walks through installing Jenkins, InfluxDB, and Grafana, configuring Jenkins plugins, creating the InfluxDB database and user, writing a pipeline script to publish build metrics, and setting up Grafana dashboards with variables to visualize Jenkins build data.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Step-by-Step Guide to Integrating Jenkins with InfluxDB and Grafana for Build Metrics

0. Specific Steps

1. Set up Jenkins, InfluxDB, and Grafana.

2. Install Jenkins plugins, configure the database address, write the pipeline, and define custom data.

3. Create the InfluxDB database and user.

4. Build a unified Grafana dashboard template and use variables to replace fixed project names.

1. Preparation

1.1 Create Database

CREATE DATABASE jenkins<br/>CREATE USER "jenkins" WITH PASSWORD 'root123' WITH ALL PRIVILEGES<br/>

1.2 Configure Jenkins

Install the InfluxDB plugin and configure the InfluxDB connection information.

1.3 Write Pipeline Code

try {<br/>    //pipeline<br/>    currentBuild.description = "构建成功"   // define Jenkins build description, default none.<br/>} catch(err){<br/>    currentBuild.description = "构建失败"   // define Jenkins build description, default none.<br/>    throw err<br/>    <br/>}finally{<br/>    step([$class: 'InfluxDbPublisher',<br/>        customData: null,<br/>        customDataMap: null,<br/>        customPrefix: null,<br/>        target: 'influxdb',         // InfluxDB configured in Jenkins.<br/>        selectedTarget: 'influxdb', // InfluxDB configured in Jenkins.<br/>        //jenkinsEnvParameterTag: 'KEY=' + env.PARAM,     // OPTIONAL, custom tag<br/>        jenkinsEnvParameterField: 'build_agent_name=' + 'master' + '
' + 'build_status_message=' + currentBuild.description, // OPTIONAL, custom fields<br/>        measurementName: 'jenkins_data', // OPTIONAL, measurement name<br/>        replaceDashWithUnderscore: false, // OPTIONAL, replace "-" with "_".
    ])<br/>}<br/><br/><br/>// Custom field names based on the plugin (not fully custom)
/*
step([$class: 'InfluxDbPublisher',
            customData: null,
            customDataMap: null,
            customPrefix: null,
            target: 'influxdb',
            selectedTarget: 'infl uxdb', 
            jenkinsEnvParameterTag: 'project_name=' + "${JOB_NAME}".split('/')[-1] ,     // OPTIONAL, custom project_name
            jenkinsEnvParameterField: 'build_agent_name=' + 'master'  +  "
" +           // custom parameters, each followed by a newline
                                      'build_status_message=' + currentBuild.description +  "
" + 
                                      'midwareType=' + "${midwareType}" +  "
" + 
                                      'listenPort=' + "${port}" +  "
" + 
                                      'runUser=' + "${user}" +  "
" +
                                      'repoName=' + "${srcUrl}".split("/")[-1] - '.git' +  '
' + 
                                      'project_name=' + "${JOB_NAME}".split('/')[-1]  +  '
' +
                                      'deployHosts=' + "${targetHosts}" ,
            measurementName: 'jenkins_data', 
            replaceDashWithUnderscore: false,
        ])
*/

1.4 Build Test

View InfluxDB data.

> USE jenkins<br/>Using database jenkins<br/>> SHOW SERIES<br/>key<br/>---<br/>jenkins_data,build_result=SUCCESS,project_name=cxy-influxdb-demo_PROD<br/>jenkins_data,build_result=SUCCESS,project_name=cxy-influxdb1-demo_PROD<br/><br/>> select * from jenkins_data<br/>name: jenkins_data<br/>time                build_agent_name build_exec_time build_measured_time build_number build_result build_result_1 build_result_ordinal build_scheduled_time build_status_message build_successful build_time deployHosts    last_stable_build last_successful_build listenPort midwareType project_build_health project_name            project_name_1          project_path            repoName         runUser<br/>----                ---------------- --------------- ------------------- ------------ ------------ -------------- -------------------- -------------------- -------------------- -------------------- ---------- -----------    ----------------- --------------------- ---------- ----------- -------------------- ------------            --------------          ------------            --------         -------<br/>1559345943038000000 master           1559345921459   1559345943038       2132         SUCCESS      SUCCESS        0                    1559345921426        构建成功                 true             21612      VM_7_14_centos 2131              2131                  80         Nginx       100                  cxy-influxdb-demo_PROD  cxy-influxdb-demo_PROD  cxy-influxdb-demo_PROD  devops-tools-web nginx<br/>1559345945612000000 master           1559345927303   1559345945612       2091         SUCCESS      SUCCESS        0                    1559345927254        构建成功                 true             18358      VM_7_14_centos 2090              2090                  80         Nginx       80                   cxy-influxdb1-demo_PROD cxy-influxdb1-demo_PROD cxy-influxdb1-demo_PROD devops-tools-web nginx<br/>1559345963040000000 master           1559345940685   1559345963040       2092         SUCCESS      SUCCESS        0                    1559345940669        构建成功                 true             22371      VM_7_14_centos 2091              2091                  80         Nginx       80                   cxy-influxdb1-demo_PROD cxy-influxdb1-demo_PROD cxy-influxdb1-demo_PROD devops-tools-web nginx<br/>1559345964061000000 master           1559345940689   1559345964061       2133         SUCCESS      SUCCESS        0                    1559345940686        构建成功                 true             23375      VM_7_14_centos 2132              2132                  80         Nginx       100                  cxy-influxdb-demo_PROD  cxy-influxdb-demo_PROD  cxy-influxdb-demo_PROD  devops-tools-web nginx<br/>

2. Configure Grafana

2.1 Set Project Variables

Dashboard → Templating

Resulting view

3. FAQ

This plugin is designed for multibranch pipeline projects; project names will be prefixed with null_ , and custom project naming is now completed.

Reference:

https://wiki.jenkins.io/display/JENKINS/InfluxDB+Plugin – plugin documentation

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

monitoringci/cdInfluxDBPipelineGrafanaJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.