Skip to content

Commit

Permalink
run cross inside earthly
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed Feb 6, 2024
1 parent d5d557f commit c300bbc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 72 deletions.
1 change: 1 addition & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
Earthfile
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push the images
run: earthly --push --no-output +docker-multiplatform --build=cross --tag=${{ github.ref_name }}
run: earthly --push --no-output +docker-multiplatform --tag=${{ github.ref_name }}
111 changes: 40 additions & 71 deletions Earthfile
Original file line number Diff line number Diff line change
@@ -1,97 +1,66 @@
VERSION --global-cache 0.7
IMPORT github.com/earthly/lib/rust AS rust

rust:
FROM rust:alpine
RUN apk add --no-cache musl-dev findutils
ARG --global CROSS_VERSION=0.2.5

cross-deps:
ARG NATIVEPLATFORM
FROM --platform=$NATIVEPLATFORM rust:slim
RUN apt-get update \
&& apt-get install -y docker.io jq wget \
&& apt-get clean
WORKDIR /app
DO rust+INIT --keep_fingerprints=true
# DO rust+CARGO --args="install cross@${CROSS_VERSION}"
RUN wget -O- "https://github.com/cross-rs/cross/releases/download/v${CROSS_VERSION}/cross-x86_64-unknown-linux-musl.tar.gz" | tar -xzf - -C /usr/local/bin
DO rust+SET_CACHE_MOUNTS_ENV

build:
FROM +rust
COPY docker/target.sh /
COPY --keep-ts . ./
DO rust+CARGO --args="build --release" --output="release/[^/\.]+"
SAVE ARTIFACT target/release/yage

prebuilt:
LOCALLY
ARG --required platform_slug
ARG --required target
RUN mkdir -p bin \
&& cp target/$target/release/yage bin/yage-$platform_slug
DO rust+CARGO --args="fetch"

cross:
LOCALLY
ARG --required platform_slug
ARG --required target
RUN rm -rf target/release \
&& cross build --target $target --release \
&& mkdir -p bin \
&& cp target/$target/release/yage bin/yage-$platform_slug

cross-dind:
FROM rust:alpine
RUN apk add --no-cache musl-dev findutils
DO rust+INIT --keep_fingerprints=true
DO rust+SET_CACHE_MOUNTS_ENV
DO rust+CARGO --args="install cross"
RUN apk add docker jq
ARG --required target
COPY --keep-ts . ./
WITH DOCKER
FROM +cross-deps
ARG TARGETPLATFORM
ARG target=$(/target.sh $TARGETPLATFORM)
# RUN rustup target add $target
WITH DOCKER --pull ghcr.io/cross-rs/$target:$CROSS_VERSION
RUN --mount=$EARTHLY_RUST_CARGO_HOME_CACHE \
--mount=$EARTHLY_RUST_TARGET_CACHE \
cross build --target $target --release
END
DO rust+COPY_OUTPUT --output="[^\./]+/release/[^\./]+"

artifact:
FROM alpine
ARG TARGETPLATFORM
RUN echo "Building for $TARGETPLATFORM"
ARG platform_slug=$(echo $TARGETPLATFORM | tr / -)
ARG build=earthly
COPY platform.sh /platform.sh
ARG target=$(/platform.sh $TARGETPLATFORM)
IF [ "$build" = "earthly" ]
COPY +build/yage /yage
ELSE IF [ "$build" = "prebuilt" ]
WAIT
BUILD +prebuilt --platform_slug=$platform_slug --target=$target
END
COPY bin/yage-$platform_slug /yage
ELSE IF [ "$build" = "cross" ]
WAIT
BUILD +cross --platform_slug=$platform_slug --target=$target
END
COPY bin/yage-$platform_slug /yage
ELSE IF [ "$build" = "cross-dind" ]
WAIT
ARG NATIVEPLATFORM
BUILD --platform $NATIVEPLATFORM +cross --target=$target
END
COPY bin/yage-$platform_slug /yage
rm -rf target/release \
&& cross build --target $target --release
END
SAVE ARTIFACT yage
DO rust+COPY_OUTPUT --output=".+/release/[^\./]+"
SAVE ARTIFACT /app/target/$target/release/yage

docker:
FROM scratch
docker-build:
ARG from=scratch
FROM $from
WORKDIR /app
ARG build=earthly
COPY (+artifact/yage --build=$build) /yage
COPY +cross/yage /yage
# make sure we have the required dependencies in the image
# can't do that unfortunately because of https://github.com/earthly/earthly/issues/2618
# RUN ["/yage", "--help"]
ENTRYPOINT ["/yage"]
ARG tag=main
# SAVE IMAGE --push glehmann/yage:$tag
SAVE IMAGE --push ghcr.io/glehmann/yage:$tag

docker:
FROM alpine
ARG TARGETPLATFORM
COPY docker/from.sh /
ARG from=$(/from.sh $TARGETPLATFORM)
ARG tag=main
BUILD +docker-build --from=$from --tag=main

docker-multiplatform:
ARG build=earthly
ARG tag=main
BUILD \
--platform=linux/amd64 \
--platform=linux/arm64 \
--platform=linux/386 \
--platform=linux/arm/v7 \
--platform=linux/arm/v6 \
--platform=linux/ppc64le \
# --platform=linux/s390x \
+docker --build=$build
--platform=linux/s390x \
+docker --tag=$tag
14 changes: 14 additions & 0 deletions docker/from.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

case "$1" in
"linux/amd64") target='scratch' ;;
"linux/arm64") target='scratch' ;;
"linux/386") target='scratch' ;;
"linux/arm/v7") target='scratch' ;;
"linux/arm/v6") target='scratch' ;;
"linux/ppc64le") target='debian:12-slim' ;;
"linux/s390x") target='debian:12-slim' ;;
*) echo >&2 "error: unsupported $TARGETPLATFORM architecture"; exit 1 ;;
esac;

echo -n $target
File renamed without changes.

0 comments on commit c300bbc

Please sign in to comment.