Skip to content

Instantly share code, notes, and snippets.

@tech-andgar
Forked from eliasvelazquezdev/docker-ecr-build.sh
Created November 27, 2024 21:29
Show Gist options
  • Select an option

  • Save tech-andgar/9b12acca2417ec9b43be3eb79eb03ce5 to your computer and use it in GitHub Desktop.

Select an option

Save tech-andgar/9b12acca2417ec9b43be3eb79eb03ce5 to your computer and use it in GitHub Desktop.
Shell script that automates the process of ECR login, Docker image building and pushing to an ECR repo
#!/bin/bash
# Function to validate input is not empty
validate_input() {
local input=$1
local field_name=$2
if [ -z "$input" ]; then
echo "Error: $field_name cannot be empty"
exit 1
fi
}
validate_account_id() {
local account_id=$1
if ! [[ $account_id =~ ^[0-9]{12}$ ]]; then
echo "Error: AWS Account ID must be exactly 12 digits"
exit 1
fi
}
validate_region() {
local region=$1
if ! [[ $region =~ ^[a-z]{2}-[a-z]+-[0-9]{1}$ ]]; then
echo "Error: Invalid AWS region format (e.g., us-east-1)"
exit 1
fi
}
validate_directory() {
local dir=$1
if [ ! -d "$dir" ]; then
echo "Error: Directory '$dir' does not exist"
exit 1
fi
}
validate_dockerfile() {
local dockerfile_path=$1
if [ ! -f "$dockerfile_path" ]; then
echo "Error: Dockerfile not found at '$dockerfile_path'"
exit 1
fi
}
echo "=== ECR Login ==="
read -p "Enter AWS Account ID (12 digits): " account_id
validate_input "$account_id" "AWS Account ID"
validate_account_id "$account_id"
read -p "Enter AWS Region (e.g., us-east-1): " region
validate_input "$region" "AWS Region"
validate_region "$region"
read -p "Enter AWS Profile (default if empty): " profile
if [ -z "$profile" ]; then
profile="default"
fi
echo "Logging into ECR..."
if [ "$profile" = "default" ]; then
sudo docker login -u AWS -p $(aws ecr get-login-password --region $region) $account_id.dkr.ecr.$region.amazonaws.com
else
sudo docker login -u AWS -p $(aws ecr get-login-password --region $region --profile $profile) $account_id.dkr.ecr.$region.amazonaws.com
fi
if [ $? -ne 0 ]; then
echo "Failed to log into ECR"
exit 1
fi
echo "Successfully logged into ECR"
echo -e "\n=== Docker Build ==="
read -p "Enter the tag name for your Docker image: " tag_name
validate_input "$tag_name" "Tag name"
read -p "Enter the directory containing your Dockerfile (default: current directory): " dockerfile_dir
if [ -z "$dockerfile_dir" ]; then
dockerfile_dir="."
echo "Using current directory"
fi
validate_directory "$dockerfile_dir"
read -p "Enter the Dockerfile name (default: Dockerfile): " dockerfile_name
if [ -z "$dockerfile_name" ]; then
dockerfile_name="Dockerfile"
fi
dockerfile_path="$dockerfile_dir/$dockerfile_name"
validate_dockerfile "$dockerfile_path"
echo "Building Docker image..."
sudo docker build -t "$tag_name" -f "$dockerfile_path" "$(dirname "$dockerfile_path")/.."
if [ $? -eq 0 ]; then
echo "Successfully built Docker image with tag: $tag_name"
else
echo "Failed to build Docker image"
exit 1
fi
echo -e "\n=== Docker Tag ==="
read -p "Enter the ECR repository name: " repo_name
validate_input "$repo_name" "Repository name"
ecr_uri="$account_id.dkr.ecr.$region.amazonaws.com/$repo_name:latest"
echo "Tagging Docker image..."
sudo docker tag "$tag_name:latest" "$ecr_uri"
if [ $? -eq 0 ]; then
echo "Successfully tagged $tag_name:latest as $ecr_uri"
else
echo "Failed to tag Docker image"
exit 1
fi
echo -e "\n=== Docker Push ==="
echo "Pushing Docker image to ECR..."
sudo docker push "$ecr_uri"
if [ $? -eq 0 ]; then
echo "Successfully pushed Docker image to ECR"
else
echo "Failed to push Docker image to ECR"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment