From b6cc2876ac3a593ede5644ca2e5a3bbec5572837 Mon Sep 17 00:00:00 2001
From: Trevor Whitney <trevorjwhitney@gmail.com>
Date: Tue, 28 Nov 2023 13:56:41 -0700
Subject: [PATCH] feat: build pipeline using jsonnet for re-use

---
 .github/workflows/jsonnet-release.yml | 99 +++++++++++++++++++++++++++
 lib/common.libsonnet                  | 17 +++++
 lib/image.libsonnet                   | 62 +++++++++++++++++
 lib/lib.libsonnet                     |  3 +
 main.jsonnet                          | 19 +++++
 package.json                          |  3 +-
 6 files changed, 202 insertions(+), 1 deletion(-)
 create mode 100644 .github/workflows/jsonnet-release.yml
 create mode 100644 lib/common.libsonnet
 create mode 100644 lib/image.libsonnet
 create mode 100644 lib/lib.libsonnet
 create mode 100644 main.jsonnet

diff --git a/.github/workflows/jsonnet-release.yml b/.github/workflows/jsonnet-release.yml
new file mode 100644
index 00000000..1cac9dde
--- /dev/null
+++ b/.github/workflows/jsonnet-release.yml
@@ -0,0 +1,99 @@
+"jobs":
+  "loki-image":
+    "runs-on": "ubuntu-latest"
+    "steps":
+    - "name": "pull loki code"
+      "uses": "actions/checkout@v3"
+      "with":
+        "ref": "prepare-release-please"
+        "repository": "grafana/loki"
+    - "name": "Set up QEMU"
+      "uses": "docker/setup-qemu-action@v3"
+      "with":
+        "go-version-file": "go.mod"
+    - "name": "set up docker buildx"
+      "uses": "docker/setup-buildx-action@v3"
+    - "id": "parse-metadata"
+      "name": "parse image metadata"
+      "run": |
+        mkdir -p dist
+        
+        platform="$(echo "${{ matrix.platform}}" |  sed  "s/\(.*\)\/\(.*\)/\1-\2/")"
+        echo "platform=${platform}" >> $GITHUB_OUTPUT
+        
+        version=$(jq -r '."cmd/loki"' .release-please-manifest.json)
+        echo "version=${version}" >> $GITHUB_OUTPUT
+      "shell": "bash"
+    - "name": "Build and export"
+      "uses": "docker/build-push-action@v5"
+      "with":
+        "context": "."
+        "file": "cmd/loki/Dockerfile"
+        "outputs": "type=docker,dest=dist/loki-${{ steps.parse-metadata.outputs.version}}-${{ steps.parse-metadata.outputs.platform }}.tar"
+        "platforms": "${{ matrix.platform }}"
+        "tags": "grafana/loki:${{ steps.parse-metadata.outputs.version }}"
+    - "name": "upload artifacts"
+      "uses": "actions/upload-artifact@v3"
+      "with":
+        "name": "loki-image-${{ matrix.platform }}"
+        "path": "dist/loki-${{ steps.parse-metadata.outputs.version}}-${{ steps.parse-metadata.outputs.platform }}.tar"
+    "strategy":
+      "fail-fast": true
+      "matrix":
+        "platform":
+        - "linux/amd64"
+        - "linux/arm64"
+        - "linux/arm"
+  "promtail-image":
+    "runs-on": "ubuntu-latest"
+    "steps":
+    - "name": "pull loki code"
+      "uses": "actions/checkout@v3"
+      "with":
+        "ref": "prepare-release-please"
+        "repository": "grafana/loki"
+    - "name": "Set up QEMU"
+      "uses": "docker/setup-qemu-action@v3"
+      "with":
+        "go-version-file": "go.mod"
+    - "name": "set up docker buildx"
+      "uses": "docker/setup-buildx-action@v3"
+    - "id": "parse-metadata"
+      "name": "parse image metadata"
+      "run": |
+        mkdir -p dist
+        
+        platform="$(echo "${{ matrix.platform}}" |  sed  "s/\(.*\)\/\(.*\)/\1-\2/")"
+        echo "platform=${platform}" >> $GITHUB_OUTPUT
+        
+        version=$(jq -r '."cmd/promtail"' .release-please-manifest.json)
+        echo "version=${version}" >> $GITHUB_OUTPUT
+      "shell": "bash"
+    - "name": "Build and export"
+      "uses": "docker/build-push-action@v5"
+      "with":
+        "context": "."
+        "file": "cmd/promtail/Dockerfile"
+        "outputs": "type=docker,dest=dist/promtail-${{ steps.parse-metadata.outputs.version}}-${{ steps.parse-metadata.outputs.platform }}.tar"
+        "platforms": "${{ matrix.platform }}"
+        "tags": "grafana/promtail:${{ steps.parse-metadata.outputs.version }}"
+    - "name": "upload artifacts"
+      "uses": "actions/upload-artifact@v3"
+      "with":
+        "name": "promtail-image-${{ matrix.platform }}"
+        "path": "dist/promtail-${{ steps.parse-metadata.outputs.version}}-${{ steps.parse-metadata.outputs.platform }}.tar"
+    "strategy":
+      "fail-fast": true
+      "matrix":
+        "platform":
+        - "linux/amd64"
+        - "linux/arm64"
+        - "linux/arm"
+"name": "release (jsonnet version)"
+"on":
+  "push":
+    "branches":
+    - "main"
+    - "release-[0-9].[0-9].x"
+    - "k[0-9]*"
+  "workflow_dispatch": {}
diff --git a/lib/common.libsonnet b/lib/common.libsonnet
new file mode 100644
index 00000000..b583dc20
--- /dev/null
+++ b/lib/common.libsonnet
@@ -0,0 +1,17 @@
+{
+  fetchLokiRepo: {
+    name: 'pull loki code',
+    uses: 'actions/checkout@v3',
+    with: {
+      repository: 'grafana/loki',
+      ref: 'prepare-release-please',
+    },
+  },
+  setupGo: {
+    uses: 'actions/setup-go@v4',
+    with: {
+      'go-version-file': 'go.mod',
+    },
+  },
+}
+
diff --git a/lib/image.libsonnet b/lib/image.libsonnet
new file mode 100644
index 00000000..7e797acd
--- /dev/null
+++ b/lib/image.libsonnet
@@ -0,0 +1,62 @@
+local common = import 'common.libsonnet';
+
+{
+  buildImage: function(name, path) {
+    'runs-on': 'ubuntu-latest',
+    strategy: {
+      'fail-fast': true,
+      matrix: {
+        platform: [
+          'linux/amd64',
+          'linux/arm64',
+          'linux/arm',
+        ],
+      },
+    },
+    steps: [
+      common.fetchLokiRepo,
+      common.setupGo
+      {
+        name: 'Set up QEMU',
+        uses: 'docker/setup-qemu-action@v3',
+      },
+      {
+        name: 'set up docker buildx',
+        uses: 'docker/setup-buildx-action@v3',
+      },
+      {
+        name: 'parse image metadata',
+        id: 'parse-metadata',
+        shell: 'bash',
+        run: |||
+          mkdir -p dist
+
+          platform="$(echo "${{ matrix.platform}}" |  sed  "s/\(.*\)\/\(.*\)/\1-\2/")"
+          echo "platform=${platform}" >> $GITHUB_OUTPUT
+
+          version=$(jq -r '."%s"' .release-please-manifest.json)
+          echo "version=${version}" >> $GITHUB_OUTPUT
+        ||| % path,
+      },
+      {
+        name: 'Build and export',
+        uses: 'docker/build-push-action@v5',
+        with: {
+          context: '.',
+          file: '%s/Dockerfile' % path,
+          platforms: '${{ matrix.platform }}',
+          tags: 'grafana/%s:${{ steps.parse-metadata.outputs.version }}' % name,
+          outputs: 'type=docker,dest=dist/%s-${{ steps.parse-metadata.outputs.version}}-${{ steps.parse-metadata.outputs.platform }}.tar' % name,
+        },
+      },
+      {
+        name: 'upload artifacts',
+        uses: 'actions/upload-artifact@v3',
+        with: {
+          name: '%s-image-${{ matrix.platform }}' % name,
+          path: 'dist/%s-${{ steps.parse-metadata.outputs.version}}-${{ steps.parse-metadata.outputs.platform }}.tar' % name,
+        },
+      },
+    ],
+  },
+}
diff --git a/lib/lib.libsonnet b/lib/lib.libsonnet
new file mode 100644
index 00000000..32ce3f29
--- /dev/null
+++ b/lib/lib.libsonnet
@@ -0,0 +1,3 @@
+{
+  image: (import 'image.libsonnet'),
+}
diff --git a/main.jsonnet b/main.jsonnet
new file mode 100644
index 00000000..edc2e8d3
--- /dev/null
+++ b/main.jsonnet
@@ -0,0 +1,19 @@
+local lib = import 'lib/lib.libsonnet';
+
+std.manifestYamlDoc({
+  name: 'release (jsonnet version)',
+  on: {
+    workflow_dispatch: {},
+    push: {
+      branches: [
+        'main',
+        'release-[0-9].[0-9].x',
+        'k[0-9]*',
+      ],
+    },
+  },
+  jobs: {
+    'loki-image': lib.image.buildImage("loki", "cmd/loki"),
+    'promtail-image': lib.image.buildImage("promtail", "cmd/promtail"),
+  },
+})
diff --git a/package.json b/package.json
index c51fa781..a1d31416 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,8 @@
   "description": "release pipeline for the Loki database",
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "build": "jsonnet -S main.jsonnet > .github/workflows/jsonnet-release.yml"
   },
   "repository": {
     "type": "git",