Integrating Jenkins with Hivel allows you to gain data-driven insights into your development workflow. Follow these steps to set up the integration correctly.
System Requirements
Before proceeding, ensure the following:
✅ You have Admin access to Jenkins. (Preferably, a DevOps engineer should handle this)
Check if the below plugins are installed on Jenkins. If not, install them.
Plugin Name
Description
Link
Status
HTTP Request Plugin
Enables making HTTP requests within Jenkins pipelines.
How to Wrap Your Stage Code for Metrics Collection
For each additional stage you add, ensure you wrap your existing commands with the following snippet. This wrapping captures the start time, end time, duration, and status of the stage:
def stageStart = System.currentTimeMillis()
try {
// Your stage commands here.
def stageEnd = System.currentTimeMillis()
stageData.add([
stage_name: "Your Stage Name",
start_time: new Date(stageStart).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
end_time: new Date(stageEnd).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
duration: ((stageEnd - stageStart) / 1000) as int,
status: "SUCCESS"
])
} catch (Exception e) {
def stageEnd = System.currentTimeMillis()
stageData.add([
stage_name: "Your Stage Name",
start_time: new Date(stageStart).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
end_time: new Date(stageEnd).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
duration: ((stageEnd - stageStart) / 1000) as int,
status: "FAILED",
error_message: e.getMessage()
])
throw e
}
Insert your stage commands where indicated.
This snippet ensures that each stage’s metrics are captured and included in the final payload sent to Hivel.
API call from Jenkins
Add the below code after each stage:
post {
always {
script {
// Capture overall build end time and calculate total build duration
def buildEndTime = System.currentTimeMillis()
def buildStartTime = env.BUILD_START_TIME.toLong()
// Retrieve Git information from scmVarsData if available; fallback to shell commands if not.
def branchName = scmVarsData?.GIT_BRANCH ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
def commitID = scmVarsData?.GIT_COMMIT ?: sh(script: "git rev-parse HEAD", returnStdout: true).trim()
// Construct the payload with build metrics and stage data for Hivel integration
def payload = [
org_id : env.ORG_ID.toInteger(),
job_name : env.JOB_NAME,
build_number: env.BUILD_NUMBER.toInteger(),
status : currentBuild.result ?: 'SUCCESS',
build_url : env.BUILD_URL,
start_time : new Date(buildStartTime).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
end_time : new Date(buildEndTime).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
duration : ((buildEndTime - buildStartTime) / 1000) as int,
git : [
commit : commitID,
branch : branchName,
author : sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim(),
author_email : sh(script: "git log -1 --pretty=format:'%ae'", returnStdout: true).trim(),
commit_message : sh(script: "git log -1 --pretty=format:'%s'", returnStdout: true).trim(),
repository : sh(script: "git config --get remote.origin.url", returnStdout: true).trim()
],
triggered_by: sh(script: "git log -1 --pretty=format:'%ae'", returnStdout: true).trim(),
node : env.NODE_NAME,
jenkins_version: sh(script: "jenkins --version || echo 'Unknown'", returnStdout: true).trim(),
stages : stageData
]
// Convert the payload to JSON format
def payloadJson = groovy.json.JsonOutput.toJson(payload)
echo "Payload: ${payloadJson}"
// Send the payload to the Hivel endpoint using the httpRequest step
httpRequest(
url: env.WEBHOOK_URL,
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
customHeaders: [[name: 'X-API-Key', value: env.API_KEY]],
requestBody: payloadJson,
ignoreSslErrors: true
)
}
}
}
Once the code wrapping is done, your code will look similar to the sample script in the next tab.
pipeline {
agent any
environment {
// Hivel Integration configuration
WEBHOOK_URL = 'https://app.hivel.ai/insightlyapi/webhook/jenkins'
ORG_ID = credentials('hivel-org-id') // Your Hivel Organization ID
API_KEY = credentials('hivel-jenkins-api-key') // Your Hivel API Key
}
stages {
// -------------------------------
// Stage: Initialize
// -------------------------------
stage('Initialize') {
steps {
script {
// Capture overall build start time and initialize a list for stage metrics
env.BUILD_START_TIME = System.currentTimeMillis()
stageData = []
}
}
}
// -------------------------------
// Stage: Checkout Code
// -------------------------------
stage('Checkout Code') {
steps {
script {
// Capture the start time for the Checkout stage
def stageStart = System.currentTimeMillis()
try {
// Checkout the repository using Git; credentials and branch are specified here.
// The returned data (e.g., branch and commit information) is stored in 'scmVarsData'
scmVarsData = checkout([
$class: 'GitSCM',
branches: [[name: 'dev']],
userRemoteConfigs: [[
url: 'https://gitlab.com/my-org/hello-world.git',
credentialsId: 'gitlab-token'
]]
])
def stageEnd = System.currentTimeMillis()
// Record stage metrics as SUCCESS
stageData.add([
stage_name: "Checkout Code",
start_time: new Date(stageStart).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
end_time: new Date(stageEnd).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
duration: ((stageEnd - stageStart) / 1000) as int,
status: "SUCCESS"
])
} catch (Exception e) {
def stageEnd = System.currentTimeMillis()
// Record stage metrics as FAILED and capture the error message
stageData.add([
stage_name: "Checkout Code",
start_time: new Date(stageStart).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
end_time: new Date(stageEnd).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
duration: ((stageEnd - stageStart) / 1000) as int,
status: "FAILED",
error_message: e.getMessage()
])
// Rethrow the exception to mark the build as failed
throw e
}
}
}
}
// -------------------------------
// Add additional stages below as required for your pipeline...
// For example, you might add stages for building, testing, or deploying:
// stage('Build') { ... }
// stage('Test') { ... }
// stage('Deploy') { ... }
}
post {
always {
script {
// Capture overall build end time and calculate total build duration
def buildEndTime = System.currentTimeMillis()
def buildStartTime = env.BUILD_START_TIME.toLong()
// Retrieve Git information from scmVarsData if available; fallback to shell commands if not.
def branchName = scmVarsData?.GIT_BRANCH ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
def commitID = scmVarsData?.GIT_COMMIT ?: sh(script: "git rev-parse HEAD", returnStdout: true).trim()
// Construct the payload with build metrics and stage data for Hivel integration
def payload = [
org_id : env.ORG_ID.toInteger(),
job_name : env.JOB_NAME,
build_number: env.BUILD_NUMBER.toInteger(),
status : currentBuild.result ?: 'SUCCESS',
build_url : env.BUILD_URL,
start_time : new Date(buildStartTime).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
end_time : new Date(buildEndTime).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")),
duration : ((buildEndTime - buildStartTime) / 1000) as int,
git : [
commit : commitID,
branch : branchName,
author : sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim(),
author_email : sh(script: "git log -1 --pretty=format:'%ae'", returnStdout: true).trim(),
commit_message : sh(script: "git log -1 --pretty=format:'%s'", returnStdout: true).trim(),
repository : sh(script: "git config --get remote.origin.url", returnStdout: true).trim()
],
triggered_by: sh(script: "git log -1 --pretty=format:'%ae'", returnStdout: true).trim(),
node : env.NODE_NAME,
jenkins_version: sh(script: "jenkins --version || echo 'Unknown'", returnStdout: true).trim(),
stages : stageData
]
// Convert the payload to JSON format
def payloadJson = groovy.json.JsonOutput.toJson(payload)
echo "Payload: ${payloadJson}"
// Send the payload to the Hivel endpoint using the httpRequest step
httpRequest(
url: env.WEBHOOK_URL,
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
customHeaders: [[name: 'X-API-Key', value: env.API_KEY]],
requestBody: payloadJson,
ignoreSslErrors: true
)
}
}
}
}
Once the changes are done, click on Apply -> Save.
Repeat the steps for each stage of your pipeline script. Please check the indentation while wrapping the code.
Run the build once changes are made to all the stages.
These are the details we will be receiving from Jenkins upon successful integration:
Base URL: https://app.hivel.ai/insightlyapi/webhook/jenkins
Method: POST
Headers:
Content-Type: application/json
X-API-Key: [API key from credentials]