2024-05-04 13:18:37 +05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# build.sh checks if docker image with same sha256 exists remotely.
|
2024-05-04 13:29:03 +05:00
|
|
|
# if exists, do nothing, if not, build the image.
|
2024-05-04 13:18:37 +05:00
|
|
|
|
|
|
|
commit_sha="${DRONE_COMMIT_SHA}"
|
|
|
|
image_path="${REGISTRY}${REGISTRY_PROJECT}"
|
|
|
|
|
|
|
|
if [ "${DRONE_TAG}" != "" ]; then
|
|
|
|
tag="${DRONE_TAG}"
|
|
|
|
else
|
|
|
|
tag="${DRONE_COMMIT_SHA}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Starting Docker daemon.
|
|
|
|
/usr/local/bin/dockerd --data-root=/var/lib/docker --max-concurrent-uploads 1 &
|
|
|
|
|
|
|
|
# Wait for it.
|
|
|
|
echo "* Waiting for Docker daemon to start..."
|
|
|
|
while true; do
|
|
|
|
if docker ps &>/dev/null; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "* Logging in to registry..."
|
|
|
|
docker login -u "${REGISTRY_USERNAME}" -p "${REGISTRY_PASSWORD}" "${REGISTRY}"
|
|
|
|
|
|
|
|
echo "* Building docker image ${image_path}:${tag}..."
|
|
|
|
|
|
|
|
if docker manifest inspect "${image_path}:${tag}" >/dev/null; then
|
|
|
|
echo "* Docker image ${image_path}:${tag} is already exists on remote, no rebuilt necessary."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
docker build --pull --build-arg VCS_REF="${commit_sha}" --build-arg VCS_URL="${image_path}" --tag "${image_path}:${tag}" .
|
|
|
|
docker push "${image_path}:${tag}"
|