-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-image.sh
30 lines (25 loc) · 992 Bytes
/
build-image.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
#!/bin/bash
docker login "${DOCKER_REGISTRY_SERVER}" --username "${DOCKER_REGISTRY_USER}" --password "${DOCKER_REGISTRY_PASSWORD}"
FILES=$(git diff --name-only --diff-filter=d HEAD^ HEAD | grep Dockerfile | sort)
if [ ${#FILES[*]} -eq 0 ]; then
echo "No Dockerfile changed, skip build image"
exit 0
fi
for FILE in $FILES; do
DIR=$(dirname "$FILE")
echo "DIR: $DIR"
cd $DIR
VERSION=${DIR##*/}
echo "VERSION: $VERSION"
IMAGE_NAME=${DIR%/*}
echo "IMAGE_NAME: $IMAGE_NAME"
docker build -t $IMAGE_NAME:$VERSION .
docker tag "$IMAGE_NAME:$VERSION" "${DOCKER_REGISTRY_ADDR}/$IMAGE_NAME:$VERSION"
docker tag "$IMAGE_NAME:$VERSION" "${DOCKER_REGISTRY_ADDR}/$IMAGE_NAME:latest"
docker push "${DOCKER_REGISTRY_ADDR}/$IMAGE_NAME:$VERSION"
docker push "${DOCKER_REGISTRY_ADDR}/$IMAGE_NAME:latest"
docker rmi -f "${DOCKER_REGISTRY_ADDR}/$IMAGE_NAME:$VERSION"
docker rmi -f "${DOCKER_REGISTRY_ADDR}/$IMAGE_NAME:latest"
docker rmi -f "$IMAGE_NAME:$VERSION"
cd -
done