-
Notifications
You must be signed in to change notification settings - Fork 1
167 lines (136 loc) · 5.78 KB
/
helm-chart-cleanup.yml
File metadata and controls
167 lines (136 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: Helm Chart Cleanup
on:
push:
branches:
- main
permissions:
contents: write
jobs:
detect-and-cleanup:
runs-on: ubuntu-latest
# Add concurrency to prevent multiple workflows running simultaneously on the same branch
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Checkout repository (main branch)
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: main
- name: Check for deleted Helm directories
id: check-deleted
run: |
# Get list of all deleted files in this push
DELETED_FILES=$(git diff --name-status HEAD^ HEAD | grep '^D' | awk '{print $2}')
# Check if any of the deleted files are in a Helm directory
DELETED_HELM_DIRS=""
for FILE in $DELETED_FILES; do
if [[ $FILE == */helm/* ]]; then
# Extract app name from path (assuming directory structure like app-name/helm/...)
APP_NAME=$(echo $FILE | cut -d'/' -f1)
# Add to our list if not already there
if [[ ! $DELETED_HELM_DIRS =~ (^|[[:space:]])$APP_NAME($|[[:space:]]) ]]; then
DELETED_HELM_DIRS="$DELETED_HELM_DIRS $APP_NAME"
fi
fi
done
# Trim leading whitespace
DELETED_HELM_DIRS=$(echo $DELETED_HELM_DIRS | xargs)
if [ -n "$DELETED_HELM_DIRS" ]; then
echo "Detected deleted Helm directories for apps: $DELETED_HELM_DIRS"
echo "DELETED_APPS=$DELETED_HELM_DIRS" >> $GITHUB_ENV
echo "CLEANUP_NEEDED=true" >> $GITHUB_ENV
else
echo "No Helm directories were deleted"
echo "CLEANUP_NEEDED=false" >> $GITHUB_ENV
fi
- name: Set up Helm
if: env.CLEANUP_NEEDED == 'true'
uses: azure/setup-helm@v3
with:
version: 'latest'
- name: Checkout gh-pages branch
if: env.CLEANUP_NEEDED == 'true'
uses: actions/checkout@v3
with:
ref: gh-pages
path: gh-pages
token: ${{ secrets.APP_INSTALLATION_TOKEN }}
# Add fetch-depth: 0 to get full history
fetch-depth: 0
- name: Install yq
if: env.CLEANUP_NEEDED == 'true'
run: |
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
sudo chmod +x /usr/local/bin/yq
- name: Remove deleted charts from index.yaml
if: env.CLEANUP_NEEDED == 'true'
run: |
cd gh-pages
# Pull latest changes from remote first to avoid conflicts
git fetch origin gh-pages
git reset --hard origin/gh-pages
echo "Checking for charts to remove..."
# For each deleted app, check if its chart exists in index.yaml
for APP in $DELETED_APPS; do
echo "Processing deleted app: $APP"
# Find all chart .tgz files for this app in the charts subdirectory and remove them
CHART_FILES=$(find ./charts -name "${APP}-*.tgz" -o -name "${APP}_*.tgz" || true)
if [ -n "$CHART_FILES" ]; then
echo "Removing chart files: $CHART_FILES"
rm -f $CHART_FILES
fi
# Check if this app has an entry in index.yaml
if grep -q "name: ${APP}" index.yaml; then
echo "Found entry for $APP in index.yaml, removing..."
# Use yq to remove the entry for this app from index.yaml
yq eval -i "del(.entries.\"${APP}\")" index.yaml
echo "Successfully removed $APP from index.yaml"
else
echo "No entry found for $APP in index.yaml"
fi
done
# Update the generated timestamp in index.yaml
yq eval -i ".generated = \"$(date -u +"%Y-%m-%dT%H:%M:%S.%NZ")\"" index.yaml
# Remove any existing timestamp comments
sed -i '/^# Charts updated from main branch on/d' index.yaml
# Add metadata annotation
echo "" >> index.yaml
echo "# Charts updated from main branch on $(date)" >> index.yaml
- name: Commit and push to gh-pages
if: env.CLEANUP_NEEDED == 'true'
run: |
cd gh-pages
git config user.name "GitHub Actions Bot"
git config user.email "actions@github.com"
# Add the index.yaml file
git add index.yaml
# Remove any deleted .tgz files from git
git ls-files --deleted | xargs -r git rm || true
# Only commit if there are changes
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "Remove charts for deleted apps: $DELETED_APPS"
# Add retry logic for pushing
MAX_RETRIES=5
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
# Pull with rebase to avoid merge conflicts
git pull --rebase origin gh-pages || true
if git push; then
echo "Successfully pushed changes"
break
else
RETRY_COUNT=$((RETRY_COUNT+1))
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
echo "Push failed, retrying in 5 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
sleep 5
else
echo "Failed to push after $MAX_RETRIES attempts"
exit 1
fi
fi
done
fi