Jenkins

Archiving in Jenkins

This blog will discuss a way to signal a need to archive in Jenkins using groovy script. You can uses script console link to write groovy script that checks for the last successful run of any job; if the year is different to the current year, then a warning is set at the beginning of the job’s description. Thus, it is hinting to you that it’s time to perform an action, such as archiving and then deleting.

Archiving:

Following are the steps to signal the need to archive in the description of a project in Jenkins.

Step 1. Firstly, log into Jenkins as an Administrator.

Step 2. Go to Manage Jenkins -> Script Console link

Manage Jenkins window
Manage Jenkins window

 

Script console link
Script console link

Step 3. Type the following Groovy script and run it.

def warning=’[You can ARCHIVE this Job]’

def now=new Date()

for (job in hudson.model.Hudson.instance.items) {

println “\nName: ${job.name}”

Run lastSuccessfulBuild = job.getLastSuccessfulBuild()

if (lastSuccessfulBuild != null)

{ def time = lastSuccessfulBuild.getTimestamp().getTime()

if (now.month.equals(time.month)){

println(“Project has same month as build”);

}else

{ if (job.description.startsWith(warning)){ println(“Description has already been changed”); }

else{

job.setDescription(“${warning}”) } } } }

Step 4. Successful execution of the script will give output based on the build jobs available in your Jenkins.

Step 5. Any project that had its last successful build in another month than this will have the description you can archive this job added to its description.

Step 6. Click on Configure to check the description as well.

Archiving signal
Archiving signal

To write your CI/CD pipeline in Jenkins click here.

Please follow and like us: