Unverified Commit 98796250 authored by Landon James's avatar Landon James Committed by GitHub
Browse files

Move gradle cache step out of conditional (#4217)

## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here -->
Improve the CI build Docker image by updating the gradle binary caching
logic to properly support both x86_64 and ARM64 (aarch64) architectures.

## Description
<!--- Describe your changes in detail -->
* Dynamically set JAVA_HOME based on the architecture of the build
machine
* Modified the smithy-rs repository cloning process to ensure gradle
binary caching works consistently

Note that this fixes a bug with our existing Dockerfile. We were
manually setting `JAVA_HOME` to the `x86_64` variant of the Java binary
from `yum`. This worked even on M series Macs because we never invoked
Java in the image build and the image currently only runs on `x86_64`
machines. But if you shelled into the container on a Mac and tried to
run Gradle it would fail because yum actually downloaded the `aarch64`
binary. The update to dynamically set `JAVA_HOME` accounts for this so
now the container should work equally well on `x86_64` or `aarch64`.

## Testing
<!--- Please describe in detail how you tested your changes -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->
Was able to build on my M1 mac and shell into the container and do some
basic gradle builds. It successfully builds and runs all CI on GH, so
should work in any environments we care about.


----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent 662330b5
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -29,9 +29,25 @@ ENV RUSTUP_HOME=/opt/rustup \
    CARGO_HOME=/opt/cargo \
    PATH=/opt/cargo/bin/:${PATH} \
    CARGO_INCREMENTAL=0 \
    JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64 \
    GRADLE_USER_HOME=/home/build/.gradle

# This complicated setup is necessary to keep the docker build working on aarch64 laptops
# since we now actually use JAVA_HOME in the build (to cache the gradle binary)
RUN set -eux; \
    ARCH="$(uname -m)"; \
    case "${ARCH}" in \
        aarch64|arm64) \
            echo "export JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.aarch64" >> /etc/profile.d/java_home.sh; \
            ;; \
        x86_64) \
            echo "export JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64" >> /etc/profile.d/java_home.sh; \
            ;; \
        *) \
            echo "Unsupported architecture: ${ARCH}"; \
            exit 1; \
            ;; \
    esac

WORKDIR /root
RUN yum -y install --allowerasing \
        autoconf \
@@ -79,15 +95,22 @@ COPY . tools/ci-build
ARG smithy_rs_commit_hash=main
# If the `checkout_smithy_rs_tools` arg is set to true, then the Dockerfile will acquire the tools
# source code by checking out smithy-lang/smithy-rs/main rather than copying them from the local directory.
# If it is false we still clone the smithy-rs repo beecause we need the gradle wrapper to install the gradle
# binary. But in this case we delete the repo before installing the tools and use the local versions from
# the build context.
ARG checkout_smithy_rs_tools=false
RUN set -eux; \
    if [[ "${checkout_smithy_rs_tools}" == "true" ]]; then \
        git clone https://github.com/smithy-lang/smithy-rs.git; \
    git clone https://github.com/smithy-lang/smithy-rs.git --depth 1; \
    cd smithy-rs; \
    if [[ "${checkout_smithy_rs_tools}" == "true" ]]; then \
        git checkout ${smithy_rs_commit_hash}; \
    else \
        # Run the gradle wrapper with no args to download the gradle binary and cache it in the image
        ./gradlew; \
        cd ..; \
        rm -rf smithy-rs; \
    fi; \

    cargo install --locked --path tools/ci-build/changelogger; \
    cargo install --locked --path tools/ci-build/crate-hasher; \
    cargo install --locked --path tools/ci-build/difftags; \