Skip to content

Commit 8db70c1

Browse files
authored
Refactor deployment fetching and matrix generation
1 parent a0abf28 commit 8db70c1

1 file changed

Lines changed: 49 additions & 27 deletions

File tree

.github/workflows/environment-matrix-dashboard.yml

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,80 @@ concurrency:
1111
jobs:
1212
generate-dashboard:
1313
runs-on: ubuntu-latest
14+
1415
permissions:
15-
contents: none
16-
deployments: read
16+
contents: none
17+
deployments: read
1718

1819
steps:
1920
- name: Generate Matrix Table
2021
env:
2122
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
REPOSITORY: ${{ github.repository }}
2224
run: |
2325
cat << 'EOF' > generate_matrix.py
2426
import json
2527
import subprocess
2628
import re
2729
import os
2830
29-
# Fetch the last 100 deployment events
30-
cmd = 'gh api repos/${{ github.repository }}/deployments --per-page 100'
31-
res = subprocess.check_output(cmd, shell=True).decode('utf-8')
31+
repository = os.environ["REPOSITORY"]
32+
stages = ["development", "test", "production"]
33+
34+
# Fetch deployments, newest first.
35+
cmd = f"gh api repos/{repository}/deployments?per_page=100"
36+
res = subprocess.check_output(cmd, shell=True).decode("utf-8")
3237
deployments = json.loads(res)
3338
39+
deployments.sort(
40+
key=lambda x: x.get("created_at", ""),
41+
reverse=True
42+
)
43+
3444
matrix = {}
35-
stages = ['development', 'test', 'production']
3645
37-
for d in deployments:
38-
env_name = d.get('environment', '')
46+
for deployment in deployments:
47+
env_name = deployment.get("environment", "")
3948
40-
# Matches "application-environment" (e.g., transfers-development)
41-
match = re.match(r'^(.*?)-(development|test|production)$', env_name, re.IGNORECASE)
42-
if match:
43-
app = match.group(1).upper()
44-
stage = match.group(2).lower()
45-
ref = d.get('ref', 'unknown')
49+
# Matches "application-environment"
50+
# Example: transfers-development
51+
match = re.match(
52+
r"^(.*?)-(development|test|production)$",
53+
env_name,
54+
re.IGNORECASE
55+
)
4656
47-
if app not in matrix:
48-
matrix[app] = {s: '-' for s in stages}
57+
if not match:
58+
continue
4959
50-
# Populate only if empty (keeps latest deploy)
51-
if matrix[app][stage] == '-':
52-
matrix[app][stage] = f'`{ref}`'
60+
app = match.group(1).upper()
61+
stage = match.group(2).lower()
62+
ref = deployment.get("ref", "unknown")
63+
64+
if app not in matrix:
65+
matrix[app] = {s: "-" for s in stages}
66+
67+
# Keep the latest deployment per app/environment.
68+
if matrix[app][stage] == "-":
69+
matrix[app][stage] = f"`{ref}`"
5370
5471
markdown = [
55-
'## 🏢 Deployment Matrix Dashboard',
56-
'| Application | 🛠️ Development | 🧪 Test | 🚀 Production |',
57-
'| :--- | :---: | :---: | :---: |'
72+
"## 🏢 Deployment Matrix Dashboard",
73+
"",
74+
"| Application | 🛠️ Development | 🧪 Test | 🚀 Production |",
75+
"| :--- | :---: | :---: | :---: |"
5876
]
5977
6078
for app in sorted(matrix.keys()):
61-
row = f"| **{app}** | {matrix[app]['development']} | {matrix[app]['test']} | {matrix[app]['production']} |"
62-
markdown.append(row)
79+
markdown.append(
80+
f"| **{app}** | {matrix[app]['development']} | {matrix[app]['test']} | {matrix[app]['production']} |"
81+
)
82+
83+
if not matrix:
84+
markdown.append("| _No deployments found_ | - | - | - |")
6385
64-
with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as f:
65-
f.write('\n'.join(markdown))
86+
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as f:
87+
f.write("\n".join(markdown))
6688
EOF
6789
68-
python3 generate_matrix.py
90+
python3 generate_matrix.py

0 commit comments

Comments
 (0)