-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
32 lines (26 loc) · 1.31 KB
/
Jenkinsfile
File metadata and controls
32 lines (26 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
pipeline {
agent any
stages {
stage('Delete Old Branches') {
steps {
script {
def gitRepo = checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '0a6011eb-9b16-4f3c-9f11-d32b82460219', url: 'https://github.com/venkatseetha/ganesh.git']]])
// Get current timestamp
def currentTime = new Date().getTime()
gitRepo.git('fetch --prune')
gitRepo.git('branch -r --format="%(refname:short) %(committerdate:raw)"').eachLine { branch ->
def parts = branch.split(" ")
def branchName = parts[0]
def commitTimestamp = parts[1].toLong() * 1000
// Calculate difference in days
def daysDiff = (currentTime - commitTimestamp) / (1000 * 60 * 60 * 24)
if (daysDiff > 30 && !branchName.contains('/main')) {
echo "Deleting branch: ${branchName}"
gitRepo.git("push origin --delete ${branchName}")
}
}
}
}
}
}
}