#!/bin/bash # Configuration DEST_HARBOR="harbor-kfc.ilionx-ocp.com" DEST_PROJECT="cp4ba_test" DEST_USER="" DEST_PASS="" OUTPUT_DIR="/home/Images" ARCH="${ARCH:-amd64}" # Function to create destination repository if it doesn't exist create_repo_if_not_exists() { local repo_name="$1" local repo_short="${repo_name#${DEST_PROJECT}/}" # Check if repository exists response=$(curl -s -w "%{http_code}" -o /dev/null -u "$DEST_USER:$DEST_PASS" \ "https://$DEST_HARBOR/api/v2.0/projects/$DEST_PROJECT/repositories/$repo_short") if [ "$response" = "404" ]; then echo "Repository $repo_name does not exist, it will be created automatically on first push" elif [ "$response" = "200" ]; then echo "Repository $repo_name already exists" else echo "Warning: Could not check repository status for $repo_name (HTTP $response)" fi } # Function to push image from directory push_image() { local image_dir="$1" local repo_name="$2" local tag="$3" echo "Pushing $repo_name:$tag from $image_dir" # Create repository if it doesn't exist create_repo_if_not_exists "$repo_name" # Push using skopeo if skopeo copy --dest-creds "$DEST_USER:$DEST_PASS" \ --override-arch "$ARCH" \ --preserve-digests \ "dir:$image_dir" \ "docker://$DEST_HARBOR/$repo_name:$tag"; then echo "Successfully pushed $repo_name:$tag" else echo "Failed to push $repo_name:$tag" return 1 fi } # Main script echo "Starting Harbor image push process..." echo "Source directory: $OUTPUT_DIR" echo "Destination Harbor: $DEST_HARBOR" echo "Destination Project: $DEST_PROJECT" echo "Architecture: $ARCH" echo "----------------------------------------" # Check if output directory exists if [ ! -d "$OUTPUT_DIR" ]; then echo "Error: Output directory $OUTPUT_DIR does not exist" exit 1 fi # Find all manifest.json files and process them find "$OUTPUT_DIR" -name "manifest.json" -type f | while read -r manifest_file; do # Get the directory containing the manifest.json image_dir=$(dirname "$manifest_file") # Calculate the relative path from OUTPUT_DIR relative_path=$(realpath --relative-to="$OUTPUT_DIR" "$image_dir") # Split the path to get repository and tag # Format should be: project/repo/tag or repo/tag IFS='/' read -ra PATH_PARTS <<< "$relative_path" if [ ${#PATH_PARTS[@]} -lt 2 ]; then echo "Warning: Invalid path structure for $relative_path, skipping..." continue fi # Extract tag (last part) tag="${PATH_PARTS[-1]}" # Extract repository name (everything except the last part) repo_parts=("${PATH_PARTS[@]:0:${#PATH_PARTS[@]}-1}") repo_name=$(IFS=/; echo "${repo_parts[*]}") # If the repository doesn't start with the destination project, prepend it if [[ "$repo_name" != "$DEST_PROJECT"* ]]; then full_repo_name="$DEST_PROJECT/$repo_name" else full_repo_name="$repo_name" fi echo "Found image: $image_dir" echo "Repository: $full_repo_name" echo "Tag: $tag" echo "----------------------------------------" # Push the image push_image "$image_dir" "$full_repo_name" "$tag" echo "----------------------------------------" done echo "Harbor image push process completed!"