diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 535455cce083..4535c1e7fddb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,8 +85,12 @@ jobs: - uses: docker/setup-qemu-action@v2 with: platforms: arm64,ppc64le + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Expose GitHub Runtime + uses: crazy-max/ghaction-github-runtime@v2 - name: Test Docker Image - run: core/docker/build.sh + run: core/docker/build.sh -c - name: Remove Trino from local Maven repo to avoid caching it # Avoid caching artifacts built in this job, cache should only include dependencies if: steps.cache.outputs.cache-hit != 'true' diff --git a/core/docker/Dockerfile b/core/docker/Dockerfile index ec91f1094a72..4363630abced 100644 --- a/core/docker/Dockerfile +++ b/core/docker/Dockerfile @@ -11,37 +11,13 @@ # See the License for the specific language governing permissions and # limitations under the License. # -FROM eclipse-temurin:17-jdk AS builder - -RUN \ - set -xeu && \ - echo 'Acquire::Retries "3";' > /etc/apt/apt.conf.d/80-retries && \ - echo 'Acquire::http::Timeout "15";' > /etc/apt/apt.conf.d/80-timeouts && \ - apt-get update -q && \ - apt-get install -y -q git gcc make && \ - git clone https://github.com/airlift/jvmkill /tmp/jvmkill && \ - make -C /tmp/jvmkill - -FROM eclipse-temurin:17-jdk - -RUN \ - set -xeu && \ - echo 'Acquire::Retries "3";' > /etc/apt/apt.conf.d/80-retries && \ - echo 'Acquire::http::Timeout "15";' > /etc/apt/apt.conf.d/80-timeouts && \ - apt-get update -q && \ - apt-get install -y -q less python3 curl && \ - rm -rf /var/lib/apt/lists/* && \ - update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \ - groupadd trino --gid 1000 && \ - useradd trino --uid 1000 --gid 1000 --create-home && \ - mkdir -p /usr/lib/trino /data/trino && \ - chown -R "trino:trino" /usr/lib/trino /data/trino +ARG TRINO_VERSION +FROM trino-base:${TRINO_VERSION}-${TARGETARCH} ARG TRINO_VERSION COPY trino-cli-${TRINO_VERSION}-executable.jar /usr/bin/trino COPY --chown=trino:trino trino-server-${TRINO_VERSION} /usr/lib/trino COPY --chown=trino:trino default/etc /etc/trino -COPY --chown=trino:trino --from=builder /tmp/jvmkill/libjvmkill.so /usr/lib/trino/bin EXPOSE 8080 USER trino:trino diff --git a/core/docker/base.Dockerfile b/core/docker/base.Dockerfile new file mode 100644 index 000000000000..e872e0174065 --- /dev/null +++ b/core/docker/base.Dockerfile @@ -0,0 +1,40 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +FROM eclipse-temurin:17-jdk AS builder + +RUN \ + set -xeu && \ + echo 'Acquire::Retries "3";' > /etc/apt/apt.conf.d/80-retries && \ + echo 'Acquire::http::Timeout "15";' > /etc/apt/apt.conf.d/80-timeouts && \ + apt-get update -q && \ + apt-get install -y -q git gcc make && \ + git clone https://github.com/airlift/jvmkill /tmp/jvmkill && \ + make -C /tmp/jvmkill + +FROM eclipse-temurin:17-jdk + +RUN \ + set -xeu && \ + echo 'Acquire::Retries "3";' > /etc/apt/apt.conf.d/80-retries && \ + echo 'Acquire::http::Timeout "15";' > /etc/apt/apt.conf.d/80-timeouts && \ + apt-get update -q && \ + apt-get install -y -q less python3 curl && \ + rm -rf /var/lib/apt/lists/* && \ + update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \ + groupadd trino --gid 1000 && \ + useradd trino --uid 1000 --gid 1000 --create-home && \ + mkdir -p /usr/lib/trino /data/trino && \ + chown -R "trino:trino" /usr/lib/trino /data/trino + +COPY --chown=trino:trino --from=builder /tmp/jvmkill/libjvmkill.so /usr/lib/trino/bin/libjvmkill.so diff --git a/core/docker/build.sh b/core/docker/build.sh index ec9c64f2034f..9a6c2e1831d5 100755 --- a/core/docker/build.sh +++ b/core/docker/build.sh @@ -4,19 +4,21 @@ set -euo pipefail usage() { cat <&2 -Usage: $0 [-h] [-a ] [-r ] +Usage: $0 [-h] [-a ] [-r ] [-c] Builds the Trino Docker image -h Display help --a Build the specified comma-separated architectures, defaults to amd64,arm64 +-a Build the specified comma-separated architectures, defaults to: amd64,arm64,ppc64le -r Build the specified Trino release version, downloads all required artifacts +-c Use Docker build cache EOF } ARCHITECTURES=(amd64 arm64 ppc64le) TRINO_VERSION= +BUILD_CACHE= -while getopts ":a:h:r:" o; do +while getopts ":a:h:r:c" o; do case "${o}" in a) IFS=, read -ra ARCHITECTURES <<< "$OPTARG" @@ -24,6 +26,9 @@ while getopts ":a:h:r:" o; do r) TRINO_VERSION=${OPTARG} ;; + c) + BUILD_CACHE="type=gha" + ;; h) usage exit 0 @@ -67,17 +72,40 @@ rm "${WORK_DIR}/trino-server-${TRINO_VERSION}.tar.gz" cp -R bin "${WORK_DIR}/trino-server-${TRINO_VERSION}" cp -R default "${WORK_DIR}/" +# Prepare an empty build context for base image to speed up context copying +BASE_WORK_DIR="$(mktemp -d)" + +BASE_PREFIX="trino-base:${TRINO_VERSION}" TAG_PREFIX="trino:${TRINO_VERSION}" +echo "๐Ÿ‘ท Docker buildx builders available:" +docker buildx ls + for arch in "${ARCHITECTURES[@]}"; do - echo "๐Ÿซ™ Building the image for $arch" - docker build \ - "${WORK_DIR}" \ - --pull \ - --platform "linux/$arch" \ - -f Dockerfile \ - -t "${TAG_PREFIX}-$arch" \ - --build-arg "TRINO_VERSION=${TRINO_VERSION}" + echo "๐Ÿซ™ Building the base image for $arch" + BASE_BUILD_OPTIONS=( + ${BASE_WORK_DIR} + --platform "linux/$arch" + --load + --tag "${BASE_PREFIX}-$arch" + -f base.Dockerfile + ) + if [ -n "${BUILD_CACHE}" ]; then + echo "๐ŸŽฏ Using Docker build cache" + SCOPE="${GITHUB_REF_NAME:-$( git rev-parse --short HEAD: )}-${arch}" + BASE_BUILD_OPTIONS+=(--cache-from "${BUILD_CACHE},scope=${SCOPE}" --cache-to "${BUILD_CACHE},scope=${SCOPE},mode=max") + else + BASE_BUILD_OPTIONS+=(--pull) + fi + docker buildx build "${BASE_BUILD_OPTIONS[@]}" + + echo "๐Ÿซ™ Building the main image for $arch" + DOCKER_BUILDKIT=1 docker build \ + "${WORK_DIR}" \ + --platform "linux/$arch" \ + -f Dockerfile \ + -t "${TAG_PREFIX}-$arch" \ + --build-arg "TRINO_VERSION=${TRINO_VERSION}" done echo "๐Ÿงน Cleaning up the build context directory"