Skip to content

Environment Matrix Dashboard #171

Environment Matrix Dashboard

Environment Matrix Dashboard #171

name: Environment Matrix Dashboard
on:
workflow_dispatch:
deployment_status:
concurrency:
group: environment-dashboard
cancel-in-progress: true
jobs:
generate-dashboard:
runs-on: ubuntu-latest
permissions:
contents: none
deployments: read
steps:
- name: Generate Matrix Table
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPOSITORY: ${{ github.repository }}
run: |
cat << 'EOF' > generate_matrix.py
import json
import subprocess
import re
import os
repository = os.environ["REPOSITORY"]
stages = ["development", "test", "production"]
# Fetch deployments, newest first.
cmd = f"gh api repos/{repository}/deployments?per_page=100"
res = subprocess.check_output(cmd, shell=True).decode("utf-8")
deployments = json.loads(res)
deployments.sort(
key=lambda x: x.get("created_at", ""),
reverse=True
)
matrix = {}
for deployment in deployments:
env_name = deployment.get("environment", "")
# Matches "application-environment"
# Example: transfers-development
match = re.match(
r"^(.*?)-(development|test|production)$",
env_name,
re.IGNORECASE
)
if not match:
continue
app = match.group(1).upper()
stage = match.group(2).lower()
ref = deployment.get("ref", "unknown")
if app not in matrix:
matrix[app] = {s: "-" for s in stages}
# Keep the latest deployment per app/environment.
if matrix[app][stage] == "-":
matrix[app][stage] = f"`{ref}`"
markdown = [
"## 🏢 Deployment Matrix Dashboard",
"",
"| Application | 🛠️ Development | 🧪 Test | 🚀 Production |",
"| :--- | :---: | :---: | :---: |"
]
for app in sorted(matrix.keys()):
markdown.append(
f"| **{app}** | {matrix[app]['development']} | {matrix[app]['test']} | {matrix[app]['production']} |"
)
if not matrix:
markdown.append("| _No deployments found_ | - | - | - |")
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as f:
f.write("\n".join(markdown))
EOF
python3 generate_matrix.py