added azure-pipeline-docker-image-cleanup.yml
d2475066
Hari Sekhon
committed
1 changed file
azure-pipeline-docker-image-cleanup.yml
/azure-pipeline-docker-image-cleanup.yml+138
/azure-pipeline-docker-image-cleanup.yml
Add comment 1 Plus  # vim:ts=2:sts=2:sw=2:et
Add comment 2 Plus  #
Add comment 3 Plus  # Author: Hari Sekhon
Add comment 4 Plus  # Date: Sun Feb 23 19:02:10 2020 +0000
Add comment 5 Plus  #
Add comment 6 Plus  # https://github.com/HariSekhon/Templates
Add comment 7 Plus  #
Add comment 8 Plus  # License: see accompanying Hari Sekhon LICENSE file
Add comment 9 Plus  #
Add comment 10 Plus  # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
Add comment 11 Plus  # to help improve or steer this or other code I publish
Add comment 12 Plus  #
Add comment 13 Plus  # https://www.linkedin.com/in/HariSekhon
Add comment 14 Plus  #
Add comment 15 Plus  
Add comment 16 Plus  # ============================================================================ #
Add comment 17 Plus  # A z u r e D e v O p s P i p e l i n e
Add comment 18 Plus  # ============================================================================ #
Add comment 19 Plus  
Add comment 20 Plus  # https://aka.ms/yaml
Add comment 21 Plus  
Add comment 22 Plus  ---
Add comment 23 Plus  trigger:
Add comment 24 Plus   branches:
Add comment 25 Plus   include:
Add comment 26 Plus   - master
Add comment 27 Plus   - main
Add comment 28 Plus   paths:
Add comment 29 Plus   include:
Add comment 30 Plus   - azure-pipeline-docker-image-cleanup.yml
Add comment 31 Plus  
Add comment 32 Plus  schedules:
Add comment 33 Plus   - cron: '0 0 * * *' # UTC timezone
Add comment 34 Plus   displayName: myScheduleAtMidnight
Add comment 35 Plus   branches:
Add comment 36 Plus   include:
Add comment 37 Plus   - main
Add comment 38 Plus   - master
Add comment 39 Plus   always: true
Add comment 40 Plus  
Add comment 41 Plus  variables:
Add comment 42 Plus   SHELLOPTS: "errexit:pipefail:nounset"
Add comment 43 Plus  
Add comment 44 Plus  steps:
Add comment 45 Plus   # XXX: do not single or double quote the end otherwise buggy behaviour leaves the trailing quote in the value eg. Path=C:\Program Files\Git\bin";...
Add comment 46 Plus   - script: echo "##vso[task.prependpath]C:\Program Files\Git\bin
Add comment 47 Plus   displayName: Prepend C:\Program Files\Git\bin to %PATH% for Bash task
Add comment 48 Plus  
Add comment 49 Plus   - script: set
Add comment 50 Plus   displayName: Environment Variables on Windows
Add comment 51 Plus  
Add comment 52 Plus   - task: PowerShell@2
Add comment 53 Plus   displayName: Disk Free Space
Add comment 54 Plus   inputs:
Add comment 55 Plus   targetType: filePath
Add comment 56 Plus   filePath: scripts/disk_space.ps1 # copy the contents from HariSekhon/Knowledge-Bash windows.md into a file in the same repo as this pipeline
Add comment 57 Plus  
Add comment 58 Plus   - script: docker info
Add comment 59 Plus   displayName: Docker Info
Add comment 60 Plus  
Add comment 61 Plus   - script: docker image ls
Add comment 62 Plus   displayName: Docker Images
Add comment 63 Plus  
Add comment 64 Plus   # requires Bash to be installed, which is provided as part of Git installation on Windows
Add comment 65 Plus   - task: Bash@3
Add comment 66 Plus   displayName: Old Docker Images
Add comment 67 Plus   inputs:
Add comment 68 Plus   targetType: inline
Add comment 69 Plus   failOnStderr: false
Add comment 70 Plus   script: |
Add comment 71 Plus   # find all docker images over 1 week old
Add comment 72 Plus   # beware 'docker images' is equivalent to 'docker image ls' so putting 'docker images ls' breaks the filter and returns nothing as a silent failure
Add comment 73 Plus   docker image ls \
Add comment 74 Plus   --filter "until=$((7*24))h" \
Add comment 75 Plus   --format '{{.Repository}}:{{.Tag}}' > docker_images.txt
Add comment 76 Plus   # Docker version 24 fails with this:
Add comment 77 Plus   #
Add comment 78 Plus   # Error response from daemon: invalid filter 'until'
Add comment 79 Plus   #
Add comment 80 Plus   # 'until' works on docker 25
Add comment 81 Plus   #
Add comment 82 Plus   # for Docker 24 do an older manual parsing
Add comment 83 Plus   #docker images --format '{{.Repository}}:{{.Tag}}__{{.CreateSince}}' |
Add comment 84 Plus   #grep -e week -e month > docker_images.txt
Add comment 85 Plus   #echo
Add comment 86 Plus   #echo "List of Docker Images older than 1 week:"
Add comment 87 Plus   #echo
Add comment 88 Plus   #sed 's/__/ /' docker_images.txt | column -t
Add comment 89 Plus  
Add comment 90 Plus   - task: Bash@3
Add comment 91 Plus   displayName: Docker Images Allowed to Delete
Add comment 92 Plus   inputs:
Add comment 93 Plus   targetType: inline
Add comment 94 Plus   failOnStderr: false
Add comment 95 Plus   script: cat docker_images_to_clean_up.txt
Add comment 96 Plus  
Add comment 97 Plus   - task: Bash@3
Add comment 98 Plus   displayName: Old Docker Images to Delete
Add comment 99 Plus   inputs:
Add comment 100 Plus   targetType: inline
Add comment 101 Plus   failOnStderr: false
Add comment 102 Plus   script: |
Add comment 103 Plus   echo "Docker Images to Delete"
Add comment 104 Plus   sed 's/__.*$//; /<none>/d' docker_images.txt |
Add comment 105 Plus   # don't delete big base images like microsoft ones that we're have to pull again
Add comment 106 Plus   #grep -v microsoft docker_images.txt < docker_images_to_delete.txt > docker_images_to_delete.txt || :
Add comment 107 Plus   # or for only a specific list of images
Add comment 108 Plus   # can't use -x because the :<tag> would break the match unless we removed -F and used regex in the file
Add comment 109 Plus   grep -Ff docker_images_to_clean_up.txt > docker_images_to_delete.txt || :
Add comment 110 Plus   cat docker_images_to_delete.txt
Add comment 111 Plus  
Add comment 112 Plus   - task: Bash@3
Add comment 113 Plus   displayName: Delete Stopped Docker Containers to Allow Deleting Dangling Images
Add comment 114 Plus   inputs:
Add comment 115 Plus   targetType: inline
Add comment 116 Plus   failOnStderr: false
Add comment 117 Plus   script: docker container prune -f
Add comment 118 Plus  
Add comment 119 Plus   - task: Bash@3
Add comment 120 Plus   displayName: Delete Old Docker Images
Add comment 121 Plus   inputs:
Add comment 122 Plus   targetType: inline
Add comment 123 Plus   failOnStderr: false
Add comment 124 Plus   script: |
Add comment 125 Plus   if [ -s docker_images_to_delete.txt ]; then
Add comment 126 Plus   cat docker_images_to_delete.txt |
Add comment 127 Plus   xargs --no-run-if-empty echo docker rmi
Add comment 128 Plus   fi
Add comment 129 Plus  
Add comment 130 Plus   - task: Bash@3
Add comment 131 Plus   displayName: Delete Dangling Docker Images
Add comment 132 Plus   inputs:
Add comment 133 Plus   targetType: inline
Add comment 134 Plus   failOnStderr: false
Add comment 135 Plus   script: |
Add comment 136 Plus   docker images -f "dangling=true" -q |
Add comment 137 Plus   xargs --no-run-if-empty docker rmi
Add comment 138 Plus