-
Notifications
You must be signed in to change notification settings - Fork 5
/
dockerhub-tag-fresh-to-stable.sh
executable file
·69 lines (56 loc) · 1.81 KB
/
dockerhub-tag-fresh-to-stable.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
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
#!/bin/bash
set -e
usage () {
echo "usage: $(basename "$0") REPO OLD_TAG [NEW_TAG]"
echo
echo "arguments:"
echo " REPO: '<owner>/<name>' eg 'opensciencegrid/frontier-squid'"
echo " (<owner> defaults to 'opensciencegrid' if omitted)"
echo " OLD_TAG: Either 'fresh' or <YYYYMMDD-HHMM>, or a sha256:... digest"
echo " NEW_TAG: Defaults to 'stable'"
echo
echo "Environment:"
echo " user: dockerhub username"
echo " pass: dockerhub password"
echo
echo "If these are omitted, the script will prompt for them."
exit
}
fail () { echo "$@" >&2; exit 1; }
[[ $2 ]] || usage
REPOSITORY=$1
TAG_OLD=$2
TAG_NEW=${3:-stable}
[[ $REPOSITORY = */* ]] || REPOSITORY=opensciencegrid/$REPOSITORY
case $TAG_OLD in
fresh | 20[1-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] ) ;; # OK
*@sha256:* | sha256:* ) # OK - verify digest format
TAG_OLD=${TAG_OLD##*@} # don't want docker-pullable:// URL
[[ $TAG_OLD =~ ^sha256:[0-9a-f]{64}$ ]] || usage ;;
* ) usage ;;
esac
REGISTRY=https://registry-1.docker.io
CONTENT_TYPE="application/vnd.docker.distribution.manifest.v2+json"
getvar () {
read $2 -p "dockerhub $1? " "$1"
}
[[ $user ]] || getvar user
[[ $pass ]] || getvar pass -s
TOKEN=$(
authurl=https://auth.docker.io/token
scope=repository:${REPOSITORY}:pull,push
service=registry.docker.io
url="$authurl?scope=$scope&service=$service"
curl -s -u "$user:$pass" "$url" | jq -r .token
)
MANIFEST=$(
curl -s -S -f \
-H "Accept: ${CONTENT_TYPE}" \
-H "Authorization: Bearer $TOKEN" \
"${REGISTRY}/v2/${REPOSITORY}/manifests/${TAG_OLD}"
) || fail "(Possibly bad tag/digest)"
curl -X PUT \
-H "Content-Type: ${CONTENT_TYPE}" \
-H "Authorization: Bearer $TOKEN" \
-d "${MANIFEST}" \
"${REGISTRY}/v2/${REPOSITORY}/manifests/${TAG_NEW}"