#!/bin/sh # Function to log messages log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" } # Function to cleanup temporary files cleanup() { log "Cleaning up temporary files..." if [ -f ./image.oci.tar ]; then rm ./image.oci.tar log "Removed temporary image file" fi } # Trap to ensure cleanup happens even if script fails trap cleanup EXIT # Start the Docker daemon in the background log "Starting Docker daemon..." mkdir -p /var/log dockerd \ --host=tcp://127.0.0.1:2375 \ --host=unix:///var/run/docker.sock \ > /var/log/dockerd.log 2>&1 & DOCKERD_PID=$! # Give the daemon a moment to start sleep 3 # Wait for Docker daemon to be ready log "Waiting for Docker daemon to be ready..." tries=0 max_tries=60 until docker version >/dev/null 2>&1; do if [ $tries -ge $max_tries ]; then log "ERROR: Docker daemon failed to start within reasonable time" log "Docker daemon logs:" if [ -f /var/log/dockerd.log ]; then cat /var/log/dockerd.log else log "Log file not found" fi # Try to see if the daemon is running if ps -p $DOCKERD_PID > /dev/null; then log "Docker daemon process is still running (PID: $DOCKERD_PID)" else log "Docker daemon process has exited" fi kill $DOCKERD_PID 2>/dev/null || true wait $DOCKERD_PID 2>/dev/null || true exit 1 fi log "Waiting for Docker daemon... ($tries/$max_tries)" tries=$((tries + 1)) sleep 1 done log "Docker daemon is ready" # Install regctl if not present log "Checking for regctl..." if ! command -v regctl >/dev/null 2>&1; then log "Installing regctl..." wget -O /usr/local/bin/regctl https://github.com/regclient/regclient/releases/latest/download/regctl-linux-amd64 chmod 755 /usr/local/bin/regctl fi # Enable Docker BuildKit export DOCKER_BUILDKIT=1 # Check if required environment variables are set log "Checking environment variables..." missing_vars="" [ -z "$USER" ] && missing_vars="$missing_vars USER" [ -z "$TOKEN" ] && missing_vars="$missing_vars TOKEN" [ -z "$REGISTRY" ] && missing_vars="$missing_vars REGISTRY" [ -z "$IMAGE" ] && missing_vars="$missing_vars IMAGE" [ -z "$TAG" ] && missing_vars="$missing_vars TAG" if [ -n "$missing_vars" ]; then log "ERROR: Missing required environment variables:$missing_vars" kill $DOCKERD_PID 2>/dev/null || true wait $DOCKERD_PID 2>/dev/null || true exit 1 fi # Login to registry log "Logging into registry $REGISTRY..." echo "$TOKEN" | docker login "$REGISTRY" -u "$USER" --password-stdin if [ $? -eq 0 ]; then log "Successfully logged into registry" else log "ERROR: Failed to login to registry" kill $DOCKERD_PID 2>/dev/null || true wait $DOCKERD_PID 2>/dev/null || true exit 1 fi # Build Docker image log "Building Docker image $REGISTRY/$IMAGE:$TAG..." if docker buildx build \ --platform linux/amd64 \ --target runner \ -t "$REGISTRY/$IMAGE:$TAG" \ --provenance=false \ --sbom=false \ -o "type=oci,dest=./image.oci.tar" \ .; then log "Successfully built Docker image" else log "ERROR: Failed to build Docker image" kill $DOCKERD_PID 2>/dev/null || true wait $DOCKERD_PID 2>/dev/null || true exit 1 fi # Push Docker image log "Pushing Docker image to registry..." # Set blob settings with defaults BLOB_CHUNK="${BLOB_CHUNK:-5242880}" BLOB_MAX="${BLOB_MAX:-104857600}" # Configure registry settings regctl registry set "$REGISTRY" --blob-chunk "$BLOB_CHUNK" --blob-max "$BLOB_MAX" # Import and push the image if regctl image import "$REGISTRY/$IMAGE:$TAG" ./image.oci.tar -v info; then log "Successfully pushed Docker image to registry" else log "ERROR: Failed to push Docker image to registry" kill $DOCKERD_PID 2>/dev/null || true wait $DOCKERD_PID 2>/dev/null || true exit 1 fi log "Docker build and push process completed successfully!" # Shutdown Docker daemon log "Shutting down Docker daemon..." kill $DOCKERD_PID 2>/dev/null || true wait $DOCKERD_PID 2>/dev/null || true log "Docker daemon shutdown complete" log "Exiting container..." exit 0