Skip to content

Commit ba4c6cb

Browse files
author
Secure Docker Images
committed
add additional tag to docker image if available
1 parent 37b3bc0 commit ba4c6cb

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

docker/build.sh

+43-14
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,44 @@ if [ -n "$DOCKER_REPO" ]; then
9595
echo "No existing image found for ${APP_IMAGE}:latest"
9696
fi
9797

98+
# Get tag if available
99+
FULL_TAG=$(git tag --contains $COMMIT_HASH | grep $APP_NAME | tail -n1)
100+
echo "Full tag: $FULL_TAG"
101+
SHORT_TAG=$(echo $FULL_TAG | sed s/".*\@"//)
102+
98103
# Build the app image.
99104
echo "====="
100105
echo "= Build the app image and push to registry"
101106
echo "====="
102-
docker build \
103-
--cache-from "${APP_IMAGE}":latest \
104-
--cache-from "${BUILD_IMAGE}":latest \
105-
--tag "${APP_IMAGE}":latest \
106-
--tag "${APP_IMAGE}":"${COMMIT_HASH}" \
107-
--build-arg TEMP_DEPS_DIR=${TEMP_DEPS_DIR} \
108-
--build-arg APP_NAME="${APP_FULL_NAME}" \
109-
--build-arg APP_DIR="${APP_DIR}" \
110-
--build-arg COMMIT_HASH="${COMMIT_HASH}" \
111-
--build-arg SHORT_APP_DIR="${SHORT_APP_DIR}" \
112-
--file ./docker/Dockerfile \
113-
.
107+
108+
if [ -n "$FULL_TAG" ]; then
109+
docker build \
110+
--cache-from "${APP_IMAGE}":latest \
111+
--cache-from "${BUILD_IMAGE}":latest \
112+
--tag "${APP_IMAGE}":latest \
113+
--tag "${APP_IMAGE}":"${COMMIT_HASH}" \
114+
--tag "${APP_IMAGE}":"${SHORT_TAG}" \
115+
--build-arg TEMP_DEPS_DIR=${TEMP_DEPS_DIR} \
116+
--build-arg APP_NAME="${APP_FULL_NAME}" \
117+
--build-arg APP_DIR="${APP_DIR}" \
118+
--build-arg COMMIT_HASH="${COMMIT_HASH}" \
119+
--build-arg SHORT_APP_DIR="${SHORT_APP_DIR}" \
120+
--file ./docker/Dockerfile \
121+
.
122+
else
123+
docker build \
124+
--cache-from "${APP_IMAGE}":latest \
125+
--cache-from "${BUILD_IMAGE}":latest \
126+
--tag "${APP_IMAGE}":latest \
127+
--tag "${APP_IMAGE}":"${COMMIT_HASH}" \
128+
--build-arg TEMP_DEPS_DIR=${TEMP_DEPS_DIR} \
129+
--build-arg APP_NAME="${APP_FULL_NAME}" \
130+
--build-arg APP_DIR="${APP_DIR}" \
131+
--build-arg COMMIT_HASH="${COMMIT_HASH}" \
132+
--build-arg SHORT_APP_DIR="${SHORT_APP_DIR}" \
133+
--file ./docker/Dockerfile \
134+
.
135+
fi
114136

115137
echo "====="
116138
echo "= Check images list"
@@ -122,6 +144,13 @@ echo "====="
122144
echo "= Push the app image"
123145
echo "====="
124146
if [ -n "$DOCKER_REPO" ]; then
125-
docker push "${APP_IMAGE}":latest
126-
docker push "${APP_IMAGE}":"${COMMIT_HASH}"
147+
if [ -n "$FULL_TAG" ]; then
148+
docker push "${APP_IMAGE}":latest
149+
docker push "${APP_IMAGE}":"${COMMIT_HASH}"
150+
docker push "${APP_IMAGE}":"${SHORT_TAG}"
151+
else
152+
docker push "${APP_IMAGE}":latest
153+
docker push "${APP_IMAGE}":"${COMMIT_HASH}"
154+
fi
155+
127156
fi

0 commit comments

Comments
 (0)