From b015fb2e6a4ac1595dee99615b4c364739469a2e Mon Sep 17 00:00:00 2001
From: Sheogorath <sheogorath@shivering-isles.com>
Date: Thu, 7 Sep 2023 01:53:53 +0200
Subject: [PATCH] Switch to earthly

---
 .gitlab-ci.yml | 28 +++++++++++++++++-----------
 Dockerfile     | 18 ------------------
 Earthfile      | 37 +++++++++++++++++++++++++++++++++++++
 renovate.json  | 12 ++++++++++--
 4 files changed, 64 insertions(+), 31 deletions(-)
 delete mode 100644 Dockerfile
 create mode 100644 Earthfile

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 1164892..a9b7d26 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,13 +1,19 @@
 include:
-  - project: 'container-library/build-ah-engine'
-    ref: 2.1.4
-    file: '/gitlab-ci-template.yml'
+  - project: 'shivering-isles/infrastructure-gitops'
+    ref: main
+    file: '/images/.utils/gitlab-earthly.yaml'
 
-variables:
-  CI_CONTAINER_BUILD_ARCHS: "amd64"
-
-container-tagging:
-  before_script:
-    - podman login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
-    - export BUILD_DATE="$(date --rfc-3339 ns)"
-    - export CI_REGISTRY_IMAGE_VERSION=$(grep 'ARG DNSPROXY_VERSION=' Dockerfile | sed -e 's/^ARG DNSPROXY_VERSION=v//g')
+build:
+  extends: .earthly
+  script:
+    - |
+      if [ -n "$CI_OCI_CACHE" ]; then
+        export ARGS="${ARGS} --remote-cache=$CI_OCI_CACHE"
+      fi
+    - |
+      if [ $CI_COMMIT_BRANCH = $CI_DEFAULT_BRANCH ]; then
+        export BUILD_ARGS="${BUILD_ARGS} --latest=true"
+      else
+        export BUILD_ARGS="${BUILD_ARGS} --tag=$CI_COMMIT_REF_SLUG --latest=false"
+      fi
+    - earthly --ci --allow-privileged --push ${ARGS} +container --registry=${CI_REGISTRY_IMAGE} ${BUILD_ARGS}
diff --git a/Dockerfile b/Dockerfile
deleted file mode 100644
index 39b65f2..0000000
--- a/Dockerfile
+++ /dev/null
@@ -1,18 +0,0 @@
-FROM docker.io/library/golang:1.21.0 as build-env
-
-# renovate: datasource=git-tags depName=https://git.shivering-isles.com/github-mirror/AdguardTeam/dnsproxy.git
-ARG DNSPROXY_VERSION=v0.54.0
-ENV DNSPROXY_VERSION=${DNSPROXY_VERSION}
-ENV CGO_ENABLED=0
-
-RUN true \
-    && git clone https://git.shivering-isles.com/github-mirror/AdguardTeam/dnsproxy.git --depth 1 -b "$DNSPROXY_VERSION" \
-    && cd dnsproxy \
-    && go build -mod=vendor
-
-
-FROM gcr.io/distroless/static
-
-COPY --from=build-env /go/dnsproxy/dnsproxy /
-CMD ["/dnsproxy"]
-
diff --git a/Earthfile b/Earthfile
new file mode 100644
index 0000000..61ce6d0
--- /dev/null
+++ b/Earthfile
@@ -0,0 +1,37 @@
+VERSION 0.7
+
+# build creates a binary release of dnsproxy
+build:
+    FROM git.shivering-isles.com/shivering-isles/infrastructure-gitops/images/mirror+golang
+    WORKDIR /go-workdir
+    ARG DNSPROXY_VERSION
+    ENV CGO_ENABLED=0
+
+    GIT CLONE --keep-ts --branch "${DNSPROXY_VERSION}" https://git.shivering-isles.com/github-mirror/AdguardTeam/dnsproxy.git ./
+
+    RUN go build -mod=vendor
+    SAVE ARTIFACT ./dnsproxy AS LOCAL local-output/dnsproxy
+
+container-internal:
+    FROM git.shivering-isles.com/shivering-isles/infrastructure-gitops/images/mirror+distroless-static
+    # renovate: datasource=git-tags depName=https://git.shivering-isles.com/github-mirror/AdguardTeam/dnsproxy.git
+    ARG DNSPROXY_VERSION
+    ENV DNSPROXY_VERSION=${DNSPROXY_VERSION}
+
+    COPY (+build/dnsproxy --DNSPROXY_VERSION=${DNSPROXY_VERSION}) /dnsproxy
+    CMD ["/dnsproxy"]
+
+# container builds the dnsproxy container
+container:
+    FROM git.shivering-isles.com/shivering-isles/infrastructure-gitops/images/mirror+golang
+    # renovate: datasource=git-tags depName=https://git.shivering-isles.com/github-mirror/AdguardTeam/dnsproxy.git
+    ARG DNSPROXY_VERSION=v0.54.0
+    ARG registry=quay.io/shivering-isles/dnsproxy
+    ARG tag=${DNSPROXY_VERSION}
+    ARG latest=false
+    IF [ $latest = "true" ]
+        FROM +container-internal --DNSPROXY_VERSION=${DNSPROXY_VERSION}
+        SAVE IMAGE --push ${registry}:latest
+    END
+    FROM +container-internal --DNSPROXY_VERSION=${DNSPROXY_VERSION}
+    SAVE IMAGE --push ${registry}:${tag}
diff --git a/renovate.json b/renovate.json
index 1091398..a9abb3d 100644
--- a/renovate.json
+++ b/renovate.json
@@ -1,7 +1,15 @@
 {
   "extends": [
-    "config:base",
-    "regexManagers:dockerfileVersions"
+    "config:base"
+  ],
+  "regexManager": [
+    {
+      "fileMatch": ["Dockerfile$","Earthfile$"],
+      "matchStrings": [
+        "#\\s*renovate:\\s*datasource=(?<datasource>.*?) depName=(?<depName>.*?)( versioning=(?<versioning>.*?))?\\sENV .*?_VERSION=\"(?<currentValue>.*)\"\\s"
+      ],
+      "versioningTemplate": "{{#if versioning}}{{{versioning}}}{{else}}semver{{/if}}"
+    }
   ],
   "prConcurrentLimit": 5,
   "automerge": true,
-- 
GitLab