From 939e7fe0e37874ca1e7b999b5e17a904162b858a Mon Sep 17 00:00:00 2001
From: Stefan Agner <stefan@agner.ch>
Date: Tue, 6 Feb 2024 12:58:53 +0100
Subject: [PATCH] Support pre-releases with version suffix

---
 .github/workflows/release.yml | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index cd6a6d63..4ec5e1a0 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -15,6 +15,19 @@ jobs:
       - name: Get tag
         id: vars
         run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
+      - name: Validate version number
+        run: >-
+          if [[ "${{ github.event.release.prerelease }}" == "true" ]]; then
+            if ! [[ "${{ steps.vars.outputs.tag }}" =~ "b" ]]; then
+            echo "Pre-release: Tag is missing beta suffix (${{ steps.vars.outputs.tag }})"
+              exit 1
+            fi
+          else
+            if [[ "${{ steps.vars.outputs.tag }}" =~ "b" ]]; then
+              echo "Release: Tag must not have a beta suffix (${{ steps.vars.outputs.tag }})"
+              exit 1
+            fi
+          fi
       - name: Set up Python 3.10
         uses: actions/setup-python@v5.0.0
         with:
@@ -67,8 +80,9 @@ jobs:
           echo "patch=${patch}" >> $GITHUB_OUTPUT
           echo "minor=${patch%.*}" >> $GITHUB_OUTPUT
           echo "major=${patch%.*.*}" >> $GITHUB_OUTPUT
-      - name: Build and Push
+      - name: Build and Push release
         uses: docker/build-push-action@v5.1.0
+        if: github.event.release.prerelease == false
         with:
           context: .
           platforms: linux/amd64,linux/arm64
@@ -81,3 +95,16 @@ jobs:
           push: true
           build-args:
             "PYTHON_MATTER_SERVER=${{ needs.build-and-publish-pypi.outputs.version }}"
+      - name: Build and Push pre-release
+        uses: docker/build-push-action@v5.1.0
+        if: github.event.release.prerelease == true
+        with:
+          context: .
+          platforms: linux/amd64,linux/arm64
+          file: Dockerfile
+          tags: |-
+            ghcr.io/${{ github.repository_owner }}/python-matter-server:${{ steps.tags.outputs.patch }},
+            ghcr.io/${{ github.repository_owner }}/python-matter-server:beta
+          push: true
+          build-args:
+            "PYTHON_MATTER_SERVER=${{ needs.build-and-publish-pypi.outputs.version }}"