In Jenkins, large volumes of artifacts from builds are the heart and soul of optimizing storage usage and maintaining an efficient CI/CD environment. The following are several effective strategies for archiving and cleaning up old artifacts in Jenkins:
1. Enable Artifact Retention Policies
Configure retention policies in your job configurations to limit the number of builds and artifacts you are keeping. For instance, you could set up Jenkins to hold only artifacts from your last 10 successful builds or to retain builds for a specified number of days.
Access Jenkins Job Configuration > Discard Old Builds and input
Max # of builds to keep Limits the number of stored builds.
Days to keep builds: This will be the number of days set up for maintaining builds.
Days to keep artifacts: This will specify the number of days after which artifacts are marked to delete.
2. Pipeline Steps archiveArtifacts and cleanWs
Archiving Artifacts selectively: Use archiveArtifacts in your pipeline to store only those files that are necessary instead of storing the entire build directory.
Workspace Cleanup: Use the cleanWs step, so all the workspace files can be deleted at the end of the builds, thus reducing the data built-up.
3.Artifact Cleanup with Jenkins Plugins
Log Rotator Plugin: you would be able to use this to configure the count of builds to be retained and how many criteria for the deletion of artifacts should be defined.
Artifact Cleanup Plugin : It automatically removes very old artifacts considering completed builds without interfering with build history.
Install and configure them at Manage Jenkins > Manage Plugins, then choose specific retention or deletion policies.
4. Save Artifacts outside Jenkins (e.g., AWS S3, Azure Blob Storage)
For long-term or high-volume storage, upload artifacts to services such as Amazon S3 or Azure Blob Storage. Configure Jenkins to upload artifacts directly through the use of plugins like the S3 Plugin for Jenkins. This offloads the storage of artifacts from the Jenkins instance and only retains metadata locally.
5. Sanitize old artifacts with scripted cleanup
You can write scripts to clean older artifacts in all the scheduled jobs.
Example: The following script deletes artifacts older than 30 days for all jobs. Use with caution and it does make it a prime candidate for being put into a cron or as part of a Jenkins job.
6. Jenkins System Clean-up: Disk Space Management
The Jenkins system clean up is available with default configuration that enables its users to enter related settings. This is accessible on Manage Jenkins > System Configuration.
Here you can select and configure options that will automatically remove unused files and old build data helping to maintain a healthy disk usage with zero human intervention.
These strategies manage artifact storage by controlling and managing it properly:
Retention policies help you hold only those builds on the system that have a reason to be there.
archiveArtifacts and cleanWs can be used to target artefact storages as well as workspace cleanup
Use external storages for very big artefacts, like S3
Under Programmatic abatement: use plugins or Groovy scripts to automate the cleanup
Finally, if such strategies are combined by an organization then all chances are quite possible that low storage usage is maintained in Jenkins without making any compromise on the required artefacts being accessible at the right time for further development and troubleshooting.