Skip to content

Commit 82911c2

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

1 file changed

Lines changed: 86 additions & 61 deletions

File tree

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

Lines changed: 86 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ jobs:
3939
with:
4040
version: 'latest'
4141

42-
- name: Find changed Helm charts
42+
- name: Process Helm charts
4343
if: env.SKIP_WORKFLOW != 'true'
44-
id: find-charts
44+
id: process-charts
4545
run: |
4646
# Find changed files in this commit
4747
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD | grep "helm/" || echo "")
@@ -68,23 +68,22 @@ jobs:
6868
exit 0
6969
fi
7070
71-
# Save chart directories to process later
72-
echo "CHART_DIRS=$(echo "${!CHART_DIRS[@]}" | tr ' ' ',')" >> $GITHUB_OUTPUT
73-
echo "HELM_EXISTS=true" >> $GITHUB_ENV
74-
75-
- name: Increment chart versions if needed
76-
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
77-
run: |
71+
# Create directory for packaged charts
72+
mkdir -p .charts
73+
74+
# Pull gh-pages branch to check current versions
75+
mkdir -p temp_ghpages
76+
git fetch origin gh-pages:gh-pages || true
77+
7878
# Configure git for possible commits
7979
git config user.name "GitHub Actions Bot"
8080
git config user.email "actions@github.com"
8181
82-
# Create directory for packaged charts
83-
mkdir -p .charts
82+
# Version increment needed for any charts?
83+
VERSION_INCREMENT_NEEDED=false
8484
85-
# Process each chart - check if version increment is needed
86-
IFS=',' read -ra DIRS <<< "${{ steps.find-charts.outputs.CHART_DIRS }}"
87-
for CHART_DIR in "${DIRS[@]}"; do
85+
# First pass - check which charts need version increment
86+
for CHART_DIR in "${!CHART_DIRS[@]}"; do
8887
echo "Processing chart in $CHART_DIR"
8988
9089
# Get current version
@@ -94,17 +93,15 @@ jobs:
9493
# Get chart name
9594
CHART_NAME=$(grep "name:" $CHART_DIR/Chart.yaml | awk '{print $2}')
9695
97-
# Pull gh-pages branch to check current versions
98-
mkdir -p temp_ghpages
99-
git fetch origin gh-pages:gh-pages || true
100-
96+
# Check if this chart exists in the index with same version
10197
NEEDS_INCREMENT=false
10298
if git show gh-pages:index.yaml > temp_ghpages/index.yaml 2>/dev/null; then
10399
if grep -q "name: $CHART_NAME" temp_ghpages/index.yaml; then
104100
if grep -q "version: $CURRENT_VERSION" temp_ghpages/index.yaml; then
105101
echo "Chart $CHART_NAME with version $CURRENT_VERSION already exists in index"
106-
echo "Incrementing patch version"
102+
echo "Will increment patch version"
107103
NEEDS_INCREMENT=true
104+
VERSION_INCREMENT_NEEDED=true
108105
else
109106
echo "Chart version has already been changed in source, not incrementing"
110107
fi
@@ -115,8 +112,33 @@ jobs:
115112
echo "No index.yaml found on gh-pages branch, using current version"
116113
fi
117114
118-
# Increment version if needed
115+
# Store info for the next pass
119116
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}')
141+
120142
# Split version into parts
121143
MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1)
122144
MINOR=$(echo $CURRENT_VERSION | cut -d. -f2)
@@ -126,52 +148,53 @@ jobs:
126148
NEW_PATCH=$((PATCH + 1))
127149
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
128150
129-
echo "Incrementing version from $CURRENT_VERSION to $NEW_VERSION"
151+
echo "Incrementing version for $CHART_NAME from $CURRENT_VERSION to $NEW_VERSION"
130152
131153
# Update Chart.yaml with new version
132154
sed -i "s/version: $CURRENT_VERSION/version: $NEW_VERSION/" $CHART_DIR/Chart.yaml
133155
134-
# Stage the changes
156+
# Add to git
135157
git add $CHART_DIR/Chart.yaml
136-
fi
137-
done
138-
139-
# If there are changes to commit, create a pull request instead of direct push
140-
if ! git diff --staged --quiet; then
141-
# Create a new branch for our changes
142-
BRANCH_NAME="chart-version-update-$(date +%s)"
143-
git checkout -b $BRANCH_NAME
158+
done < charts_to_increment.txt
144159
145160
# Commit the changes
146161
git commit -m "Automatically increment Helm chart versions [skip ci]"
147162
148-
# Push the branch and create a PR
149-
git push origin $BRANCH_NAME
163+
# Try to push the branch, with retry logic
164+
MAX_RETRIES=5
165+
RETRY_COUNT=0
166+
PUSH_SUCCESS=false
150167
151-
# Create a PR using GitHub CLI (needs to be installed in the workflow)
152-
gh pr create --title "Update Helm chart versions" \
153-
--body "Automatically incrementing chart versions for publishing" \
154-
--base main \
155-
--head $BRANCH_NAME
168+
while [ $RETRY_COUNT -lt $MAX_RETRIES ] && [ "$PUSH_SUCCESS" != "true" ]; do
169+
if git push origin $BRANCH_NAME; then
170+
echo "Successfully pushed branch $BRANCH_NAME"
171+
PUSH_SUCCESS=true
172+
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"
181+
else
182+
RETRY_COUNT=$((RETRY_COUNT+1))
183+
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
184+
echo "Push failed, retrying in 10 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
185+
sleep 10
186+
else
187+
echo "Failed to push after $MAX_RETRIES attempts, but will continue with publishing"
188+
fi
189+
fi
190+
done
156191
157-
# Checkout main again for the next steps
192+
# Switch back to main branch to continue with publishing
158193
git checkout main
159-
160-
# Since we've created a PR, we need to use the current versions
161-
# But we should wait for the PR to be merged before continuing
162-
echo "Created version update PR. Will package current versions for now."
163194
fi
164-
165-
- name: Package Helm charts
166-
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
167-
run: |
168-
# Process each chart - package it
169-
IFS=',' read -ra DIRS <<< "${{ steps.find-charts.outputs.CHART_DIRS }}"
170-
for CHART_DIR in "${DIRS[@]}"; do
171-
# Package chart
172-
helm package $CHART_DIR -d .charts/
173-
echo "Packaged chart in $CHART_DIR"
174-
done
195+
196+
# Always set HELM_EXISTS=true if we got this far
197+
echo "HELM_EXISTS=true" >> $GITHUB_ENV
175198
176199
- name: Checkout gh-pages branch
177200
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
@@ -182,7 +205,13 @@ jobs:
182205
token: ${{ secrets.APP_INSTALLATION_TOKEN }}
183206
fetch-depth: 0
184207

185-
- name: Update Helm index and charts
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
213+
214+
- name: Update Helm repository
186215
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
187216
run: |
188217
cd gh-pages
@@ -197,10 +226,6 @@ jobs:
197226
NEW_CHARTS=$(ls ../.charts/*.tgz | xargs -n1 basename)
198227
echo "New/updated charts: $NEW_CHARTS"
199228
200-
# Install yq for YAML processing
201-
sudo wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
202-
sudo chmod +x /usr/bin/yq
203-
204229
# If there's no index.yaml, create a new one with the proper URL path
205230
if [ ! -f "index.yaml" ] || [ ! -s "index.yaml" ]; then
206231
helm repo index . --url https://deploystackio.github.io/deploy-templates/charts
@@ -282,13 +307,13 @@ jobs:
282307
git pull --rebase origin gh-pages
283308
284309
if git push origin gh-pages; then
285-
echo "Successfully pushed changes"
310+
echo "Successfully pushed changes to gh-pages"
286311
break
287312
else
288313
RETRY_COUNT=$((RETRY_COUNT+1))
289314
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
290-
echo "Push failed, retrying in 10 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
291-
sleep 10
315+
echo "Push failed, retrying in 15 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
316+
sleep 15
292317
else
293318
echo "Failed to push after $MAX_RETRIES attempts"
294319
exit 1

0 commit comments

Comments
 (0)