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