diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..f98663de --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git +.task +build +**/target +packages-* diff --git a/.github/workflows/build-dependency-image.yaml b/.github/workflows/build-dependency-image.yaml new file mode 100644 index 00000000..a2e0d058 --- /dev/null +++ b/.github/workflows/build-dependency-image.yaml @@ -0,0 +1,154 @@ +# Publishes the content-addressed build-env image used by package builds. +# Pull requests build the image locally without publishing it. +# +# Flow: +# plan derive the env- image reference and decide whether to build +# build validate or publish per-architecture images +# manifest publish the multi-architecture env- tag + +name: "build-dependency-image" + +on: + pull_request: + paths: + - ".dockerignore" + - ".github/workflows/build-dependency-image.yaml" + - "taskfile.yaml" + - "taskfiles/**" + - "tools/build-packages/build-dependency-image.sh" + - "tools/build-packages/dependency-image/**" + - "tools/yscope-dev-utils" + workflow_call: + outputs: + image: + description: "Fully qualified dependency image reference" + value: "${{jobs.plan.outputs.image}}" + workflow_dispatch: + +concurrency: + # Keep direct and reusable-workflow invocations in the same concurrency group. + group: "build-dependency-image-${{github.ref}}" + cancel-in-progress: true + +env: + IMAGE_NAME: "build-env" + +jobs: + plan: + runs-on: "ubuntu-24.04" + permissions: + contents: "read" + packages: "read" + outputs: + image: "${{steps.image.outputs.ref}}" + needs-build: "${{steps.decide.outputs.needs-build}}" + steps: + - name: "Check out repository" + uses: "actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0" # v7.0.0 + with: + persist-credentials: false + + - name: "Resolve dependency image" + id: "image" + run: |- + source tools/build-packages/dependency-image/utils.sh + build_env_hash=$(derive_build_env_hash) + image_repo="ghcr.io/$(printf '%s' "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]')" + image=$(image_ref "${image_repo}" "${IMAGE_NAME}" "${build_env_hash}") + echo "ref=${image}" >> "$GITHUB_OUTPUT" + + - name: "Log in to GHCR" + if: "github.event_name != 'pull_request'" + uses: "docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0" # v4.4.0 + with: + registry: "ghcr.io" + username: "${{github.actor}}" + password: "${{secrets.GITHUB_TOKEN}}" + + - name: "Decide whether to build" + id: "decide" + env: + IMAGE: "${{steps.image.outputs.ref}}" + run: |- + if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then + echo "::notice::Pull request validation will build ${IMAGE} locally without publishing" + needs_build=true + elif docker buildx imagetools inspect "${IMAGE}" &>/dev/null; then + echo "::notice::${IMAGE} already exists — skipping rebuild" + needs_build=false + else + echo "::notice::${IMAGE} not found — will build" + needs_build=true + fi + echo "needs-build=${needs_build}" >> "$GITHUB_OUTPUT" + + build: + needs: "plan" + if: "needs.plan.outputs.needs-build == 'true'" + permissions: + contents: "read" + packages: "write" + strategy: + fail-fast: false + matrix: + include: + - arch: "amd64" + runner: "ubuntu-24.04" + - arch: "arm64" + runner: "ubuntu-24.04-arm" + runs-on: "${{matrix.runner}}" + timeout-minutes: 360 + + steps: + - name: "Check out repository" + uses: "actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0" # v7.0.0 + with: + persist-credentials: false + + - name: "Log in to GHCR" + if: "github.event_name != 'pull_request'" + uses: "docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0" # v4.4.0 + with: + registry: "ghcr.io" + username: "${{github.actor}}" + password: "${{secrets.GITHUB_TOKEN}}" + + - name: "Set up Docker Buildx" + uses: "docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c" # v4.2.0 + + - name: "Build per-arch dependency image" + env: + IMAGE: "${{needs.plan.outputs.image}}" + ARCH: "${{matrix.arch}}" + run: |- + source tools/build-packages/dependency-image/utils.sh + output="--push" + if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then + output="--load" + fi + build_image "${IMAGE}-${ARCH}" "linux/${ARCH}" "${output}" + + manifest: + needs: ["plan", "build"] + if: "github.event_name != 'pull_request' && needs.plan.outputs.needs-build == 'true'" + permissions: + packages: "write" + runs-on: "ubuntu-24.04" + timeout-minutes: 15 + + steps: + - name: "Log in to GHCR" + uses: "docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0" # v4.4.0 + with: + registry: "ghcr.io" + username: "${{github.actor}}" + password: "${{secrets.GITHUB_TOKEN}}" + + - name: "Create multi-arch manifest" + env: + IMAGE: "${{needs.plan.outputs.image}}" + run: |- + docker buildx imagetools create \ + --tag "${IMAGE}" \ + "${IMAGE}-amd64" \ + "${IMAGE}-arm64" diff --git a/taskfile.yaml b/taskfile.yaml index 96705498..b6dbf50f 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -6,7 +6,8 @@ includes: velox-connector: "taskfiles/velox-connector/main.yaml" vars: - G_BUILD_DIR: "{{.ROOT_DIR}}/build" + DEFAULT_CLP_PLUGIN_BUILD_DIR: "{{.ROOT_DIR}}/build" + G_BUILD_DIR: '{{env "CLP_PLUGIN_BUILD_DIR" | default .DEFAULT_CLP_PLUGIN_BUILD_DIR}}' tasks: clean: diff --git a/taskfiles/velox-connector/deps.yaml b/taskfiles/velox-connector/deps.yaml index 438fdd51..6b2985bf 100644 --- a/taskfiles/velox-connector/deps.yaml +++ b/taskfiles/velox-connector/deps.yaml @@ -124,7 +124,6 @@ tasks: - task: "yscope-dev-utils:cmake:build" vars: BUILD_DIR: "{{.BUILD_DIR}}" - JOBS: "{{.G_CPP_MAX_PARALLELISM_PER_BUILD_TASK}}" - task: "yscope-dev-utils:cmake:install" vars: BUILD_DIR: "{{.BUILD_DIR}}" diff --git a/tools/build-packages/build-dependency-image.sh b/tools/build-packages/build-dependency-image.sh new file mode 100755 index 00000000..4c73c465 --- /dev/null +++ b/tools/build-packages/build-dependency-image.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +# Resolves this repo's dependency-image reference for local builds. +# Checks local Docker first, then GHCR, then builds a host-arch image. +# +# Prints the resolved image reference to stdout. +# +# Example: +# image=$(tools/build-packages/build-dependency-image.sh) +# docker run --rm -v "$(pwd):/src" -w /src "${image}" \ +# task velox-connector:build +# +# Requires: docker (with buildx), git, sha256sum. + +set -o errexit +set -o nounset +set -o pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +source "${script_dir}/dependency-image/utils.sh" + +# Derive this repo's GHCR namespace from its GitHub origin remote. +image_repo_from_origin() { + local remote_url owner_repo + remote_url="$(git -C "${_REPO_ROOT}" remote get-url origin)" + case "${remote_url}" in + https://github.com/*) owner_repo="${remote_url#https://github.com/}" ;; + git@github.com:*) owner_repo="${remote_url#git@github.com:}" ;; + ssh://git@github.com/*) owner_repo="${remote_url#ssh://git@github.com/}" ;; + *) + echo >&2 "ERROR: can't derive GHCR image repo from origin remote: ${remote_url}" + echo >&2 " Expected a github.com remote." + exit 1 + ;; + esac + owner_repo="${owner_repo%.git}" + printf 'ghcr.io/%s\n' "$(printf '%s' "${owner_repo}" | tr '[:upper:]' '[:lower:]')" +} + +host_platform() { + case "$(uname -m)" in + x86_64) printf 'linux/amd64\n' ;; + aarch64|arm64) printf 'linux/arm64\n' ;; + *) echo >&2 "ERROR: unsupported host arch: $(uname -m)"; exit 1 ;; + esac +} + +main() { + local build_env_hash image image_repo platform pull_err + + echo >&2 "==> Deriving build-env hash..." + build_env_hash="$(derive_build_env_hash)" + image_repo="$(image_repo_from_origin)" + image="$(image_ref "${image_repo}" "build-env" "${build_env_hash}")" + platform="$(host_platform)" + + echo >&2 " build-env hash: ${build_env_hash}" + echo >&2 " image: ${image}" + + if docker image inspect "${image}" &>/dev/null; then + echo >&2 "==> Found in local Docker cache." + echo "${image}" + return + fi + + echo >&2 "==> Checking repository registry..." + if pull_err="$(docker pull "${image}" 2>&1)"; then + echo >&2 "==> Pulled from repository registry." + echo "${image}" + return + fi + + echo >&2 " docker pull failed; will build from scratch. Pull error:" + printf '%s\n' "${pull_err}" | sed 's/^/ /' >&2 + + echo >&2 "==> Image not available — building from scratch..." + build_image "${image}" "${platform}" "--load" + echo >&2 "==> Built locally." + echo "${image}" +} + +main diff --git a/tools/build-packages/dependency-image/Dockerfile b/tools/build-packages/dependency-image/Dockerfile new file mode 100644 index 00000000..b08cec20 --- /dev/null +++ b/tools/build-packages/dependency-image/Dockerfile @@ -0,0 +1,50 @@ +# syntax=docker/dockerfile:1 + +FROM quay.io/pypa/manylinux_2_28:latest + +ARG HOST_CA_BUNDLE=/run/secrets/host-ca +ARG HOST_CA_ENV=/usr/local/share/clp-plugin-presto-connector/use-host-ca.sh + +# When available, expose the host CA bundle only to networked build steps via +# tool-specific environment variables. This supports local corporate builds +# without baking host CA certificates into the final image. +COPY tools/build-packages/dependency-image/use-host-ca.sh ${HOST_CA_ENV} + +# Install OS packages missing from the manylinux base image. +# dnf talks to libcurl directly and ignores CURL_CA_BUNDLE/SSL_CERT_FILE, so +# it needs its own --setopt=sslcacert pointed at the host CA bundle. +RUN --mount=type=bind,from=host-ca,source=host-ca,target=${HOST_CA_BUNDLE} \ + . "${HOST_CA_ENV}" \ + && dnf_sslcacert_opt="" \ + && if [ -s "${HOST_CA_BUNDLE}" ]; then \ + dnf_sslcacert_opt="--setopt=sslcacert=${HOST_CA_BUNDLE}"; \ + fi \ + && dnf install -y --setopt=install_weak_deps=False ${dnf_sslcacert_opt} \ + dpkg gettext git java-17-openjdk-devel libcurl-devel libevent-devel \ + libunwind-devel ninja-build openssl-devel patchelf python3-pip rpm-build \ + && dnf clean all + +RUN --mount=type=bind,from=host-ca,source=host-ca,target=${HOST_CA_BUNDLE} \ + . "${HOST_CA_ENV}" \ + && python3 -m pip install --no-cache-dir 'pip==21.3.1' \ + && python3 -m pip install --no-cache-dir 'cmake==3.28.*' + +ARG GO_TASK_VERSION=v3.48.0 +ARG TARGETARCH +RUN --mount=type=bind,from=host-ca,source=host-ca,target=${HOST_CA_BUNDLE} \ + set -euxo pipefail; \ + . "${HOST_CA_ENV}"; \ + mkdir -p /opt/go-task/bin; \ + curl -fsSL "https://github.com/go-task/task/releases/download/${GO_TASK_VERSION}/task_linux_${TARGETARCH}.tar.gz" \ + | tar xz -C /opt/go-task/bin task + +ENV PATH=/opt/go-task/bin:${PATH} + +ENV CLP_PLUGIN_BUILD_DIR=/opt/clp-plugin-presto-connector/build +ENV TASK_TEMP_DIR=/opt/clp-plugin-presto-connector/.task + +# Build one dependency at a time to avoid excessive memory usage from parallel builds. +RUN --mount=type=bind,source=.,target=/repo,readonly \ + --mount=type=bind,from=host-ca,source=host-ca,target=${HOST_CA_BUNDLE} \ + . "${HOST_CA_ENV}" \ + && task --concurrency 1 -d /repo velox-connector:deps:install-all diff --git a/tools/build-packages/dependency-image/use-host-ca.sh b/tools/build-packages/dependency-image/use-host-ca.sh new file mode 100644 index 00000000..5cc9a740 --- /dev/null +++ b/tools/build-packages/dependency-image/use-host-ca.sh @@ -0,0 +1,7 @@ +if [ -s "${HOST_CA_BUNDLE}" ]; then + export CURL_CA_BUNDLE="${HOST_CA_BUNDLE}" + export GIT_SSL_CAINFO="${HOST_CA_BUNDLE}" + export PIP_CERT="${HOST_CA_BUNDLE}" + export REQUESTS_CA_BUNDLE="${HOST_CA_BUNDLE}" + export SSL_CERT_FILE="${HOST_CA_BUNDLE}" +fi diff --git a/tools/build-packages/dependency-image/utils.sh b/tools/build-packages/dependency-image/utils.sh new file mode 100644 index 00000000..34230572 --- /dev/null +++ b/tools/build-packages/dependency-image/utils.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash + +# Shared dependency-image helpers for tag derivation and Docker builds. +# Callers decide whether to use a local image, pull from GHCR, or build. + +_REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &>/dev/null && pwd)" + +# Keep local builds working when the checkout lacks initialized submodules. +ensure_yscope_dev_utils_submodule() { + git -C "${_REPO_ROOT}" submodule update --init --recursive tools/yscope-dev-utils +} + +# ── Image identity ──────────────────────────────────────────────────────────── + +# Builds the canonical `:env-` image reference. +# +# Args: +image_ref() { + echo "$1/$2:env-$3" +} + +# Inputs that should change the build-env image tag. +_BUILD_ENV_HASH_INPUTS=( + ".dockerignore" + ".github/workflows/build-dependency-image.yaml" + "taskfile.yaml" + "taskfiles" + "tools/build-packages/build-dependency-image.sh" + "tools/build-packages/dependency-image" + "tools/yscope-dev-utils" +) + +# Computes the 16-hex-char hash used in the image tag. +# +# Requires: git, sha256sum +derive_build_env_hash() { + ( + cd "${_REPO_ROOT}" || exit + ensure_yscope_dev_utils_submodule >&2 + git ls-files -z --recurse-submodules -- "${_BUILD_ENV_HASH_INPUTS[@]}" \ + | LC_ALL=C sort -z \ + | xargs -0 sha256sum \ + | sha256sum \ + | cut -c1-16 + ) +} + +# Stages the host CA bundle into the temporary Docker build context. +# +# Args: +_stage_host_ca_bundle() { + local dest="${1:?_stage_host_ca_bundle requires a destination path}" + local ca_bundle_candidates=( + "${SSL_CERT_FILE:-}" + /etc/ssl/certs/ca-certificates.crt + /etc/pki/tls/certs/ca-bundle.crt + /etc/ssl/cert.pem + ) + + local src + for src in "${ca_bundle_candidates[@]}"; do + [[ -f "${src}" && -s "${src}" ]] || continue + echo >&2 "==> Staging host CA bundle: ${src} -> ${dest}" + if ! cp "${src}" "${dest}"; then + echo >&2 "ERROR: failed to stage host CA bundle: ${src}" + return 1 + fi + return 0 + done + + echo >&2 "==> No host CA bundle found; continuing without host CA context." + return 1 +} + +# ── Docker build ────────────────────────────────────────────────────────────── + +# Builds the dependency image. +# +# Args: +# $1 image tag — e.g. ghcr.io/owner/build-env:env- +# $2 platform — linux/amd64 or linux/arm64 +# $3 output flag — --push (registry) or --load (local docker) +# +# Requires: docker buildx, git +build_image() { + local tag="$1" platform="$2" output="$3" + + # Expose the host CA bundle as a narrow named build context so the Dockerfile + # can bind-mount it during networked RUN steps without baking it into image + # layers. Use a real context instead of a BuildKit secret because corporate CA + # bundles can exceed BuildKit's 500KiB secret limit. + local ca_stage; ca_stage=$(mktemp -d) + ( + trap 'rm -rf "${ca_stage}"' EXIT + + local ca_bundle="${ca_stage}/host-ca" + _stage_host_ca_bundle "${ca_bundle}" || : > "${ca_bundle}" + + ensure_yscope_dev_utils_submodule + + docker buildx build \ + --platform "${platform}" \ + --build-context "host-ca=${ca_stage}" \ + --tag "${tag}" \ + "${output}" \ + -f "${_REPO_ROOT}/tools/build-packages/dependency-image/Dockerfile" \ + "${_REPO_ROOT}" + ) +}