From 3e1754daa0b73d4d517ba52998cdefc911108b59 Mon Sep 17 00:00:00 2001 From: Ido David Date: Fri, 3 Nov 2023 14:44:04 -0400 Subject: [PATCH] Add new ud to perform an op[timized deep clone --- Earthfile | 5 ++++- git/Earthfile | 32 ++++++++++++++++++++++++++++++ git/tests/Earthfile | 48 +++++++++++++++++++++++++++++++++++++++++++++ ssh/Earthfile | 48 +++++---------------------------------------- ssh/tests/Earthfile | 38 +++++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 44 deletions(-) create mode 100644 git/Earthfile create mode 100644 git/tests/Earthfile create mode 100644 ssh/tests/Earthfile diff --git a/Earthfile b/Earthfile index 405675a..8fa5011 100644 --- a/Earthfile +++ b/Earthfile @@ -11,8 +11,11 @@ install-dind-script: SAVE ARTIFACT ./install-dind.sh test: + FROM alpine:3.18 BUILD +test-install-dind-amd64 - BUILD ./ssh+test-add-known-hosts + FOR dir IN "ssh git" + BUILD ./$dir+test + END test-install-dind-amd64: BUILD --platform=linux/amd64 +test-install-dind-for-image \ diff --git a/git/Earthfile b/git/Earthfile new file mode 100644 index 0000000..4004361 --- /dev/null +++ b/git/Earthfile @@ -0,0 +1,32 @@ +VERSION --arg-scope-and-set 0.7 + +# DEEP_CLONE deep clones the provided git repository +DEEP_CLONE: + COMMAND + ARG USE_GIT_ENV="false" + IF [ $USE_GIT_ENV = "true" ] + FROM alpine/git:v2.40.1 + END + DO ../ssh+ADD_KNOWN_HOSTS + ARG --required GIT_URL + ARG DEST_DIR + LET dest_dir=$DEST_DIR + IF [ -z $dest_dir ] + SET dest_dir=$(basename ${GIT_URL%.git}) + END + GIT CLONE $GIT_URL $dest_dir + WORKDIR $dest_dir + RUN git remote set-url origin $GIT_URL + ARG git_hash=$(git rev-parse HEAD) + ARG SECRET_PATH + IF [ -z $SECRET_PATH ] + RUN echo secrets path is z + RUN --ssh git fetch --unshallow + ELSE + RUN echo secrets path is NOT z + RUN --mount=type=secret,id=$SECRET_PATH,mode=0400,target=/root/.ssh/id_rsa \ + git fetch --unshallow + END + +test: + BUILD ./tests+all \ No newline at end of file diff --git a/git/tests/Earthfile b/git/tests/Earthfile new file mode 100644 index 0000000..a40ffaf --- /dev/null +++ b/git/tests/Earthfile @@ -0,0 +1,48 @@ +VERSION --arg-scope-and-set --pass-args 0.7 + +PROJECT earthly-technologies/core + +all: + BUILD --platform=linux/amd64 --platform=linux/arm64 +test-deep-clone-image \ + --base_image=alpine/git:latest \ + --base_image=alpine:latest \ + --base_image=debian:stable \ + --base_image=debian:stable-slim \ + --base_image=ubuntu:latest \ + --base_image=amazonlinux:1 \ + --base_image=amazonlinux:2 \ + --GIT_URL=https://github.com/earthly/lib.git \ + --GIT_URL=git@github.com:earthly/cloud.git \ + --GIT_URL=git@github.com:earthly/cloud \ + --SECRET_PATH="" \ + --SECRET_PATH="littleredcorvette-id_rsa" \ + --DEST_DIR="" \ + --DEST_DIR="some-other-dir" + +test-deep-clone-image: + ARG --required base_image + ARG TARGETPLATFORM + FROM alpine + IF [ "$base_image" = "amazonlinux:1" ] && [ "$TARGETPLATFORM" = "linux/arm64" ] # no amazonlinux:1 for arm64, skipping + RUN echo skipping $base_image with platform $TARGETPLATFORM + ELSE + FROM "$base_image" + LET use_git_env="true" + IF [ "${base_image%:*}" = "alpine/git" ] + SET use_git_env="false" + RUN apk add git + ELSE IF [ "${base_image%:*}" = "alpine" ] + RUN apk add git + ELSE IF [ "${base_image%:*}" = "debian" ] + RUN apt update && apt install -y git + ELSE IF [ "${base_image%:*}" = "ubuntu" ] + RUN apt-get update && apt-get -y install git + ELSE IF [[ $base_image == amazonlinux* ]] + RUN yum -y install git + END + DO --pass-args ..+DEEP_CLONE --USE_GIT_ENV=$use_git_env + IF [ -n $DEST_DIR ] + WORKDIR $DEST_DIR + END + RUN git checkout main + END diff --git a/ssh/Earthfile b/ssh/Earthfile index 9d290ae..a0cd61f 100644 --- a/ssh/Earthfile +++ b/ssh/Earthfile @@ -1,6 +1,6 @@ -VERSION --pass-args --arg-scope-and-set 0.7 +VERSION 0.7 -# ADD_KNOWN_HOSTS will append some known public keys into the $target_file +# ADD_KNOWN_HOSTS appends some known public keys into the $target_file ADD_KNOWN_HOSTS: COMMAND ARG target_file=~/.ssh/known_hosts @@ -11,47 +11,9 @@ ADD_KNOWN_HOSTS: # known-hosts is used to copy the known_hosts file into the build context (internal use) known-hosts: - FROM alpine + FROM alpine:3.18 COPY known_hosts . SAVE ARTIFACT known_hosts -####################### -# Tests: -####################### - -test-add-known-hosts: - BUILD --platform=linux/amd64 +test-add-known-hosts-image \ - --base_image=alpine:latest \ - --base_image=debian:stable \ - --base_image=debian:stable-slim \ - --base_image=ubuntu:latest \ - --base_image=amazonlinux:1 \ - --base_image=amazonlinux:2 \ - --target_file=~/to_interpolate/known_hosts \ - --target_file=no_dir_new_known_hosts \ - --target_file=/some/dir/to/file/new_known_hosts \ - --target_file=existing_known_hosts # this will be handled in the test target - -test-add-known-hosts-image: - ARG --required base_image - ARG TARGETPLATFORM - FROM alpine - IF [ "$base_image" = "amazonlinux:1" ] && [ "$TARGETPLATFORM" = "linux/arm64" ] # no amazonlinux:1 for arm64, skipping - RUN echo skipping $base_image with platform $TARGETPLATFORM - ELSE - FROM "$base_image" - IF [ "$base_image" = "amazonlinux:1" ] - RUN yum -y install diffutils.x86_64 - END - COPY ./known_hosts /tmp/expected-temp - RUN test -s /tmp/expected-temp - ARG target_file - IF [ "$target_file" = "existing_known_hosts" ] - RUN echo some-key >> /tmp/expected - RUN echo some-key >> $target_file - END - RUN cat /tmp/expected-temp >> /tmp/expected - DO --pass-args +ADD_KNOWN_HOSTS - LET expanded_target_file="$(eval echo $target_file)" - RUN diff /tmp/expected $(eval echo $expanded_target_file) - END +test: + BUILD ./tests+all \ No newline at end of file diff --git a/ssh/tests/Earthfile b/ssh/tests/Earthfile new file mode 100644 index 0000000..58d6241 --- /dev/null +++ b/ssh/tests/Earthfile @@ -0,0 +1,38 @@ +VERSION --pass-args --arg-scope-and-set 0.7 + +all: + BUILD --platform=linux/amd64 --platform=linux/arm64 +test-add-known-hosts-image \ + --base_image=alpine:latest \ + --base_image=debian:stable \ + --base_image=debian:stable-slim \ + --base_image=ubuntu:latest \ + --base_image=amazonlinux:1 \ + --base_image=amazonlinux:2 \ + --target_file=~/to_interpolate/known_hosts \ + --target_file=no_dir_new_known_hosts \ + --target_file=/some/dir/to/file/new_known_hosts \ + --target_file=existing_known_hosts # this will be handled in the test target + +test-add-known-hosts-image: + ARG --required base_image + ARG TARGETPLATFORM + FROM alpine + IF [ "$base_image" = "amazonlinux:1" ] && [ "$TARGETPLATFORM" = "linux/arm64" ] # no amazonlinux:1 for arm64, skipping + RUN echo skipping $base_image with platform $TARGETPLATFORM + ELSE + FROM "$base_image" + IF [ "$base_image" = "amazonlinux:1" ] + RUN yum -y install diffutils.x86_64 + END + COPY ..+known-hosts/known_hosts /tmp/expected-temp + RUN test -s /tmp/expected-temp + ARG target_file + IF [ "$target_file" = "existing_known_hosts" ] + RUN echo some-key >> /tmp/expected + RUN echo some-key >> $target_file + END + RUN cat /tmp/expected-temp >> /tmp/expected + DO --pass-args ..+ADD_KNOWN_HOSTS + LET expanded_target_file="$(eval echo $target_file)" + RUN diff /tmp/expected $(eval echo $expanded_target_file) + END