diff --git a/.ci/containers/go-plus/Dockerfile b/.ci/containers/go-plus/Dockerfile index 6a6d48d9038f..c7b8641eb297 100644 --- a/.ci/containers/go-plus/Dockerfile +++ b/.ci/containers/go-plus/Dockerfile @@ -1,5 +1,25 @@ -from golang:1.19-bullseye as resource +# Stage 1: Download go module cache for builds +FROM golang:1.19-bullseye AS builder +ENV GOCACHE=/go/cache + +RUN apt-get update && apt-get install -y unzip +WORKDIR /app1 +# Add the source code and build +ADD "https://github.com/GoogleCloudPlatform/magic-modules/archive/refs/heads/main.zip" source.zip +RUN unzip source.zip && rm source.zip +WORKDIR /app1/magic-modules-main/.ci/magician +# Build the binary (we won't use it in the final image, but it's cached) +RUN go build -o /dev/null . + +# Stage 2: Creating the final image +FROM golang:1.19-bullseye SHELL ["/bin/bash", "-c"] +ENV GOCACHE=/go/cache + +# Copy Go dependencies and Go build cache +COPY --from=builder /go/pkg/mod /go/pkg/mod +COPY --from=builder /go/cache /go/cache + # Set up Github SSH cloning. RUN ssh-keyscan github.com >> /known_hosts RUN echo "UserKnownHostsFile /known_hosts" >> /etc/ssh/ssh_config diff --git a/.ci/scripts/go-plus/magician/exec.sh b/.ci/scripts/go-plus/magician/exec.sh index 11f636d23791..acd9714f09bd 100755 --- a/.ci/scripts/go-plus/magician/exec.sh +++ b/.ci/scripts/go-plus/magician/exec.sh @@ -1,20 +1,28 @@ #!/bin/bash -# Check if there's at least one argument -if [ "$#" -eq 0 ]; then - echo "No arguments provided" - exit 1 -fi - # Get the directory of the current script DIR="$(dirname $(realpath $0))" -# Construct the path to the Go program -GO_PROGRAM="$DIR/../../../magician/" +# Construct the path to the Go program directory and binary +GO_PROGRAM_DIR="$DIR/../../../magician/" +GO_BINARY="$GO_PROGRAM_DIR/magician_binary" -pushd $GO_PROGRAM +pushd $GO_PROGRAM_DIR set -x -# Pass all arguments to the child command -go run . "$@" +# Check if the binary exists +if [ ! -f "$GO_BINARY" ]; then + # If it doesn't exist, compile the binary + echo "Building the magician binary at $GO_BINARY" + go build -o "$GO_BINARY" +fi + +# If there are no arguments only compile the binary +if [ "$#" -eq 0 ]; then + echo "No arguments provided" + exit 0 +fi + +# Run the binary and pass all arguments +$GO_BINARY "$@" set +x