mirror of
https://github.com/shadoll/just-commons.git
synced 2025-12-20 03:26:43 +00:00
Fix multi-platform image build and push workflow
This commit is contained in:
109
images/mod.just
109
images/mod.just
@@ -74,45 +74,38 @@ build project="" tag="" local="false":
|
|||||||
# For podman, build for each platform and create manifest
|
# For podman, build for each platform and create manifest
|
||||||
IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms"
|
IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms"
|
||||||
|
|
||||||
# Build for each platform
|
# For multi-platform builds with podman, build locally only for current platform
|
||||||
for platform in "${PLATFORM_ARRAY[@]}"; do
|
echo -e "{{BLUE}}Multi-platform build detected, building for local platform only{{NORMAL}}"
|
||||||
echo -e "{{BLUE}}Building for platform: $platform{{NORMAL}}"
|
|
||||||
platform_tag="${image_name}-${platform//\//-}"
|
|
||||||
|
|
||||||
$runtime buildx build \
|
# Detect local platform
|
||||||
--platform "$platform" \
|
if [ "$(uname -m)" = "x86_64" ]; then
|
||||||
-f "$containerfile" \
|
local_platform="linux/amd64"
|
||||||
-t "$platform_tag" \
|
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
|
||||||
--load \
|
local_platform="linux/arm64"
|
||||||
"$build_context"
|
else
|
||||||
|
local_platform="linux/amd64"
|
||||||
|
fi
|
||||||
|
|
||||||
# Push platform-specific image
|
echo -e "{{BLUE}}Building for local platform: $local_platform{{NORMAL}}"
|
||||||
$runtime push "$platform_tag"
|
|
||||||
done
|
|
||||||
|
|
||||||
# Create and push manifest
|
$runtime buildx build \
|
||||||
echo -e "{{BLUE}}Creating multi-platform manifest{{NORMAL}}"
|
--platform "$local_platform" \
|
||||||
|
-f "$containerfile" \
|
||||||
|
-t "$image_name" \
|
||||||
|
--load \
|
||||||
|
"$build_context"
|
||||||
|
|
||||||
# Remove existing manifest if it exists
|
echo -e "{{GREEN}}✓ Local build completed: $image_name{{NORMAL}}"
|
||||||
$runtime manifest rm "$image_name" 2>/dev/null || true
|
echo -e "{{YELLOW}}Note: For multi-platform registry deployment, use 'just images push'{{NORMAL}}"
|
||||||
|
|
||||||
manifest_cmd="$runtime manifest create $image_name"
|
|
||||||
for platform in "${PLATFORM_ARRAY[@]}"; do
|
|
||||||
platform_tag="${image_name}-${platform//\//-}"
|
|
||||||
manifest_cmd="$manifest_cmd $platform_tag"
|
|
||||||
done
|
|
||||||
eval "$manifest_cmd"
|
|
||||||
|
|
||||||
echo -e "{{BLUE}}Pushing multi-platform manifest to registry{{NORMAL}}"
|
|
||||||
$runtime manifest push "$image_name"
|
|
||||||
else
|
else
|
||||||
if [[ "$platforms" == *","* ]]; then
|
if [[ "$platforms" == *","* ]]; then
|
||||||
# Local build requested for multi-platform - use local platform only
|
# Local build requested for multi-platform - use local platform only
|
||||||
local_platform="linux/$(uname -m)"
|
|
||||||
if [ "$(uname -m)" = "x86_64" ]; then
|
if [ "$(uname -m)" = "x86_64" ]; then
|
||||||
local_platform="linux/amd64"
|
local_platform="linux/amd64"
|
||||||
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
|
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
|
||||||
local_platform="linux/arm64"
|
local_platform="linux/arm64"
|
||||||
|
else
|
||||||
|
local_platform="linux/amd64"
|
||||||
fi
|
fi
|
||||||
echo -e "{{BLUE}}Local-only build (from multi-platform): $local_platform{{NORMAL}}"
|
echo -e "{{BLUE}}Local-only build (from multi-platform): $local_platform{{NORMAL}}"
|
||||||
platforms="$local_platform"
|
platforms="$local_platform"
|
||||||
@@ -213,23 +206,57 @@ push project="" tag="":
|
|||||||
|
|
||||||
echo -e "{{GREEN}}✓ Successfully pushed both commit and latest tags{{NORMAL}}"
|
echo -e "{{GREEN}}✓ Successfully pushed both commit and latest tags{{NORMAL}}"
|
||||||
else
|
else
|
||||||
# Regular tag push
|
# Regular tag push - build and push multi-platform to registry
|
||||||
image_name=$(just _get_image_name "$tag")
|
image_name=$(just _get_image_name "$tag")
|
||||||
|
|
||||||
# Check if the image exists locally
|
echo -e "{{BLUE}}Building and pushing multi-platform image: $image_name{{NORMAL}}"
|
||||||
if ! $runtime images "$image_name" >/dev/null 2>&1; then
|
|
||||||
echo "{{BOLD}}{{RED}}Error:{{NORMAL}} Image $image_name not found locally" >&2
|
|
||||||
echo "{{YELLOW}}Build the image first with: just images build{{NORMAL}}" >&2
|
|
||||||
echo "{{YELLOW}}Available images:{{NORMAL}}" >&2
|
|
||||||
image_base=$(just _get_image_name "")
|
|
||||||
$runtime images | grep "$image_base" || echo "No images found for project: $project" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -e "{{BLUE}}Pushing image: $image_name{{NORMAL}}"
|
# Auto-discover containerfile
|
||||||
$runtime push "$image_name"
|
containerfile_info=$(just _discover_containerfile)
|
||||||
|
read -r containerfile project_name build_context <<< "$containerfile_info"
|
||||||
|
|
||||||
echo -e "{{GREEN}}✓ Successfully pushed: $image_name{{NORMAL}}"
|
# Detect platforms
|
||||||
|
platforms=$(just _detect_platforms "$containerfile")
|
||||||
|
echo -e "{{BLUE}}Platforms: $platforms{{NORMAL}}"
|
||||||
|
|
||||||
|
# Build for each platform and push to registry
|
||||||
|
IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms"
|
||||||
|
|
||||||
|
for platform in "${PLATFORM_ARRAY[@]}"; do
|
||||||
|
echo -e "{{BLUE}}Building and pushing for platform: $platform{{NORMAL}}"
|
||||||
|
platform_tag="${image_name}-${platform//\//-}"
|
||||||
|
|
||||||
|
$runtime buildx build \
|
||||||
|
--platform "$platform" \
|
||||||
|
-f "$containerfile" \
|
||||||
|
-t "$platform_tag" \
|
||||||
|
"$build_context"
|
||||||
|
|
||||||
|
# Push platform-specific image to registry
|
||||||
|
$runtime push "$platform_tag"
|
||||||
|
done
|
||||||
|
|
||||||
|
# Create and push manifest from registry images
|
||||||
|
echo -e "{{BLUE}}Creating multi-platform manifest{{NORMAL}}"
|
||||||
|
$runtime manifest rm "$image_name" 2>/dev/null || true
|
||||||
|
|
||||||
|
manifest_cmd="$runtime manifest create $image_name"
|
||||||
|
for platform in "${PLATFORM_ARRAY[@]}"; do
|
||||||
|
platform_tag="${image_name}-${platform//\//-}"
|
||||||
|
manifest_cmd="$manifest_cmd $platform_tag"
|
||||||
|
done
|
||||||
|
eval "$manifest_cmd"
|
||||||
|
|
||||||
|
echo -e "{{BLUE}}Pushing multi-platform manifest to registry{{NORMAL}}"
|
||||||
|
$runtime manifest push "$image_name"
|
||||||
|
|
||||||
|
# Clean up local platform-specific tags
|
||||||
|
for platform in "${PLATFORM_ARRAY[@]}"; do
|
||||||
|
platform_tag="${image_name}-${platform//\//-}"
|
||||||
|
$runtime rmi "$platform_tag" 2>/dev/null || true
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e "{{GREEN}}✓ Successfully pushed multi-platform image: $image_name{{NORMAL}}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Pull project image from registry
|
# Pull project image from registry
|
||||||
|
|||||||
Reference in New Issue
Block a user