Introduction
In the world of continuous integration and continuous deployment (CI/CD), maintaining a streamlined process for building and deploying Docker images is crucial. A common challenge is how to automatically increment image tags in a local development environment before pushing them to a Docker registry. In this post, we’ll explore a simple yet effective script to automate this process, ensuring a smooth and error-free workflow.
The Need for Automation
Managing Docker image versions manually can be error-prone and time-consuming. Automatic tag incrementation ensures that each new build has a unique identifier, making it easier to track and deploy specific versions of your application.
Setting Up the Script
We’ll create a bash script that does the following:
- Increments the image tag based on a version file.
- Builds the Docker image with the new tag.
- Pushes the image to a specified Docker registry.
Prerequisites
- Docker installed on your system.
- A Docker registry where images will be pushed (e.g., Docker Hub, Google Container Registry).
- Basic knowledge of bash scripting.
Script Breakdown
Step 1: Define Input Parameters
The script accepts four parameters:
- Registry URL (default:
asia-docker.pkg.dev
). - Image name (default:
user-service
). - Tag base (default:
0.0
). - Location of the version file (default:
version.txt
).
REGISTRY_URL=${1:-"asia-docker.pkg.dev"}
IMAGE_NAME=${2:-"user-service"}TAG_BASE=${3:-"0.0"}
VERSION_FILE=${4:-"version.txt"}
Step 2: Check and Create Version File
The script checks if the version file exists; if not, it creates one and initialises it with 0.
if [ ! -f "$VERSION_FILE" ]; then
echo "0" > "$VERSION_FILE"
fi
Step 3: Read and Increment Version
It reads the current version, increments it, and uses it as part of the tag.
VERSION=$(cat "$VERSION_FILE")
NEW_VERSION=$((VERSION+1))
Step 4: Build and Tag the Docker Image
The image is built with the new tag and then tagged for the specified registry.
docker build -t ${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION .
docker tag ${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION $REGISTRY_URL/docker-registry/${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION
Step 5: Push to Registry
The newly tagged image is pushed to the Docker registry.
docker push $REGISTRY_URL/docker-registry/${IMAGE_NAME}:${TAG_BASE}.$NEW_VERSION
Step 6: Update Version File and Output
The version file is updated with the new version number, and a confirmation message is displayed.
echo $NEW_VERSION > "$VERSION_FILE"
echo "Docker build, tag, and push for ${IMAGE_NAME} version ${TAG_BASE}.$NEW_VERSION complete."
Running the Script
To use the script, simply run it from your command line, optionally passing the desired parameters. For example:
./build_and_push_docker.sh custom-registry.com my-image 1.0 /path/to/my/version.txt
Reference:
Conclusion
Automating the Docker image tag incrementation and pushing process simplifies the development workflow, reduces human error, and ensures a consistent versioning system. By integrating this script into your development pipeline, you can focus more on development and less on the intricacies of Docker image management.
of course like your web site however you have to take a look at the spelling on several of your posts. Several of them are rife with spelling issues and I in finding it very bothersome to inform the truth however I will surely come back again.