From a44d4a52a5f3525433a26e9fb4220a50783a83ae Mon Sep 17 00:00:00 2001 From: Alexander Stoklasa Date: Mon, 9 Dec 2024 16:13:39 +0100 Subject: [PATCH] Multi platform build (#7) --- .version | 1 + build.sh | 77 +++++++++++++++++++++++++++++++++++------------- bumpversion.sh | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 .version create mode 100755 bumpversion.sh diff --git a/.version b/.version new file mode 100644 index 0000000..45c7a58 --- /dev/null +++ b/.version @@ -0,0 +1 @@ +v0.0.1 diff --git a/build.sh b/build.sh index 54b7f61..a1e6dc4 100755 --- a/build.sh +++ b/build.sh @@ -1,31 +1,68 @@ #!/bin/bash -# -# Usage ./build.sh repo/user [push] -# -IMAGE_PREFIX="${1:-app-simulator}" -for DIR in nodes/*; +# Default values for parameters +PUSH= +#MacOSX M architecture: linux/arm64/v8 +PLATFORM="linux/amd64" +IMAGE_PREFIX="app-simulator" +VERSION=$(./bumpversion.sh) + +# Function to display help +show_help() { + echo "Usage: $(basename "$0") [OPTIONS]" + echo "Options:" + echo " --push Specify whether to push the built images" + echo " --platform= Specify the build platform (default: linux/amd64), can be multiple platforms comma separated" + echo " --repoprefix= Specify the image prefix (default: app-simulator)" + echo " --help Display this help message" +} + +# Parse command line options +for ARG in "$@"; do + case $ARG in + --push) + PUSH="--push" + VERSION=$(./bumpversion.sh) + shift + ;; + --platform=*) + PLATFORM="${ARG#*=}" + shift + ;; + --repoprefix=*) + IMAGE_PREFIX="${ARG#*=}" + shift + ;; + --help) + show_help + exit 0 + ;; + *) + echo "Unknown option: $ARG" + show_help + exit 1 + ;; + esac +done + +for DIR in services/*; do if [ -d $DIR ] ; then - echo "Building ${IMAGE_PREFIX}/app-simulator:`basename $DIR`..." - docker build --platform=linux/amd64 -t "${IMAGE_PREFIX}/app-simulator:`basename $DIR`" $DIR; - if [ "$2" == "push" ]; then - echo "Pushing ${IMAGE_PREFIX}/app-simulator:`basename $DIR` to registry ...." - docker push "${IMAGE_PREFIX}/app-simulator:`basename $DIR`" - # Add your commands here - fi + IMAGE_TAG="${IMAGE_PREFIX}/services-$(basename "$DIR"):$VERSION" + echo "Building $IMAGE_TAG..." + echo "Running 'docker buildx build --platform $PLATFORM -t $IMAGE_TAG $DIR $PUSH'" + docker buildx build --platform $PLATFORM -t $IMAGE_TAG $PUSH $DIR fi done; for DIR in loaders/*; do if [ -d $DIR ] ; then - echo "Building ${IMAGE_PREFIX}/app-simulator:`basename $DIR`..." - docker build --platform=linux/amd64 -t "${IMAGE_PREFIX}/app-simulator:`basename $DIR`" $DIR; - if [ "$2" == "push" ]; then - echo "Pushing ${IMAGE_PREFIX}/app-simulator:`basename $DIR` to registry ...." - docker push "${IMAGE_PREFIX}/app-simulator:`basename $DIR`" - # Add your commands here - fi + IMAGE_TAG="${IMAGE_PREFIX}/loaders-$(basename "$DIR"):$VERSION" + echo "Building $IMAGE_TAG..." + echo "Building $IMAGE_TAG..." + echo "Running 'docker buildx build --platform $PLATFORM -t $IMAGE_TAG $DIR $PUSH'" + docker buildx build --platform $PLATFORM -t $IMAGE_TAG $PUSH $DIR $PUSH fi -done; \ No newline at end of file +done; + diff --git a/bumpversion.sh b/bumpversion.sh new file mode 100755 index 0000000..45da0a3 --- /dev/null +++ b/bumpversion.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# File that stores the version string +VERSION_FILE=".version" + +# Function to display help +show_help() { + echo "Usage: bumpversion.sh [OPTION]" + echo "Modify or display the version string in the form of 'v{major}.{minor}.{build}'." + echo "Stores the version in the file '$VERSION_FILE', defaults to v0.0.0 if it does not exist" + echo + echo "Options:" + echo " --build Increase the build number by 1." + echo " --minor Increase the minor number by 1 and the build number by 1." + echo " --major Increase the major number by 1, reset minor to 0, and increase build by 1." + echo " --help Display this help message." + echo " (no option) Display the current version." +} + +# Function to read the current version from the file +get_version() { + if [ -f "$VERSION_FILE" ]; then + cat "$VERSION_FILE" + else + echo "v0.0.0" # Default version if the file does not exist + fi +} + +# Function to update the version in the file +set_version() { + echo "$1" > "$VERSION_FILE" +} + +# Get the current version +current_version=$(get_version) + +# Extract major, minor, and build numbers +version_pattern='^v([0-9]+)\.([0-9]+)\.([0-9]+)$' +if [[ $current_version =~ $version_pattern ]]; then + major=${BASH_REMATCH[1]} + minor=${BASH_REMATCH[2]} + build=${BASH_REMATCH[3]} +else + echo "Invalid version format" + exit 1 +fi + +# Handle command-line arguments +case "$1" in + --build) + ((build++)) + ;; + --minor) + ((minor++)) + ((build++)) + ;; + --major) + ((major++)) + minor=0 + ((build++)) + ;; + --help) + show_help + exit 0 + ;; + *) + # No argument, just return the current version + echo "$current_version" + exit 0 + ;; +esac + +# Construct the new version string +new_version="v${major}.${minor}.${build}" + +# Update the version file +set_version "$new_version" + +# Output the new version +echo "$new_version" \ No newline at end of file