Skip to content

Commit f40c0a1

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

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

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

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

2526
- name: Check for deleted Helm directories
2627
id: check-deleted
@@ -38,8 +39,9 @@ jobs:
3839
with:
3940
version: 'latest'
4041

41-
- name: Find changed Helm charts and increment versions
42+
- name: Find changed Helm charts
4243
if: env.SKIP_WORKFLOW != 'true'
44+
id: find-charts
4345
run: |
4446
# Find changed files in this commit
4547
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD | grep "helm/" || echo "")
@@ -66,29 +68,37 @@ jobs:
6668
exit 0
6769
fi
6870
69-
# Create directory for packaged charts
70-
mkdir -p .charts
71-
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: |
7278
# Configure git for possible commits
7379
git config user.name "GitHub Actions Bot"
7480
git config user.email "[email protected]"
7581
76-
# Process each chart - increment version if needed
77-
for CHART_DIR in "${!CHART_DIRS[@]}"; do
82+
# Create directory for packaged charts
83+
mkdir -p .charts
84+
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
7888
echo "Processing chart in $CHART_DIR"
7989
8090
# Get current version
8191
CURRENT_VERSION=$(grep "version:" $CHART_DIR/Chart.yaml | awk '{print $2}')
8292
echo "Current version: $CURRENT_VERSION"
8393
84-
# Check if this is a new commit that hasn't been deployed yet
85-
# by comparing the chart version with what's in the index.yaml
94+
# Get chart name
8695
CHART_NAME=$(grep "name:" $CHART_DIR/Chart.yaml | awk '{print $2}')
87-
NEEDS_INCREMENT=false
8896
8997
# Pull gh-pages branch to check current versions
9098
mkdir -p temp_ghpages
9199
git fetch origin gh-pages:gh-pages || true
100+
101+
NEEDS_INCREMENT=false
92102
if git show gh-pages:index.yaml > temp_ghpages/index.yaml 2>/dev/null; then
93103
if grep -q "name: $CHART_NAME" temp_ghpages/index.yaml; then
94104
if grep -q "version: $CURRENT_VERSION" temp_ghpages/index.yaml; then
@@ -121,22 +131,47 @@ jobs:
121131
# Update Chart.yaml with new version
122132
sed -i "s/version: $CURRENT_VERSION/version: $NEW_VERSION/" $CHART_DIR/Chart.yaml
123133
124-
# Commit the version change
134+
# Stage the changes
125135
git add $CHART_DIR/Chart.yaml
126-
git commit -m "Automatically increment $CHART_NAME version to $NEW_VERSION [skip ci]"
127-
git push origin main
128136
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
129144
145+
# Commit the changes
146+
git commit -m "Automatically increment Helm chart versions [skip ci]"
147+
148+
# Push the branch and create a PR
149+
git push origin $BRANCH_NAME
150+
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
156+
157+
# Checkout main again for the next steps
158+
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."
163+
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
130171
# Package chart
131172
helm package $CHART_DIR -d .charts/
132173
echo "Packaged chart in $CHART_DIR"
133174
done
134-
135-
if [ -z "$(ls -A .charts/ 2>/dev/null)" ]; then
136-
echo "HELM_EXISTS=false" >> $GITHUB_ENV
137-
else
138-
echo "HELM_EXISTS=true" >> $GITHUB_ENV
139-
fi
140175
141176
- name: Checkout gh-pages branch
142177
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
@@ -147,7 +182,7 @@ jobs:
147182
token: ${{ secrets.APP_INSTALLATION_TOKEN }}
148183
fetch-depth: 0
149184

150-
- name: Custom indexing approach
185+
- name: Update Helm index and charts
151186
if: env.SKIP_WORKFLOW != 'true' && env.HELM_EXISTS == 'true'
152187
run: |
153188
cd gh-pages
@@ -162,6 +197,10 @@ jobs:
162197
NEW_CHARTS=$(ls ../.charts/*.tgz | xargs -n1 basename)
163198
echo "New/updated charts: $NEW_CHARTS"
164199
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+
165204
# If there's no index.yaml, create a new one with the proper URL path
166205
if [ ! -f "index.yaml" ] || [ ! -s "index.yaml" ]; then
167206
helm repo index . --url https://deploystackio.github.io/deploy-templates/charts
@@ -180,11 +219,6 @@ jobs:
180219
helm repo index . --url https://deploystackio.github.io/deploy-templates/charts
181220
cd ../gh-pages
182221
183-
# Now manually merge the indexes using yq
184-
# Install yq
185-
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
186-
sudo chmod +x /usr/local/bin/yq
187-
188222
# For each new/updated chart, replace its entry in the main index
189223
for chart in $NEW_CHARTS; do
190224
# Extract chart name from filename (removes version and extension)
@@ -240,10 +274,11 @@ jobs:
240274
git commit -m "Update Helm charts from main branch"
241275
242276
# Push changes with retry logic
243-
MAX_RETRIES=3
277+
MAX_RETRIES=5
244278
RETRY_COUNT=0
245279
246280
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
281+
# Pull with rebase to get latest changes first
247282
git pull --rebase origin gh-pages
248283
249284
if git push origin gh-pages; then
@@ -252,8 +287,8 @@ jobs:
252287
else
253288
RETRY_COUNT=$((RETRY_COUNT+1))
254289
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
255-
echo "Push failed, retrying in 5 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
256-
sleep 5
290+
echo "Push failed, retrying in 10 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
291+
sleep 10
257292
else
258293
echo "Failed to push after $MAX_RETRIES attempts"
259294
exit 1

0 commit comments

Comments
 (0)