From bc10e61f9ee20f240be70eea79cdbb647d52885d Mon Sep 17 00:00:00 2001
From: Jean-Louis Fuchs <jean-louis.fuchs@adfinis.com>
Date: Mon, 25 Dec 2023 11:09:37 +0100
Subject: [PATCH] use buildx/inline cache

---
 .github/workflows/main.yml         | 27 ++++++++++++
 .github/workflows/pull-request.yml | 36 +++++++++++++++
 .github/workflows/testing.yml      | 27 ------------
 Makefile                           | 29 ++++++++++--
 compose/Dockerfile                 | 71 ++++++++++++++++--------------
 compose/setup/version              |  1 +
 docker-compose.yml                 |  9 ----
 7 files changed, 126 insertions(+), 74 deletions(-)
 create mode 100644 .github/workflows/main.yml
 create mode 100644 .github/workflows/pull-request.yml
 delete mode 100644 .github/workflows/testing.yml
 create mode 100644 compose/setup/version

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
new file mode 100644
index 0000000..43741ee
--- /dev/null
+++ b/.github/workflows/main.yml
@@ -0,0 +1,27 @@
+name: Tests
+
+on:
+  push:
+    branches:
+      - main
+  schedule:
+    - cron: "0 0 * * 0"
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+
+    steps:
+    - name: Checkout code
+      uses: actions/checkout@v4
+
+    - name: Login to GitHub Container Registry
+      uses: docker/login-action@v3
+      with:
+        registry: ghcr.io
+        username: ${{ github.actor }}
+        password: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: Run build
+      run: |
+        make build push
diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml
new file mode 100644
index 0000000..38fa927
--- /dev/null
+++ b/.github/workflows/pull-request.yml
@@ -0,0 +1,36 @@
+name: Tests
+
+on:
+  pull_request:
+    branches:
+      - main
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+
+    steps:
+    - name: Checkout code
+      uses: actions/checkout@v2
+
+    - name: Login to GitHub Container Registry
+      uses: docker/login-action@v3
+      with:
+        registry: ghcr.io
+        username: ${{ github.actor }}
+        password: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: Set up Docker Buildx
+      uses: docker/setup-buildx-action@v3
+      with:
+        buildkitd-flags: --debug
+
+    - name: Build container
+      uses: docker/build-push-action@v5
+      with:
+        context: compose
+        push: false
+        tags: ghcr.io/adfinis/pyaptly/cache:latest
+        cache-from: type=gha
+        cache-to: type=gha,mode=max
+        build-args: BUILDKIT_INLINE_CACHE=1
diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml
deleted file mode 100644
index e6e1f33..0000000
--- a/.github/workflows/testing.yml
+++ /dev/null
@@ -1,27 +0,0 @@
-name: Tests
-
-on:
-  push:
-    branches:
-      - main
-  pull_request:
-    branches:
-      - main
-  schedule:
-    - cron: "0 0 * * 0"
-
-jobs:
-  build:
-    runs-on: ubuntu-latest
-
-    steps:
-    - name: Checkout code
-      uses: actions/checkout@v2
-
-    - name: Run build
-      run: |
-        make up push
-
-    - name: Run tests
-      run: |
-        make test
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 592979c..2772b1f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,11 @@
 .DEFAULT_GOAL := help
 
+CACHE_IMG = ghcr.io/adfinis/pyaptly/cache:latest
+
+BUILDER = d25260b82a6972131f7a2dd705b9383d417f457
+DOCKER_BUILDKIT = 1
+export DOCKER_BUILDKIT
+
 # Help target extracts the double-hash comments from the targets and shows them
 # in a convenient way. This allows us to easily document the user-facing Make
 # targets
@@ -7,14 +13,29 @@
 help:
 	@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort -k 1,1 | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
 
+.PHONY: pull
+pull: ## pull docker build cache from registry
+	@docker pull $(CACHE_IMG)
+
+
+.PHONY: build
+build: ## build image using buildx
+	docker buildx create --name $(BUILDER) --use
+	docker buildx build \
+		--cache-from type=registry,ref=ghcr.io/adfinis/pyaptly/cache:latest \
+		--cache-to type=registry,ref=ghcr.io/adfinis/pyaptly/cache:latest,mode=max \
+		-t ghcr.io/adfinis/pyaptly/cache:latest compose
+	docker buildx stop $(BUILDER)
+	docker buildx rm $(BUILDER)
+
+
 .PHONY: up
-up: ## start and build container if needed
-	#@docker pull ghcr.io/adfinis/pyaptly/cache:latest
-	@DOCKER_BUILDKIT=1 docker compose up -d --build
+up: build ## start the container (cached)
+	@docker compose up -d
 
 .PHONY: push
 push: ## push docker build cache to registry
-	@docker push ghcr.io/adfinis/pyaptly/cache:latest
+	@docker push $(CACHE_IMG)
 
 .PHONY: down
 down: ## stop and remove container 
diff --git a/compose/Dockerfile b/compose/Dockerfile
index 9b20f4d..c8db0ab 100644
--- a/compose/Dockerfile
+++ b/compose/Dockerfile
@@ -1,35 +1,38 @@
-ARG DOCKER_IMAGE
-FROM $DOCKER_IMAGE
+FROM debian:bookworm-slim
 
-RUN apt-get update && apt-get install -y --no-install-recommends \
-    python3-toml \
-    python3-requests \
-    python3-poetry \
-    gnupg \
-    bzip2 \
-    tini \
-    curl \
-    wait-for-it \
-    entr \
-    gnutls-bin \
-    nettle-dev \
-    gcc \
-    llvm-dev \
-    libclang-dev \
-    build-essential \
-    pkg-config \
-    gettext \
-    git \
-    procps \
-    psmisc \
-    vim-tiny \
-    cargo \
- && rm -rf /var/lib/apt/lists/* \
- && apt-get clean \
- && apt-get autoclean \
- && rm -rf /var/lib/apt/archives/* \
- && rm -rf /var/cache/apt/*
-ADD setup /setup
-RUN /setup/setup
-ADD run /setup/run
-ENTRYPOINT ["/usr/bin/tini", "--"]
\ No newline at end of file
+RUN echo 1 > /one
+#RUN apt-get update && apt-get install -y --no-install-recommends \
+#    python3-toml \
+#    python3-requests \
+#    python3-poetry \
+#    gnupg \
+#    bzip2 \
+#    tini \
+#    curl \
+#    wait-for-it \
+#    entr \
+#    gnutls-bin \
+#    nettle-dev \
+#    gcc \
+#    llvm-dev \
+#    libclang-dev \
+#    build-essential \
+#    pkg-config \
+#    gettext \
+#    git \
+#    procps \
+#    psmisc \
+#    vim-tiny \
+#    cargo \
+# && rm -rf /var/lib/apt/lists/* \
+# && apt-get clean \
+# && apt-get autoclean \
+# && rm -rf /var/lib/apt/archives/* \
+# && rm -rf /var/cache/apt/*
+# ADD setup /setup
+# RUN echo build build build build
+# RUN /setup/setup
+# ADD run /setup/run
+#WORKDIR /source
+#ENTRYPOINT ["/usr/bin/tini", "--"]
+#CMD ["bash", "/setup/run"]
\ No newline at end of file
diff --git a/compose/setup/version b/compose/setup/version
new file mode 100644
index 0000000..0cfbf08
--- /dev/null
+++ b/compose/setup/version
@@ -0,0 +1 @@
+2
diff --git a/docker-compose.yml b/docker-compose.yml
index e90c033..72fe5cf 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -2,14 +2,5 @@
 services:
   testing:
     image: ghcr.io/adfinis/pyaptly/cache:latest
-    build:
-      cache_from:
-        - ghcr.io/adfinis/pyaptly/cache:latest
-      context: compose
-      args:
-        BUILDKIT_INLINE_CACHE: 1
-        DOCKER_IMAGE: debian:bookworm-slim
-    working_dir: /source
-    command: ["bash", "/setup/run"]
     volumes:
       - ./:/source