Skip to content

Commit

Permalink
Copy magician dependencies to the container image (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
…m#9299)

Co-authored-by: Thomas Rodgers <[email protected]>
  • Loading branch information
2 people authored and jiayimeow committed Oct 31, 2023
1 parent 6cf8844 commit 5fe6a45
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
22 changes: 21 additions & 1 deletion .ci/containers/go-plus/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 19 additions & 11 deletions .ci/scripts/go-plus/magician/exec.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5fe6a45

Please sign in to comment.