What I want to achieve:
- I want to have a cron job in Jenkins that will help me delete the build history of some of the stale branches in my Multibranch pipeline. That is, inside the Multibranch pipeline named
Demo
, check for stale branches older than three months, then delete the build history there.
Context:
- I have a Multibranch pipeline named
Demo
. It is linked to my Github, hence, inside the pipeline it contains a number of branches. Normally, I can delete the build history of the branches on the Jenkins UI but this is very stressful. Is there a way I can have a cron job or groovy script that will help achieve this?
What I have so far:
<code>import com.cloudbees.hudson.plugins.folder.Folder
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
int months = 3
int totalJobs = 0
int totalBuilds = 0
int olderThanThreeMonths = 0
String targetJobName = 'demo'
// Find the multibranch pipeline project named 'demo'
def targetJob = Jenkins.instance.getAllItems(WorkflowMultiBranchProject).find { it.fullName == targetJobName }
if (targetJob) {
println "Found multibranch pipeline named '${targetJobName}'"
// Count the total number of branches (WorkflowJobs) in the multibranch pipeline
int branchCount = targetJob.items.size()
println "Total number of branches: ${branchCount}"
</code>
<code>import com.cloudbees.hudson.plugins.folder.Folder
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
int months = 3
int totalJobs = 0
int totalBuilds = 0
int olderThanThreeMonths = 0
String targetJobName = 'demo'
// Find the multibranch pipeline project named 'demo'
def targetJob = Jenkins.instance.getAllItems(WorkflowMultiBranchProject).find { it.fullName == targetJobName }
if (targetJob) {
println "Found multibranch pipeline named '${targetJobName}'"
// Count the total number of branches (WorkflowJobs) in the multibranch pipeline
int branchCount = targetJob.items.size()
println "Total number of branches: ${branchCount}"
</code>
import com.cloudbees.hudson.plugins.folder.Folder
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
int months = 3
int totalJobs = 0
int totalBuilds = 0
int olderThanThreeMonths = 0
String targetJobName = 'demo'
// Find the multibranch pipeline project named 'demo'
def targetJob = Jenkins.instance.getAllItems(WorkflowMultiBranchProject).find { it.fullName == targetJobName }
if (targetJob) {
println "Found multibranch pipeline named '${targetJobName}'"
// Count the total number of branches (WorkflowJobs) in the multibranch pipeline
int branchCount = targetJob.items.size()
println "Total number of branches: ${branchCount}"
I am very new to groovy. Started learning just yesterday. I am able to print the total number of branches in the pipeline which works, but I am finding it difficult to check for branches older than three months, and inside the branch, delete the build history to free up space.
Kindly help. Thanks.