Skip to content

Commit 09d923e

Browse files
authored
Update helm-chart-publish.yml
1 parent 82911c2 commit 09d923e

File tree

1 file changed

+67
-131
lines changed

1 file changed

+67
-131
lines changed

.github/workflows/helm-chart-publish.yml

Lines changed: 67 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/checkout@v3
2222
with:
2323
fetch-depth: 0
24-
token: ${{ secrets.APP_INSTALLATION_TOKEN }}
24+
token: ${{ secrets.GITHUB_TOKEN }}
2525

2626
- name: Check for deleted Helm directories
2727
id: check-deleted
@@ -41,7 +41,6 @@ jobs:
4141

4242
- name: Process Helm charts
4343
if: env.SKIP_WORKFLOW != 'true'
44-
id: process-charts
4544
run: |
4645
# Find changed files in this commit
4746
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD | grep "helm/" || echo "")
@@ -71,18 +70,16 @@ jobs:
7170
# Create directory for packaged charts
7271
mkdir -p .charts
7372
74-
# Pull gh-pages branch to check current versions
75-
mkdir -p temp_ghpages
76-
git fetch origin gh-pages:gh-pages || true
77-
7873
# Configure git for possible commits
7974
git config user.name "GitHub Actions Bot"
8075
git config user.email "[email protected]"
8176
82-
# Version increment needed for any charts?
83-
VERSION_INCREMENT_NEEDED=false
77+
# For version increments create a separate branch
78+
TIMESTAMP=$(date +%s)
79+
BRANCH_NAME="helm-version-updates-$TIMESTAMP"
80+
NEED_VERSION_BRANCH=false
8481
85-
# First pass - check which charts need version increment
82+
# Process each chart
8683
for CHART_DIR in "${!CHART_DIRS[@]}"; do
8784
echo "Processing chart in $CHART_DIR"
8885
@@ -93,15 +90,18 @@ jobs:
9390
# Get chart name
9491
CHART_NAME=$(grep "name:" $CHART_DIR/Chart.yaml | awk '{print $2}')
9592
96-
# Check if this chart exists in the index with same version
93+
# Check if this is a new commit that hasn't been deployed yet
9794
NEEDS_INCREMENT=false
95+
96+
# Pull gh-pages branch to check current versions
97+
mkdir -p temp_ghpages
98+
git fetch origin gh-pages:gh-pages || true
9899
if git show gh-pages:index.yaml > temp_ghpages/index.yaml 2>/dev/null; then
99100
if grep -q "name: $CHART_NAME" temp_ghpages/index.yaml; then
100101
if grep -q "version: $CURRENT_VERSION" temp_ghpages/index.yaml; then
101102
echo "Chart $CHART_NAME with version $CURRENT_VERSION already exists in index"
102103
echo "Will increment patch version"
103104
NEEDS_INCREMENT=true
104-
VERSION_INCREMENT_NEEDED=true
105105
else
106106
echo "Chart version has already been changed in source, not incrementing"
107107
fi
@@ -112,32 +112,12 @@ jobs:
112112
echo "No index.yaml found on gh-pages branch, using current version"
113113
fi
114114
115-
# Store info for the next pass
115+
# Increment version if needed - create branch only once if needed
116116
if [ "$NEEDS_INCREMENT" = "true" ]; then
117-
echo "$CHART_DIR" >> charts_to_increment.txt
118-
fi
119-
120-
# Package the chart with current version for now
121-
helm package $CHART_DIR -d .charts/
122-
echo "Packaged chart in $CHART_DIR"
123-
done
124-
125-
# If we need to increment versions, create a separate branch
126-
if [ "$VERSION_INCREMENT_NEEDED" = "true" ] && [ -f charts_to_increment.txt ]; then
127-
echo "Version increments needed, creating branch"
128-
129-
# Create a new branch for version changes
130-
TIMESTAMP=$(date +%s)
131-
BRANCH_NAME="helm-version-updates-$TIMESTAMP"
132-
git checkout -b $BRANCH_NAME
133-
134-
# Second pass - increment versions
135-
while IFS= read -r CHART_DIR; do
136-
# Get current version
137-
CURRENT_VERSION=$(grep "version:" $CHART_DIR/Chart.yaml | awk '{print $2}')
138-
139-
# Get chart name
140-
CHART_NAME=$(grep "name:" $CHART_DIR/Chart.yaml | awk '{print $2}')
117+
if [ "$NEED_VERSION_BRANCH" = "false" ]; then
118+
NEED_VERSION_BRANCH=true
119+
git checkout -b $BRANCH_NAME
120+
fi
141121
142122
# Split version into parts
143123
MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1)
@@ -148,68 +128,76 @@ jobs:
148128
NEW_PATCH=$((PATCH + 1))
149129
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
150130
151-
echo "Incrementing version for $CHART_NAME from $CURRENT_VERSION to $NEW_VERSION"
131+
echo "Incrementing version from $CURRENT_VERSION to $NEW_VERSION"
152132
153133
# Update Chart.yaml with new version
154134
sed -i "s/version: $CURRENT_VERSION/version: $NEW_VERSION/" $CHART_DIR/Chart.yaml
155135
156-
# Add to git
136+
# Stage the changes
157137
git add $CHART_DIR/Chart.yaml
158-
done < charts_to_increment.txt
138+
fi
159139
160-
# Commit the changes
140+
# Always package the chart with current version
141+
helm package $CHART_DIR -d .charts/
142+
echo "Packaged chart in $CHART_DIR"
143+
done
144+
145+
# If we created a version branch, commit and push it
146+
if [ "$NEED_VERSION_BRANCH" = "true" ]; then
161147
git commit -m "Automatically increment Helm chart versions [skip ci]"
162148
163149
# Try to push the branch, with retry logic
164150
MAX_RETRIES=5
165151
RETRY_COUNT=0
166-
PUSH_SUCCESS=false
167152
168-
while [ $RETRY_COUNT -lt $MAX_RETRIES ] && [ "$PUSH_SUCCESS" != "true" ]; do
153+
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
169154
if git push origin $BRANCH_NAME; then
170155
echo "Successfully pushed branch $BRANCH_NAME"
171-
PUSH_SUCCESS=true
172156
173-
# Create a pull request
174-
# Note: This requires GH CLI to be installed and authenticated
175-
# If you're using GitHub Actions, you might want to use the GitHub API directly or a dedicated action
176-
echo "Creating PR for version updates"
177-
gh pr create --title "Update Helm chart versions" \
178-
--body "Automatically incrementing chart versions that already exist in the repository" \
179-
--base main \
180-
--head $BRANCH_NAME || echo "PR creation failed, but will continue with publishing"
157+
# Here you would create a PR
158+
# If using GitHub CLI:
159+
# gh pr create --title "Update Helm chart versions" \
160+
# --body "Automatically incrementing chart versions" \
161+
# --base main \
162+
# --head $BRANCH_NAME
163+
164+
# If you don't have GitHub CLI, you can use the GitHub API:
165+
curl -X POST \
166+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
167+
-H "Accept: application/vnd.github.v3+json" \
168+
https://api.github.com/repos/deploystackio/deploy-templates/pulls \
169+
-d "{\"title\":\"Update Helm chart versions\",\"body\":\"Automatically incrementing chart versions\",\"head\":\"$BRANCH_NAME\",\"base\":\"main\"}"
170+
171+
break
181172
else
182173
RETRY_COUNT=$((RETRY_COUNT+1))
183174
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
184175
echo "Push failed, retrying in 10 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
185176
sleep 10
186177
else
187-
echo "Failed to push after $MAX_RETRIES attempts, but will continue with publishing"
178+
echo "Failed to push after $MAX_RETRIES attempts"
179+
# Continue with chart publishing using current versions
188180
fi
189181
fi
190182
done
191183
192-
# Switch back to main branch to continue with publishing
184+
# Go back to main branch to continue with publishing
193185
git checkout main
194186
fi
195187
196-
# Always set HELM_EXISTS=true if we got this far
197-
echo "HELM_EXISTS=true" >> $GITHUB_ENV
188+
if [ -z "$(ls -A .charts/ 2>/dev/null)" ]; then
189+
echo "HELM_EXISTS=false" >> $GITHUB_ENV
190+
else
191+
echo "HELM_EXISTS=true" >> $GITHUB_ENV
192+
fi
198193
199194
- name: Checkout gh-pages branch
200195
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
201196
uses: actions/checkout@v3
202197
with:
203198
ref: gh-pages
204199
path: gh-pages
205-
token: ${{ secrets.APP_INSTALLATION_TOKEN }}
206-
fetch-depth: 0
207-
208-
- name: Install yq
209-
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
210-
run: |
211-
sudo wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
212-
sudo chmod +x /usr/bin/yq
200+
token: ${{ secrets.GITHUB_TOKEN }}
213201

214202
- name: Update Helm repository
215203
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
@@ -219,95 +207,41 @@ jobs:
219207
# Create charts directory if it doesn't exist
220208
mkdir -p charts
221209
222-
# Copy packaged charts to the charts subdirectory
210+
# Copy packaged charts
223211
cp ../.charts/*.tgz charts/
224212
225-
# Store a list of the helm chart packages we just added
226-
NEW_CHARTS=$(ls ../.charts/*.tgz | xargs -n1 basename)
227-
echo "New/updated charts: $NEW_CHARTS"
228-
229-
# If there's no index.yaml, create a new one with the proper URL path
230-
if [ ! -f "index.yaml" ] || [ ! -s "index.yaml" ]; then
213+
# If no index.yaml, create it
214+
if [ ! -f "index.yaml" ]; then
231215
helm repo index . --url https://deploystackio.github.io/deploy-templates/charts
232216
else
233-
# First, make a backup of the existing index
234-
cp index.yaml index.yaml.bak
235-
236-
# Make a temporary directory for the new charts only
237-
mkdir -p ../temp_charts
238-
for chart in $NEW_CHARTS; do
239-
cp charts/"$chart" ../temp_charts/
240-
done
241-
242-
# Generate an index for just the new charts with the proper URL path
243-
cd ../temp_charts
244-
helm repo index . --url https://deploystackio.github.io/deploy-templates/charts
245-
cd ../gh-pages
246-
247-
# For each new/updated chart, replace its entry in the main index
248-
for chart in $NEW_CHARTS; do
249-
# Extract chart name from filename (removes version and extension)
250-
CHART_NAME=$(echo "$chart" | sed -E 's/(.+)-[0-9]+\.[0-9]+\.[0-9]+\.tgz/\1/')
251-
echo "Processing chart name: $CHART_NAME"
252-
253-
# Check if this chart exists in the new index
254-
if grep -q "name: $CHART_NAME" ../temp_charts/index.yaml; then
255-
# Extract the updated entry from new index
256-
yq eval ".entries.$CHART_NAME" ../temp_charts/index.yaml > temp_entry.yaml
257-
258-
# Replace the entry in the main index
259-
yq eval -i ".entries.$CHART_NAME = load(\"temp_entry.yaml\")" index.yaml
260-
echo "Updated entry for $CHART_NAME in main index"
261-
fi
262-
done
263-
264-
# Update the generated timestamp in index.yaml
265-
yq eval -i ".generated = \"$(date -u +"%Y-%m-%dT%H:%M:%S.%NZ")\"" index.yaml
266-
267-
# Clean up
268-
rm -f temp_entry.yaml
217+
# Update existing index
218+
helm repo index . --url https://deploystackio.github.io/deploy-templates/charts --merge index.yaml
269219
fi
270220
271-
# Remove any existing timestamp comments
272-
sed -i '/^# Charts updated from main branch on/d' index.yaml
273-
274-
# Add a single new metadata annotation
275-
echo "" >> index.yaml
221+
# Add timestamp comment
276222
echo "# Charts updated from main branch on $(date)" >> index.yaml
277-
278-
- name: Commit and push to gh-pages
279-
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
280-
run: |
281-
cd gh-pages
282223
224+
# Configure git
283225
git config user.name "GitHub Actions Bot"
284226
git config user.email "[email protected]"
285227
286-
# Add the index.yaml file
228+
# Add changes
287229
git add index.yaml
230+
git add charts/*.tgz
288231
289-
# Add the chart files in the charts directory
290-
for file in ../.charts/*.tgz; do
291-
basename=$(basename "$file")
292-
git add charts/"$basename"
293-
done
294-
295-
# Only commit if there are changes
296-
if git diff --staged --quiet; then
297-
echo "No changes to commit"
298-
else
299-
git commit -m "Update Helm charts from main branch"
232+
# Commit and push with retry logic
233+
if ! git diff --staged --quiet; then
234+
git commit -m "Update Helm repository"
300235
301-
# Push changes with retry logic
302236
MAX_RETRIES=5
303237
RETRY_COUNT=0
304238
305239
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
306-
# Pull with rebase to get latest changes first
240+
# Pull with rebase first to get latest changes
307241
git pull --rebase origin gh-pages
308242
309243
if git push origin gh-pages; then
310-
echo "Successfully pushed changes to gh-pages"
244+
echo "Successfully pushed to gh-pages"
311245
break
312246
else
313247
RETRY_COUNT=$((RETRY_COUNT+1))
@@ -320,4 +254,6 @@ jobs:
320254
fi
321255
fi
322256
done
257+
else
258+
echo "No changes to commit"
323259
fi

0 commit comments

Comments
 (0)