ОБНОВЛЕНИЕ ВНУТРИ ..
Во-первых: это мой первый опыт работы с Groovy заданиями, а также новичок в Jenkins. Пожалуйста, не думайте, что я больше, чем любитель пользователей Jenkins, Groovy, Java. Заранее спасибо за помощь работает, и я получаю сообщение об ошибке при вызове функций внутри скрипта. Вот ошибка:
невозможно разрешить элемент класса
@ строка 26, столбец 18.
void disabledJob (задание элемента) {
...... ...................... ^
import java.util.concurrent.TimeUnit
long now = System.currentTimeMillis()
Jenkins.instance.getAllItems(Job.class).each{
def numDaysBack = 730
if (it.class == hudson.maven.MavenModule){
if (it.getLastBuild()){
lastBuilt = TimeUnit.MILLISECONDS.toDays(now - it.getLastBuild().getTimeInMillis())
if(lastBuilt > numDaysBack){
println it.getFullName()
println it.absoluteUrl
if (!it.disabled){
lastBuild(it)
lastFailedBuild(it)
lastSuccessfulBuild(it)
lastStableBuild(it)
} else {
disabledJob(job)
}
}
}
}
}
void disabledJob(Item job){
println "Job Disabled : " + job.disabled
}
void lastBuild(Item job){
long now = System.currentTimeMillis()
if(job.getLastBuild()){
println " Last Build : " + TimeUnit.MILLISECONDS.toDays(now - job.getLastBuild().getTimeInMillis())
} else {
println " Last Build : Not Available "
}
}
void lastSuccessfulBuild(Item job){
long now = System.currentTimeMillis()
if(job.getLastSuccessfulBuild()){
println " Last Success : " + TimeUnit.MILLISECONDS.toDays(now - job.getLastSuccessfulBuild().getTimeInMillis())
} else {
println " Last Success : Not Available "
}
}
void lastFailedBuild(Item job){
long now = System.currentTimeMillis()
if(job.getLastFailedBuild()){
println " Last Failed : " + TimeUnit.MILLISECONDS.toDays(now - job.getLastFailedBuild().getTimeInMillis())
} else {
println " Last Failed : Not Available "
}
}
void lastStableBuild(Item job){
long now = System.currentTimeMillis()
if(job.getLastStableBuild()){
println " Last Build : " + TimeUnit.MILLISECONDS.toDays(now - job.getLastStableBuild().getTimeInMillis())
} else {
println " Last Build : Not Available "
}
}
Так что я многому научился, устраняя неисправности, хотя это было постепенно и утомительно.
- Запуск Groovy заданий как «Выполнить систему Groovy Сценарий». Было бы здорово, если бы они были рядом друг с другом в списке выбора. Какая-то глупость, что это не так, но я новичок.
- Вызов функций не такой, как у меня, он работает в консоли скриптов, но не в сборке.
void processJob(Item job){
становится void processJob(job){
Элемент работает сейчас, поэтому я скопирую рабочий пример ниже:
import com.cloudbees.hudson.plugins.folder.Folder
import java.util.concurrent.TimeUnit
import jenkins.model.Jenkins
long lastRestarted = Jenkins.instance.toComputer().getConnectTime()
long now = System.currentTimeMillis()
println "Days Since Jenkins Restart: " + TimeUnit.MILLISECONDS.toDays(now - lastRestarted)
def allJob = hudson.model.Hudson.getInstance().getItems()
for(int i=0; i<allJob.size(); i++){
def job = allJob[i]
if(job instanceof hudson.model.Project && job.getLastBuild() != null ){
processJob(job)
} else if (job instanceof Folder) {
processFolder(job)
}
}
void processFolder(folder){
// println "** Folder:" + folder.getFullName()
folder.getItems().each {
if(it instanceof com.cloudbees.hudson.plugins.folder.AbstractFolder){
processFolder(it)
} else {
if (it.disabled){
// println "Disabled?: " + it.disabled + " - Full Name: " + it.getFullName()
} else {
if (it.class == org.jenkinsci.plugins.workflow.job.WorkflowJob){
processJob(it)
} else if (it.class == hudson.model.FreeStyleProject){
processJob(it)
}
}
}
}
}
void processJob(job){
long now = System.currentTimeMillis()
def numDaysBack = 730
def doDisableJobs = 0 //change to 1 to perform disable & uncomment lines 127-132
def cutOffDate = now - 1000L * 60 * 60 * 24 * numDaysBack
def disableCandidate = 0 //defining index for disable tests
def lastBuilt = 0
// Determine if the job is currently disabled and skip if true.
if (job.disabled){
println " JOB DISABLED - Skipping"
} else {
//Determine if job has proper data or is past the cut off date
if (job.getLastBuild() != null){
lastBuilt = TimeUnit.MILLISECONDS.toDays(now - job.getLastBuild().getTimeInMillis())
if(lastBuilt < numDaysBack){
} else {
// Print available information for the job
println " "
println "******************* "
println " Job Name: " + job.getFullName()
println " Job Class: " + job.getClass()
println " URL: " + job.getAbsoluteUrl()
println " Job Disabled: " + job.disabled
// Print information about the last build
if(job.getLastBuild() != null ){
def lastBuild = job.getLastBuild().getTimeInMillis()
buildDate = new Date(lastBuild)
lastBuiltDays = TimeUnit.MILLISECONDS.toDays(now - job.getLastBuild().getTimeInMillis())
if (lastBuiltDays > numDaysBack ){
disableCandidate++
}
println " Last Build: " + lastBuiltDays
} else {
println " Last Build: No Build Date "
disableCandidate++
}
// Print information about the Last Failed Build
if(job.getLastFailedBuild() != null ){
def lastFailed = job.getLastFailedBuild().getTimeInMillis()
failedDate = new Date(lastFailed)
lastFailedDays = TimeUnit.MILLISECONDS.toDays(now - job.getLastFailedBuild().getTimeInMillis())
if (lastFailedDays < numDaysBack ){
println lastFailedDays + " < " + numDaysBack
disableCandidate--
} else {
disableCandidate++
}
println " Last Failed: " + lastFailedDays
} else {
println " Last Failed: No Failed Date"
disableCandidate++
}
// Print information about the Last Successful Build
if(job.getLastSuccessfulBuild() != null ){
def lastSuccess = job.getLastSuccessfulBuild().getTimeInMillis()
successDate = new Date(lastSuccess)
lastSuccessDays = TimeUnit.MILLISECONDS.toDays(now - job.getLastSuccessfulBuild().getTimeInMillis())
if (lastSuccessDays > numDaysBack ){
disableCandidate++
}
println " Last Success: " + lastSuccessDays
} else {
println " Last Success: No Succeeded Date"
disableCandidate++
}
// Print information about the Last Stable Build
if(job.getLastStableBuild() != null ){
def lastStable = job.getLastStableBuild().getTimeInMillis()
stableDate = new Date(lastStable)
lastStableDays = TimeUnit.MILLISECONDS.toDays(now - job.getLastStableBuild().getTimeInMillis())
if (lastStableDays > numDaysBack ){
disableCandidate++
}
println " Last Stable: " + lastStableDays
} else {
println " Last Stable: No Stable Date"
disableCandidate++
}
println " *Disable Candidate: " + disableCandidate
if(doDisableJobs == 1){
if(disableCandidate >= 4){
println "DISABLING JOB"
job.doDisable()
job.save()
}
}
// Do disable would fall in this position if disableCandidate was 4.
}
}
}
}