'How to cleanup azuredevops server agents maven repositories and docker images when pushing is completed

We are using Azuredevops server integrated with the self hosted agents running on linux vms in azure.

We are facing some unexpected behavior in our build agents like, for maven builds, the dependencies are again and again getting downloaded for each builds. our settings.xml file seems fine. but not sure why this is happening. also notioced that the .m2 folder repository is keep on the files and getting the disk space issues regularly. is there any way to cleanup or cache this properly in regular basis.?

Also its noticed that the images which built and pushed to the container registry, still present in the build agents. We need to cleanup this images present in the build agents in automated way. now we are executing "prune" command manually.. How we can keep the agents in healthy state without such issues in automated way?



Solution 1:[1]

I use this sort of flow to clean the images (and more) from the agent:

- job: DockerCleanBuildAndTest
    displayName: Docker Build and Test
    workspace:
      clean: all
    steps:
      - task: DockerCompose@0
        displayName: Clean
        inputs:
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: ${{ variables['container-registry-name'] }}
          dockerComposeFile: '**/docker-compose.yml'
          action: 'Run a Docker Compose command'
          dockerComposeCommand: down
          detached: false

      - task: DockerCompose@0
        displayName: Run Tests
        inputs:
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: ${{ variables['container-registry-name'] }}
          dockerComposeFile: '**/docker-compose.yml'
          action: 'Run a Docker Compose command'
          dockerComposeCommand: 'run all-tests'
          detached: false

And you can Run

docker rmi -f image-name 

which will forcefully remove the image after you push the image to the registry.

After pushing image add command line step to delete image:

- task: CmdLine@2
  inputs:
    script: 'docker rmi -f IMAGE:TAG' 

or more destructive

- task: CmdLine@2
  inputs:
    script: 'docker system prune -a --force' 

Also using docker system prune. Doing this usually removes all dangling images from the system but using it with -a should take care of removing any unused images as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kangcheng Jin-MSFT