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
CREATE USER "jenkins" WITH PASSWORD 'root123' WITH ALL PRIVILEGES

1.2 Configure Jenkins

Install the InfluxDB plugin and configure the InfluxDB connection information.

1.3 Write Pipeline Code

try {
//pipeline
currentBuild.description = "构建成功"   // define Jenkins build description, default none.
} catch(err){
currentBuild.description = "构建失败"   // define Jenkins build description, default none.
throw err
}finally{
step([$class: 'InfluxDbPublisher',
customData: null,
customDataMap: null,
customPrefix: null,
target: 'influxdb',         // InfluxDB configured in Jenkins.
selectedTarget: 'influxdb', // InfluxDB configured in Jenkins.
//jenkinsEnvParameterTag: 'KEY=' + env.PARAM,     // OPTIONAL, custom tag
jenkinsEnvParameterField: 'build_agent_name=' + 'master' + '
' + 'build_status_message=' + currentBuild.description, // OPTIONAL, custom fields
measurementName: 'jenkins_data', // OPTIONAL, measurement name
replaceDashWithUnderscore: false, // OPTIONAL, replace "-" with "_".
    ])
}
// 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
Using database jenkins
> SHOW SERIES
key
---
jenkins_data,build_result=SUCCESS,project_name=cxy-influxdb-demo_PROD
jenkins_data,build_result=SUCCESS,project_name=cxy-influxdb1-demo_PROD
> select * from jenkins_data
name: jenkins_data
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
----                ---------------- --------------- ------------------- ------------ ------------ -------------- -------------------- -------------------- -------------------- -------------------- ---------- -----------    ----------------- --------------------- ---------- ----------- -------------------- ------------            --------------          ------------            --------         -------
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
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
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
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

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

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

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