Automating GitStats Reports with a Jenkins Pipeline
To keep Git repository analysis up‑to‑date, this guide shows how to create a Jenkins pipeline that periodically or on‑demand runs GitStats, generates HTML reports, deploys them to Tomcat, and cleans up workspace, supporting multiple repositories with parameterized builds and email notifications.
The boss wants the GitStats analysis reports to be always up‑to‑date, so the author decides to automate the generation and deployment of these reports using Jenkins.
Two main requirements are defined: the job should run automatically once a week, and it should also allow manual execution for any individual repository when an immediate update is needed.
For manual execution, a Jenkins UI lets the boss select which repositories to update, as illustrated by the provided screenshot.
For scheduled execution, the pipeline is triggered every Sunday at 3 AM, ensuring the latest data is available by Monday morning.
Pipeline Script
pipeline {
agent {
node {
label 'main-slave'
customWorkspace "/workspace/gitstats"
}
}
environment {
USER_CRE = credentials("d1cbab74-823d-41aa-abb7")
webapproot = "/workspace/apache-tomcat-7.0.99/webapps/gitstats"
}
parameters {
booleanParam(defaultValue: true, name: 'repo1', description: 'uncheck to disable [repo1]')
booleanParam(defaultValue: true, name: 'repo2', description: 'uncheck to disable [repo2]')
}
triggers {
cron '0 3 * * 7' # 每周日早上进行定时运行,因此此时机器是空闲的。
}
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage("Checkout gitstats") {
steps {
sh "mkdir -p html"
sh "rm -rf gitstats && git clone https://github.com/hoxu/gitstats.git"
}
}
stage("Under statistics") {
parallel {
stage("repo1") {
when {
expression { return params.repo1 }
}
steps {
sh 'git clone -b master https://$USER_CRE_USR:"$USER_CRE_PSW"@git.software.com/scm/repo1.git'
sh "cd gitstats && ./gitstats ../repo1 ../html/repo1"
}
post {
success {
sh 'rm -rf ${webapproot}/repo1 && mv html/repo1 ${webapproot}'
sh "rm -rf repo1"
sh "rm -rf html/repo1"
}
}
}
stage("repo2") {
when {
expression { return params.repo2 }
}
steps {
sh 'git clone -b master https://$USER_CRE_USR:"$USER_CRE_PSW"@git.software.com/scm/repo2.git'
sh "cd gitstats && ./gitstats ../repo2 ../html/repo2"
}
post {
success {
sh 'rm -rf ${webapproot}/repo2 && mv html/repo2 ${webapproot}'
sh "rm -rf repo2"
sh "rm -rf html/repo2"
}
}
}
}
}
}
post {
always {
// 总是给执行者分送邮件通知,不论是否成功都会对工作空间进行清理
script {
def email = load "vars/email.groovy"
email.build(currentBuild.result, '')
}
cleanWs()
}
}
}If you work in testing, DevOps, or R&D efficiency, leveraging open‑source tools like Jenkins and GitStats can quickly provide multi‑dimensional code analysis reports for Git repositories, helping you and your stakeholders better understand the codebase.
DevOps Engineer
DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.