-
Notifications
You must be signed in to change notification settings - Fork 20
/
img_fresh.sh
33 lines (30 loc) · 1.26 KB
/
img_fresh.sh
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
#!/bin/bash
# $1 image
# $2 source path
# return true if the image is newer than the newest file in the source tree
function img_fresh() {
# find creation date of container image, if present
# NB: remove sub-second precision and make it find-friendly
local version=$(docker -v | cut -d' ' -f 3)
local major=$(echo $version | cut -d. -f 1)
local minor=$(echo $version | cut -d. -f 2)
if (( 10#"$major" >= 1 && 10#"$minor" >= 8 )) ; then
local created=$(
docker inspect --format='{{.Created}}' --type=image "$1" |\
cut -d. -f1 |\
sed 's/T/ /'
)
else
docker build -t test-newer - &>/dev/null <<EOF
FROM alpine:3.5
RUN apk -U add curl jq
EOF
local created=$(docker run --rm -v /var/run/docker.sock:/var/run/docker.sock test-newer sh -c \
"curl --fail --unix-socket /var/run/docker.sock http:/images/\$(echo $1 | jq -R -r @uri)/json 2>/dev/null \
| jq -r '.Created[0:19]' | sed 's/T/ /'"
)
fi
# find first file in build context newer than image, if any
# NB: docker gives UTC timestamps, make sure find does not compare it to local time
[[ -n "$created" ]] && [[ -e "$2" ]] && [[ -z "$(TZ=UTC find "$2" -type f -newermt "$created" | head -n 1)" ]]
}