From ec6e07647f50e2b40cdc8761f9563b6103f1e794 Mon Sep 17 00:00:00 2001 From: Xingyao Wang Date: Sat, 28 Sep 2024 13:19:59 -0500 Subject: [PATCH] fix hash equivalance verification ci for fork (#4107) --- .github/workflows/ghcr-build.yml | 6 ++-- containers/build.sh | 60 ++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ghcr-build.yml b/.github/workflows/ghcr-build.yml index ebc40d8851..3482477790 100644 --- a/.github/workflows/ghcr-build.yml +++ b/.github/workflows/ghcr-build.yml @@ -71,11 +71,11 @@ jobs: - name: Build and push app image if: "!github.event.pull_request.head.repo.fork" run: | - ./containers/build.sh openhands ${{ github.repository_owner }} --push + ./containers/build.sh -i openhands -o ${{ github.repository_owner }} --push - name: Build app image if: "github.event.pull_request.head.repo.fork" run: | - ./containers/build.sh openhands image ${{ github.repository_owner }} + ./containers/build.sh -i openhands -o ${{ github.repository_owner }} --load - name: Get hash in App Image id: get_hash_in_app_image run: | @@ -154,7 +154,7 @@ jobs: - name: Build and push runtime image ${{ matrix.base_image.image }} if: github.event.pull_request.head.repo.fork != true run: | - ./containers/build.sh runtime ${{ github.repository_owner }} --push ${{ matrix.base_image.tag }} + ./containers/build.sh -i runtime -o ${{ github.repository_owner }} --push -t ${{ matrix.base_image.tag }} # Forked repos can't push to GHCR, so we need to upload the image as an artifact - name: Build runtime image ${{ matrix.base_image.image }} for fork if: github.event.pull_request.head.repo.fork diff --git a/containers/build.sh b/containers/build.sh index 93f9d394ca..5b518c4fad 100755 --- a/containers/build.sh +++ b/containers/build.sh @@ -1,13 +1,40 @@ #!/bin/bash set -eo pipefail -image_name=$1 -org_name=$2 +# Initialize variables with default values +image_name="" +org_name="" push=0 -if [[ $3 == "--push" ]]; then - push=1 +load=0 +tag_suffix="" + +# Function to display usage information +usage() { + echo "Usage: $0 -i [-o ] [--push] [--load] [-t ]" + echo " -i: Image name (required)" + echo " -o: Organization name" + echo " --push: Push the image" + echo " --load: Load the image" + echo " -t: Tag suffix" + exit 1 +} + +# Parse command-line options +while [[ $# -gt 0 ]]; do + case $1 in + -i) image_name="$2"; shift 2 ;; + -o) org_name="$2"; shift 2 ;; + --push) push=1; shift ;; + --load) load=1; shift ;; + -t) tag_suffix="$2"; shift 2 ;; + *) usage ;; + esac +done +# Check if required arguments are provided +if [[ -z "$image_name" ]]; then + echo "Error: Image name is required." + usage fi -tag_suffix=$4 echo "Building: $image_name" tags=() @@ -95,14 +122,35 @@ if [[ $push -eq 1 ]]; then args+=" --cache-to=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag,mode=max" fi +if [[ $load -eq 1 ]]; then + args+=" --load" +fi + echo "Args: $args" +# Modify the platform selection based on --load flag +if [[ $load -eq 1 ]]; then + # When loading, build only for the current platform + platform=$(docker version -f '{{.Server.Os}}/{{.Server.Arch}}') +else + # For push or without load, build for multiple platforms + platform="linux/amd64,linux/arm64" +fi + +echo "Building for platform(s): $platform" + docker buildx build \ $args \ --build-arg OPENHANDS_BUILD_VERSION="$OPENHANDS_BUILD_VERSION" \ --cache-from=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag \ --cache-from=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag_base-main \ - --platform linux/amd64,linux/arm64 \ + --platform $platform \ --provenance=false \ -f "$dir/Dockerfile" \ "$DOCKER_BASE_DIR" + +# If load was requested, print the loaded images +if [[ $load -eq 1 ]]; then + echo "Local images built:" + docker images "$DOCKER_REPOSITORY" --format "{{.Repository}}:{{.Tag}}" +fi