imap-rules 0.2.4

A utility for running rules against an IMAP server
# ---- Builder Stage ----
# We don't use --platform=$BUILDPLATFORM here to let Docker run the builder natively
# (via emulation if necessary) for the target architecture, which simplifies
# cross-compilation for musl targets.
FROM rust:slim-bookworm AS builder

# Install musl-tools, pkg-config, perl and make for static linking and building OpenSSL from source
RUN apt-get update && apt-get install -y musl-tools pkg-config libssl-dev perl make && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

# Copy source code
COPY . .

# Use TARGETARCH and TARGETVARIANT to determine the correct Rust target
ARG TARGETARCH
ARG TARGETVARIANT

# Build a statically-linked, release-optimized binary for the target architecture
RUN case "${TARGETARCH}" in \
    "amd64") \
        RUST_TARGET="x86_64-unknown-linux-musl"; \
        if [ "${TARGETVARIANT}" = "v3" ]; then \
            export RUSTFLAGS="-C target-cpu=x86-64-v3"; \
        fi \
        ;; \
    "arm64") \
        RUST_TARGET="aarch64-unknown-linux-musl" \
        ;; \
    "arm") \
        case "${TARGETVARIANT}" in \
            "v7") RUST_TARGET="armv7-unknown-linux-musleabihf" ;; \
            "v6") RUST_TARGET="arm-unknown-linux-musleabi" ;; \
            *) echo "Unsupported ARM variant: ${TARGETVARIANT}" && exit 1 ;; \
        esac \
        ;; \
    *) \
        echo "Unsupported architecture: ${TARGETARCH}" && exit 1 \
        ;; \
    esac && \
    rustup target add "${RUST_TARGET}" && \
    cargo build --target "${RUST_TARGET}" --release && \
    cp target/${RUST_TARGET}/release/imap-rules /usr/src/app/imap-rules

# ---- Final Stage ----
FROM alpine:3.23

# Install ca-certificates to allow connecting to TLS-enabled IMAP servers
RUN apk add --no-cache ca-certificates

# Copy the compiled binary from the builder stage
COPY --from=builder /usr/src/app/imap-rules /usr/local/bin/imap-rules

# Set the entrypoint for the container.
# Using exec form for ENTRYPOINT allows the container to receive arguments.
ENTRYPOINT ["/usr/local/bin/imap-rules"]