mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
feat(build): make multi arch manifest docker image
This commit is contained in:
+49
-22
@@ -27,6 +27,19 @@ readlink_mac() {
|
||||
REAL_PATH=$PHYS_DIR/$TARGET_FILE
|
||||
}
|
||||
|
||||
get_current_arch() {
|
||||
local current_arch
|
||||
case $(uname -m) in
|
||||
x86_64)
|
||||
current_arch=amd64
|
||||
;;
|
||||
aarch64)
|
||||
current_arch=arm64
|
||||
;;
|
||||
esac
|
||||
echo $current_arch
|
||||
}
|
||||
|
||||
pushd $(cd "$(dirname "$0")"; pwd) > /dev/null
|
||||
readlink_mac $(basename "$0")
|
||||
cd "$(dirname "$REAL_PATH")"
|
||||
@@ -41,6 +54,8 @@ export DOCKER_BUILDKIT=1
|
||||
|
||||
REGISTRY=${REGISTRY:-docker.io/yunion}
|
||||
TAG=${TAG:-latest}
|
||||
CURRENT_ARCH=$(get_current_arch)
|
||||
ARCH=${ARCH:-$CURRENT_ARCH}
|
||||
|
||||
build_bin() {
|
||||
local BUILD_ARCH=$2
|
||||
@@ -90,15 +105,25 @@ buildx_and_push() {
|
||||
docker pull "$tag"
|
||||
}
|
||||
|
||||
get_image_name() {
|
||||
local component=$1
|
||||
local arch=$2
|
||||
local is_all_arch=$3
|
||||
local img_name="$REGISTRY/$component:$TAG"
|
||||
if [[ "$is_all_arch" == "true" || "$arch" == arm64 ]]; then
|
||||
img_name="${img_name}-$arch"
|
||||
fi
|
||||
echo $img_name
|
||||
}
|
||||
|
||||
build_process() {
|
||||
local component=$1
|
||||
local arch=$2
|
||||
local is_all_arch=$3
|
||||
local img_name=$(get_image_name $component $arch $is_all_arch)
|
||||
|
||||
build_bin $component
|
||||
build_bundle_libraries $component
|
||||
img_name="$REGISTRY/$component:$TAG"
|
||||
|
||||
if [[ "$(uname -m)" == aarch64 ]]; then
|
||||
img_name="${img_name}-arm64"
|
||||
fi
|
||||
|
||||
build_image $img_name $DOCKER_DIR/Dockerfile.$component $SRC_DIR
|
||||
}
|
||||
@@ -106,11 +131,11 @@ build_process() {
|
||||
build_process_with_buildx() {
|
||||
local component=$1
|
||||
local arch=$2
|
||||
local is_all_arch=$3
|
||||
local img_name=$(get_image_name $component $arch $is_all_arch)
|
||||
|
||||
build_env="GOARCH=$arch"
|
||||
img_name="$REGISTRY/$component:$TAG"
|
||||
if [[ "$arch" == arm64 ]]; then
|
||||
img_name="$img_name-$arch"
|
||||
build_env="$build_env"
|
||||
if [[ $component == host ]]; then
|
||||
build_env="$build_env CGO_ENABLED=1"
|
||||
@@ -128,28 +153,29 @@ build_process_with_buildx() {
|
||||
esac
|
||||
}
|
||||
|
||||
function general_build(){
|
||||
local current_arch
|
||||
case $(uname -m) in
|
||||
x86_64)
|
||||
current_arch=amd64
|
||||
;;
|
||||
aarch64)
|
||||
current_arch=arm64
|
||||
;;
|
||||
esac
|
||||
|
||||
general_build() {
|
||||
local component=$1
|
||||
# 如果未指定,则默认使用当前架构
|
||||
local arch=${2:-$current_arch}
|
||||
local is_all_arch=$3
|
||||
|
||||
if [[ "$current_arch" == "$arch" ]]; then
|
||||
build_process $component
|
||||
build_process $component $arch $is_all_arch
|
||||
else
|
||||
build_process_with_buildx $component $arch
|
||||
build_process_with_buildx $component $arch $is_all_arch
|
||||
fi
|
||||
}
|
||||
|
||||
make_manifest_image() {
|
||||
local component=$1
|
||||
local img_name=$(get_image_name $component "" "false")
|
||||
docker manifest create --amend $img_name \
|
||||
$img_name-amd64 \
|
||||
$img_name-arm64
|
||||
docker manifest annotate $img_name $img_name-arm64 --arch arm64
|
||||
docker manifest push $img_name
|
||||
}
|
||||
|
||||
ALL_COMPONENTS=$(ls cmd | grep -v '.*cli$' | xargs)
|
||||
|
||||
if [ "$#" -lt 1 ]; then
|
||||
@@ -184,12 +210,13 @@ for component in $COMPONENTS; do
|
||||
case "$ARCH" in
|
||||
all)
|
||||
for arch in "arm64" "amd64"; do
|
||||
general_build $component $arch
|
||||
general_build $component $arch "true"
|
||||
done
|
||||
make_manifest_image $component
|
||||
;;
|
||||
*)
|
||||
if [ -e "$DOCKER_DIR/Dockerfile.$component" ]; then
|
||||
general_build $component $ARCH
|
||||
general_build $component $ARCH "false"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user