From 19b75aa3d6a26a36373e5976a358a9da45ff6f02 Mon Sep 17 00:00:00 2001
From: Tristan Cacqueray <tdecacqu@redhat.com>
Date: Thu, 14 Nov 2024 15:59:50 -0500
Subject: [PATCH] Add release workflow

---
 .github/workflows/release.yml | 18 ++++++++++++++++++
 flake.nix                     | 21 +++++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 .github/workflows/release.yml

diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
new file mode 100644
index 0000000..12a25a5
--- /dev/null
+++ b/.github/workflows/release.yml
@@ -0,0 +1,18 @@
+name: release
+on:
+  push:
+    tags:
+    - "[0-9]+.[0-9]+.[0-9]+"
+jobs:
+  create-container-release:
+    name: create-container-release
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v3
+      - uses: DeterminateSystems/nix-installer-action@main
+      - uses: DeterminateSystems/magic-nix-cache-action@main
+      - name: Push Image
+        run: nix run ".#publish-container-release"
+        env:
+          GH_USERNAME: ${{ github.actor }}
+          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/flake.nix b/flake.nix
index 03024f0..c9a04f1 100644
--- a/flake.nix
+++ b/flake.nix
@@ -14,6 +14,9 @@
         pkgs = import nixpkgs { inherit system; };
         python = pkgs.python311.withPackages
           (ps: [ ps.openstacksdk ps.prometheus-client ps.pyyaml ]);
+
+        info = builtins.fromTOML (builtins.readFile ./pyproject.toml);
+
         container-name = "ghcr.io/tristancacqueray/zuul-capacity";
         container = pkgs.dockerTools.streamLayeredImage {
           name = container-name;
@@ -27,8 +30,26 @@
           };
         };
 
+        publish-container-release =
+          pkgs.writeShellScriptBin "container-release" ''
+            set -e
+            export PATH=$PATH:${pkgs.gzip}/bin:${pkgs.skopeo}/bin
+            IMAGE="docker://${container-name}"
+
+            echo "Logging to registry..."
+            echo $GH_TOKEN | skopeo login --username $GH_USERNAME --password-stdin ghcr.io
+
+            echo "Building and publishing the image..."
+            ${container} | gzip --fast | skopeo copy docker-archive:/dev/stdin $IMAGE:${info.project.version}
+
+            echo "Tagging latest"
+            skopeo copy $IMAGE:${info.project.version} $IMAGE:latest
+          '';
+
       in {
         packages.container = container;
+        apps.publish-container-release =
+          flake-utils.lib.mkApp { drv = publish-container-release; };
         devShell = pkgs.mkShell { buildInputs = with pkgs; [ uv ]; };
       });
 }