Add Dockerfile and CI/CD script for building and pushing Docker images

This commit is contained in:
2025-10-02 13:43:01 +08:00
parent 24154f42d4
commit cf8ebd01d3
2 changed files with 172 additions and 0 deletions

32
Dockerfile Normal file
View File

@@ -0,0 +1,32 @@
# Build stage
FROM node:24-alpine AS builder
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci
# Copy project files
COPY . .
# Build the application
RUN npm run build
# Production stage
FROM nginx:alpine
# Copy built assets from builder stage
COPY --from=builder /app/dist /usr/share/nginx/html
# Copy nginx configuration (optional - using default)
# COPY nginx.conf /etc/nginx/nginx.conf
# Expose port 80
EXPOSE 80
# Start nginx
CMD ["nginx", "-g", "daemon off;"]

140
script/ci.sh Normal file
View File

@@ -0,0 +1,140 @@
#!/bin/bash
# CI/CD Script for Docker Build and Push
# This script builds a Docker image and pushes it to a private container registry
# =============================================================================
# Environment Variables (with default values)
# =============================================================================
CONTAINER_REGISTRY_URL="${CONTAINER_REGISTRY_URL:-127.0.0.1}"
CONTAINER_REGISTRY_USERNAME="${CONTAINER_REGISTRY_USERNAME:-username}"
CONTAINER_REGISTRY_NAMESPACE="${CONTAINER_REGISTRY_NAMESPACE:-username}"
CONTAINER_REGISTRY_PASSWORD="${CONTAINER_REGISTRY_PASSWORD:-password}"
CONTAINER_IMAGE_NAME="${CONTAINER_IMAGE_NAME:-image-name}"
CONTAINER_IMAGE_TAG="${CONTAINER_IMAGE_TAG:-latest}"
# =============================================================================
# Functions
# =============================================================================
# Print help message
print_help() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " build Build Docker image"
echo " push Push Docker image to registry"
echo " help Show this help message (default)"
echo ""
echo "Environment Variables:"
echo " CONTAINER_REGISTRY_URL Registry URL (default: 127.0.0.1)"
echo " CONTAINER_REGISTRY_USERNAME Registry username (default: username)"
echo " CONTAINER_REGISTRY_NAMESPACE Registry namespace (default: username)"
echo " CONTAINER_REGISTRY_PASSWORD Registry password (default: password)"
echo " CONTAINER_IMAGE_NAME Image name (default: image-name)"
echo " CONTAINER_IMAGE_TAG Image tag (default: latest)"
echo ""
echo "Examples:"
echo " $0 build"
echo " $0 push"
echo " CONTAINER_IMAGE_TAG=v1.0.0 $0 build"
}
# Build Docker image
build_image() {
# Construct the full image name with registry, namespace, name, and tag
FULL_IMAGE_NAME="${CONTAINER_REGISTRY_URL}/${CONTAINER_REGISTRY_NAMESPACE}/${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG}"
echo "=========================================="
echo "Building Docker Image"
echo "=========================================="
echo "Image: ${FULL_IMAGE_NAME}"
echo ""
# Build the Docker image using Dockerfile in current directory
docker build -t "${FULL_IMAGE_NAME}" .
# Check if build was successful
if [ $? -eq 0 ]; then
echo ""
echo "✓ Build successful: ${FULL_IMAGE_NAME}"
else
echo ""
echo "✗ Build failed"
exit 1
fi
}
# Push Docker image to registry
push_image() {
# Construct the full image name
FULL_IMAGE_NAME="${CONTAINER_REGISTRY_URL}/${CONTAINER_REGISTRY_NAMESPACE}/${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG}"
echo "=========================================="
echo "Pushing Docker Image to Registry"
echo "=========================================="
echo "Registry: ${CONTAINER_REGISTRY_URL}"
echo "Image: ${FULL_IMAGE_NAME}"
echo ""
# Login to the container registry
echo "Logging in to registry..."
echo "${CONTAINER_REGISTRY_PASSWORD}" | docker login "${CONTAINER_REGISTRY_URL}" \
-u "${CONTAINER_REGISTRY_USERNAME}" \
--password-stdin
# Check if login was successful
if [ $? -ne 0 ]; then
echo "✗ Login failed"
exit 1
fi
echo "✓ Login successful"
echo ""
# Push the image to registry
echo "Pushing image..."
docker push "${FULL_IMAGE_NAME}"
# Check if push was successful
if [ $? -eq 0 ]; then
echo ""
echo "✓ Push successful: ${FULL_IMAGE_NAME}"
else
echo ""
echo "✗ Push failed"
exit 1
fi
# Logout from registry
docker logout "${CONTAINER_REGISTRY_URL}"
}
# =============================================================================
# Main Script Logic
# =============================================================================
# If no arguments provided, show help
if [ $# -eq 0 ]; then
print_help
exit 0
fi
# Process command line arguments
case "$1" in
build)
build_image
;;
push)
push_image
;;
help|--help|-h)
print_help
;;
*)
echo "Error: Unknown option '$1'"
echo ""
print_help
exit 1
;;
esac