Search code examples
groovynexusnexus3

Nexus 3 create clean up policy with Groovy script


I want to create a cleanup policy for maven and docker repositories on nexus 3 with Groovy. All the solutions and examples I've found so far are cleanup scripts and cleanup tasks. I want with Groovy to create a cleanup policy, attach it to repository(maven or docker) and create a task to run periodically with that policy.


Solution

  • Update: This the new solution. The 1st groovy script creates and attaches a policy to a Docker repository and the 2nd groovy script is creating the Docker repository

    import org.sonatype.nexus.cleanup.storage.CleanupPolicyStorage
    
    def createPolicy (policyName) {
        try {
            def policyStorage = container.lookup(CleanupPolicyStorage.class.getName())
            def cleanupPolicy = policyStorage.newCleanupPolicy()
            cleanupPolicy.setName(policyName)
            cleanupPolicy.setNotes('')
            cleanupPolicy.setMode('deletion')
            cleanupPolicy.setFormat('docker')
            cleanupPolicy.setCriteria(['regex': '.*SNAPSHOT'])
            policyStorage.add(cleanupPolicy)
        } catch (e) {
            log.info("Cleanup policy already exists, skipping...")
        }
    
    }
    
    def attachPolicy (policyName, repositoryName) {
        try {
            def repo = repository.repositoryManager.get(repositoryName)
            def cleanupPolicyAttribute = [policyName: [policyName].toSet()]
            def conf = repo.getConfiguration()
            conf.getAttributes().put("cleanup", cleanupPolicyAttribute)
            repo.stop()
            repo.update(conf)
            repo.start()
        } catch (e) {
            log.info("Attaching policy fail")
        }
    }
    
    createPolicy('dockerCleanupPolicy')
    attachPolicy('dockerCleanupPolicy', 'docker-NameOfTheRepo')
    

    With this script we are creating the repository. Keep in mind that in older nexus version you should import import org.sonatype.nexus.repository.storage.WritePolicy instead of import org.sonatype.nexus.repository.config.WritePolicy

    import org.sonatype.nexus.blobstore.api.BlobStoreManager
    import org.sonatype.nexus.repository.config.WritePolicy
    
    def createDockerHosted(repoName, repoHttpPort){
        try {
            repository.createDockerHosted(repoName, repoHttpPort, null, BlobStoreManager.DEFAULT_BLOBSTORE_NAME, true, false, WritePolicy.ALLOW)
        } catch (e) {
            log.info("Repo already exists, skipping...")
        }
    }
    createDockerHosted('docker-NameOfTheRepo', 5001)
    

    Old solution Found it, this is an example of how you do it

    import org.sonatype.nexus.cleanup.storage.CleanupPolicyStorage;
    def policyStorage = container.lookup(CleanupPolicyStorage.class.getName());
    def cleanupPolicy = policyStorage.newCleanupPolicy();
    cleanupPolicy.setName('name');
    cleanupPolicy.setNotes('');
    cleanupPolicy.setMode('deletion');
    cleanupPolicy.setFormat('raw');
    cleanupPolicy.setCriteria(['lastBlobUpdated':'432000']);
    policyStorage.add(cleanupPolicy);