-
Notifications
You must be signed in to change notification settings - Fork 3
/
make_docker.sh
executable file
·89 lines (72 loc) · 2.1 KB
/
make_docker.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
# make_docker V2
#
# Build or push an image.
# Images from non-tagged commits are versioned by <last_git_tag>-<commits_since_tag>-<abbreviated_commit_sha>
# Images from tagged commits are versioned by git tag
# Images for staging are tagged and pushed only with ${VERSION}
# Images for production are tagged and pushed with both tags: latest and ${VERSION}
#
# Usage:
# $0 build <image-name> [Dockerfile-filename]
# $0 push <image-name> <target-registry> [production|staging]
# image name is pure name without repository (image-name, not sodaliteh2020/image-name)
build() {
set -x
docker build --build-arg VERSION="${VERSION}" --build-arg DATE="${DATE}" -t "${IMAGE}":latest -f "${FILE}" .
set +x
}
push() {
set -x
docker tag "${IMAGE}":latest "${TARGET_REGISTRY}/${IMAGE}:${VERSION}"
docker push "${TARGET_REGISTRY}/${IMAGE}:${VERSION}"
set +x
if [[ "$TARGET" = 'production' ]]; then
set -x
docker tag "${IMAGE}":latest "${TARGET_REGISTRY}/${IMAGE}:latest"
docker push "${TARGET_REGISTRY}/${IMAGE}:latest"
set +x
fi
}
if [[ "$1" != "build" && "$1" != "push" ]] || \
[[ "$1" = "build" ]] && [[ $# -gt 3 || $# -lt 2 ]] || \
[[ "$1" = "push" ]] && [[ $# -gt 4 || $# -lt 3 ]] && [[ "$4" != "staging" && "$4" != "production" ]]; then
echo "Usage: $0 build <image-name> [Dockerfile-filename]"
echo "Usage: $0 push <image-name> <target-registry> [production|staging]"
exit 1
fi
git fetch --tags -f
ACTION=$1
IMAGE=$2
if [ "$ACTION" = 'build' ];
then
FILE=${3:-Dockerfile}
else
TARGET_REGISTRY=$3
TARGET=${4:-staging}
fi
VERSION=$(git describe --tag --always | sed -e"s/^v//")
DATE=$(date -u +%Y-%m-%dT%H:%M:%S)
# debug section
echo "make_docker.sh:"
echo
echo "---------------"
echo "ACTION: $ACTION"
if [ "$ACTION" = 'push' ]; then
echo "TARGET: $TARGET"
echo "TARGET_REGISTRY: $TARGET_REGISTRY"
fi
echo "IMAGE: $IMAGE"
if [ "$ACTION" = 'build' ]; then
echo "FILE: $FILE"
fi
echo "VERSION: $VERSION"
echo "DATE: $DATE"
echo "---------------"
echo
if [ "$ACTION" = "build" ]; then
build
else
push
fi
echo