Created
December 15, 2023 11:02
-
-
Save ucguy4u/e15be48d235daf9642353b20f2b7ab70 to your computer and use it in GitHub Desktop.
Detailed description present at : https://www.developerscoffee.com/blog/automating-docker-image-tag-incrementation-for-local-development-and-repository-pushes/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Input Parameters | |
REGISTRY_URL=${1:-"custom-registry.com"} # Default to asia-docker.pkg.dev if not provided | |
IMAGE_NAME=${2:-"user-service"} | |
TAG_BASE=${3:-"0.0"} | |
VERSION_FILE=${4:-"version.txt"} | |
# Check and create version file if not exists | |
if [ ! -f "$VERSION_FILE" ]; then | |
echo "0" > "$VERSION_FILE" | |
fi | |
# Read and increment version | |
VERSION=$(cat "$VERSION_FILE") | |
NEW_VERSION=$((VERSION+1)) | |
# Build Docker image | |
docker build -t ${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION . | |
if [ $? -ne 0 ]; then | |
echo "Docker build failed" | |
exit 1 | |
fi | |
# Tag for the registry | |
docker tag ${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION $REGISTRY_URL/docker-registry/${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION | |
# Push to the registry | |
docker push $REGISTRY_URL/docker-registry/${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION | |
# Update version file | |
echo $NEW_VERSION > "$VERSION_FILE" | |
# Output the commands used | |
echo "Docker build, tag, and push for ${IMAGE_NAME} version ${TAG_BASE}.$NEW_VERSION complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment