From 0dd034b7b633d6cb005caea3cdfa7d8e702cd89e Mon Sep 17 00:00:00 2001
From: jo <ljonas@riseup.net>
Date: Sun, 26 Feb 2023 15:18:10 +0100
Subject: [PATCH] ci: use bake file for container build

---
 .../actions/build-container/action.yml        |  31 ------
 .github/workflows/container.yml               | 105 +++++++++---------
 docker-bake.json                              |  24 ++++
 3 files changed, 74 insertions(+), 86 deletions(-)
 delete mode 100644 .github/workflows/actions/build-container/action.yml
 create mode 100644 docker-bake.json

diff --git a/.github/workflows/actions/build-container/action.yml b/.github/workflows/actions/build-container/action.yml
deleted file mode 100644
index e891576b5..000000000
--- a/.github/workflows/actions/build-container/action.yml
+++ /dev/null
@@ -1,31 +0,0 @@
-name: Build container
-description: Build and push a container
-
-inputs:
-  target:
-    required: true
-
-runs:
-  using: composite
-  steps:
-    - uses: docker/metadata-action@v4
-      id: meta
-      with:
-        images: ${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ inputs.target }}
-        tags: |
-          type=ref,event=branch
-          type=semver,pattern={{version}}
-          type=semver,pattern={{major}}.{{minor}}
-
-    - uses: docker/build-push-action@v4
-      with:
-        context: .
-        pull: true
-        push: ${{ github.event_name == 'push' }}
-        build-args: |
-          LIBRETIME_VERSION=${{ env.LIBRETIME_VERSION }}
-        target: ${{ inputs.target }}
-        tags: ${{ steps.meta.outputs.tags }}
-        labels: ${{ steps.meta.outputs.labels }}
-        cache-from: type=gha,scope=${{ inputs.target }}
-        cache-to: type=gha,scope=${{ inputs.target }},mode=max
diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml
index 00a4c08cb..2c475285d 100644
--- a/.github/workflows/container.yml
+++ b/.github/workflows/container.yml
@@ -11,12 +11,37 @@ concurrency:
   group: ${{ github.workflow }}-${{ github.ref }}
 
 jobs:
-  build:
+  meta:
     runs-on: ubuntu-latest
 
-    env:
-      REGISTRY: ghcr.io
-      NAMESPACE: ${{ github.repository_owner }}
+    strategy:
+      matrix:
+        target: [analyzer, api, legacy, playout, worker]
+
+    if: ${{ github.repository_owner == 'libretime' }}
+    steps:
+      - uses: actions/checkout@v3
+
+      - uses: docker/metadata-action@v4
+        id: meta
+        with:
+          bake-target: ${{ matrix.target }}
+          images: |
+            ghcr.io/libretime/libretime-${{ matrix.target }}
+          tags: |
+            type=ref,event=branch
+            type=semver,pattern={{version}}
+            type=semver,pattern={{major}}.{{minor}}
+
+      - name: Upload metadata bake file
+        uses: actions/upload-artifact@v3
+        with:
+          name: meta-${{ matrix.target }}
+          path: ${{ steps.meta.outputs.bake-file }}
+
+  build:
+    needs: [meta]
+    runs-on: ubuntu-latest
 
     if: ${{ github.repository_owner == 'libretime' }}
     steps:
@@ -24,65 +49,35 @@ jobs:
 
       - uses: docker/setup-buildx-action@v2
 
-      - uses: docker/login-action@v2
+      - name: Login ghcr.io
+        if: github.event_name == 'push'
+        uses: docker/login-action@v2
         with:
-          registry: ${{ env.REGISTRY }}
+          registry: ghcr.io
           username: ${{ github.actor }}
           password: ${{ secrets.GITHUB_TOKEN }}
 
+      - name: Download all metadata bake files
+        uses: actions/download-artifact@v3
+
       - name: Guess LIBRETIME_VERSION
         run: |
           make VERSION
           echo "LIBRETIME_VERSION=$(cat VERSION | tr -d [:blank:])" >> $GITHUB_ENV
 
-      - name: Build python-builder
-        uses: docker/build-push-action@v4
+      - name: Build
+        uses: docker/bake-action@v2
         with:
-          context: .
           pull: true
-          target: python-builder
-          cache-from: type=gha,scope=python-builder
-          cache-to: type=gha,scope=python-builder,mode=max
-
-      - name: Build python-base
-        uses: docker/build-push-action@v4
-        with:
-          context: .
-          pull: true
-          target: python-base
-          cache-from: type=gha,scope=python-base
-          cache-to: type=gha,scope=python-base,mode=max
-
-      - name: Build python-base-ffmpeg
-        uses: docker/build-push-action@v4
-        with:
-          context: .
-          pull: true
-          target: python-base-ffmpeg
-          cache-from: type=gha,scope=python-base-ffmpeg
-          cache-to: type=gha,scope=python-base-ffmpeg,mode=max
-
-      - name: Build analyzer
-        uses: ./.github/workflows/actions/build-container
-        with:
-          target: libretime-analyzer
-
-      - name: Build api
-        uses: ./.github/workflows/actions/build-container
-        with:
-          target: libretime-api
-
-      - name: Build playout
-        uses: ./.github/workflows/actions/build-container
-        with:
-          target: libretime-playout
-
-      - name: Build worker
-        uses: ./.github/workflows/actions/build-container
-        with:
-          target: libretime-worker
-
-      - name: Build legacy
-        uses: ./.github/workflows/actions/build-container
-        with:
-          target: libretime-legacy
+          push: ${{ github.event_name == 'push' }}
+          files: |
+            docker-bake.json
+            meta-analyzer/docker-metadata-action-bake.json
+            meta-api/docker-metadata-action-bake.json
+            meta-legacy/docker-metadata-action-bake.json
+            meta-playout/docker-metadata-action-bake.json
+            meta-worker/docker-metadata-action-bake.json
+          set: |
+            *.cache-from=type=gha,scope=container
+            *.cache-to=type=gha,scope=container,mode=max
+            *.args.LIBRETIME_VERSION=${{ env.LIBRETIME_VERSION }}
diff --git a/docker-bake.json b/docker-bake.json
new file mode 100644
index 000000000..03d5ed6c5
--- /dev/null
+++ b/docker-bake.json
@@ -0,0 +1,24 @@
+{
+  "group": {
+    "default": {
+      "targets": ["analyzer", "playout", "api", "worker", "legacy"]
+    }
+  },
+  "target": {
+    "analyzer": {
+      "target": "libretime-analyzer"
+    },
+    "playout": {
+      "target": "libretime-playout"
+    },
+    "api": {
+      "target": "libretime-api"
+    },
+    "worker": {
+      "target": "libretime-worker"
+    },
+    "legacy": {
+      "target": "libretime-legacy"
+    }
+  }
+}