From 06d8903b7822a51357d76bf63266ab75759175f1 Mon Sep 17 00:00:00 2001
From: Kyle Colantonio <k@yle.sh>
Date: Sat, 31 Aug 2024 17:10:35 -0400
Subject: [PATCH] feat: Adding workflow to automate README tags

---
 .github/workflows/readme-versions.yml | 80 +++++++++++++++++++++++++++
 README.md                             | 20 ++++---
 2 files changed, 93 insertions(+), 7 deletions(-)
 create mode 100644 .github/workflows/readme-versions.yml

diff --git a/.github/workflows/readme-versions.yml b/.github/workflows/readme-versions.yml
new file mode 100644
index 0000000..954d826
--- /dev/null
+++ b/.github/workflows/readme-versions.yml
@@ -0,0 +1,80 @@
+name: README Versions
+
+on:
+  pull_request:
+    types: [opened, reopened, synchronize]
+    branches: [main]
+    paths:
+      - Dockerfile
+
+jobs:
+  readme:
+    name: Update README versions
+    runs-on: ubuntu-latest
+    permissions:
+      contents: write
+    steps:
+      - name: Checkout PR
+        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
+        with:
+          ref: ${{ github.event.pull_request.head.ref }}
+
+      - name: Parse Caddy version
+        id: caddy
+        shell: bash
+        run: |
+          CADDY_VERSION=$(grep -m 1 -Eo 'caddy:[0-9]+\.[0-9]+\.[0-9]+' Dockerfile \
+            | sed -E 's/.+:([0-9]+\.[0-9]+\.[0-9]+)(.+)?$/\1/g')
+          MAJOR=$(echo "$CADDY_VERSION" | cut -d . -f 1)
+          MINOR=$(echo "$CADDY_VERSION" | cut -d . -f 2)
+          PATCH=$(echo "$CADDY_VERSION" | cut -d . -f 3)
+          cat <<EOF | tee -a "$GITHUB_OUTPUT"
+          version=$CADDY_VERSION
+          major=$MAJOR
+          minor=$MINOR
+          patch=$PATCH
+          EOF
+
+      - name: Update README
+        shell: bash
+        env:
+          VERSION: ${{ steps.caddy.outputs.version }}
+          MAJOR: ${{ steps.caddy.outputs.major }}
+          MINOR: ${{ steps.caddy.outputs.minor }}
+          PATCH: ${{ steps.caddy.outputs.patch }}
+        run: |
+          cat > tags.md <<EOF
+          - [\`latest\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/tree/v$VERSION)
+          - [\`$VERSION\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/tree/v$VERSION)
+          - [\`$MAJOR.$MINOR\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/tree/v$MAJOR.$MINOR)
+          - [\`$MAJOR\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/tree/v$MAJOR)
+          EOF
+          awk '
+              BEGIN{p=1}
+              $1=="<!--START-TAGS-->"{p=0;print;next}
+              $1=="<!--END-TAGS-->"{system("cat tags.md");p=1}
+              p
+          ' README.md > README.md.new
+          mv README.md.new README.md
+          rm tags.md
+
+      - name: Check if README was updated
+        id: changed
+        shell: bash
+        run: |
+          if git diff-index --quiet HEAD ; then
+            echo "changed=false" | tee -a "$GITHUB_OUTPUT"
+          else
+            echo "changed=true" | tee -a "$GITHUB_OUTPUT"
+          fi
+
+      - name: Push changes back to PR
+        if: steps.changed.outputs.changed == 'true'
+        uses: ChromeQ/commit@99a563633526aeb2d5c2b02b1521accee9a8c896 # v2.x
+        env:
+          GH_TOKEN: ${{ github.token }}
+        with:
+          ref: ${{ github.event.pull_request.head.ref }}
+          commit-message: "docs: Updating README for v${{ steps.caddy.outputs.version }}"
+          files: |
+            README.md
diff --git a/README.md b/README.md
index 63b2598..e9226c0 100644
--- a/README.md
+++ b/README.md
@@ -25,13 +25,19 @@ docker pull ghcr.io/iarekylew00t/caddy-cloudflare:latest
 
 ## Tags
 
-The following tags are available for the `iarekylew00t/caddy-cloudflare` image.
-
-- `latest`
-- `<version>` (eg: `2.6.4`, including: `2.6`, `2`, etc.)
-
-Check the GitHub [Tags](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/tags)
-for a full list of supported versions!
+The following tags are the latest available for the
+`iarekylew00t/caddy-cloudflare` image.
+
+<!--START-TAGS-->
+- [`latest`](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/tree/v2.8.4)
+- [`2.8.4`](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/tree/v2.8.4)
+- [`2.8`](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/tree/v2.8)
+- [`2`](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/tree/v2)
+<!--END-TAGS-->
+
+Check the GitHub
+[Tags](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/tags)
+for a list of all versions!
 
 ## Usage