Skip to content

Commit 0165b58

Browse files
committed
feat: add PR-based workflow to Helm chart updates with enhanced metadata tracking
1 parent 1bda867 commit 0165b58

File tree

1 file changed

+99
-4
lines changed

1 file changed

+99
-4
lines changed

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

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ name: "Update Helm Chart"
33
# Reusable workflow for updating Helm charts from dispatch payload
44
# Receives a JSON payload with chart name, components, versions, and env vars
55
# Updates values.yaml, Chart.yaml (appVersion), and optionally configmap/secret templates
6+
# Creates a PR for review instead of pushing directly to the target branch
67
#
78
# Usage:
89
# jobs:
910
# update:
1011
# uses: LerianStudio/github-actions-shared-workflows/.github/workflows/helm-update-chart.yml@main
1112
# with:
1213
# payload: ${{ inputs.payload }}
14+
# base_branch: develop
1315
# secrets: inherit
1416

1517
on:
@@ -19,6 +21,10 @@ on:
1921
description: 'JSON payload with chart, components, and metadata'
2022
type: string
2123
required: true
24+
base_branch:
25+
description: 'Target branch for the PR (default: develop)'
26+
type: string
27+
default: 'main'
2228
scripts_path:
2329
description: 'Path to scripts directory (default: .github/scripts)'
2430
type: string
@@ -74,8 +80,18 @@ jobs:
7480
PAYLOAD='${{ inputs.payload }}'
7581
echo "$PAYLOAD" | jq .
7682
77-
echo "chart=$(echo "$PAYLOAD" | jq -r '.chart')" >> $GITHUB_OUTPUT
78-
echo "has_new_env_vars=$(echo "$PAYLOAD" | jq -r '.has_new_env_vars')" >> $GITHUB_OUTPUT
83+
CHART=$(echo "$PAYLOAD" | jq -r '.chart')
84+
HAS_NEW_ENV_VARS=$(echo "$PAYLOAD" | jq -r '.has_new_env_vars')
85+
SOURCE_REF=$(echo "$PAYLOAD" | jq -r '.source_ref // "unknown"')
86+
87+
echo "chart=${CHART}" >> $GITHUB_OUTPUT
88+
echo "has_new_env_vars=${HAS_NEW_ENV_VARS}" >> $GITHUB_OUTPUT
89+
echo "source_ref=${SOURCE_REF}" >> $GITHUB_OUTPUT
90+
91+
# Generate branch name
92+
TIMESTAMP=$(date +%Y%m%d%H%M%S)
93+
BRANCH_NAME="update/${CHART}/${SOURCE_REF}-${TIMESTAMP}"
94+
echo "branch_name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
7995
8096
# Save components array to file for processing
8197
echo "$PAYLOAD" | jq -c '.components' > /tmp/components.json
@@ -84,6 +100,7 @@ jobs:
84100
uses: actions/checkout@v4
85101
with:
86102
token: ${{ steps.app-token.outputs.token }}
103+
ref: ${{ inputs.base_branch }}
87104
fetch-depth: 0
88105

89106
- name: Import GPG key
@@ -104,6 +121,10 @@ jobs:
104121
git config user.name "${{ secrets.GIT_USER_NAME }}"
105122
git config user.email "${{ secrets.GIT_USER_EMAIL }}"
106123
124+
- name: Create feature branch
125+
run: |
126+
git checkout -b "${{ steps.payload.outputs.branch_name }}"
127+
107128
- name: Setup Go
108129
if: ${{ inputs.update_readme }}
109130
uses: actions/setup-go@v5
@@ -273,7 +294,8 @@ jobs:
273294
--app-version "${APP_VERSION}"
274295
done
275296
276-
- name: Commit and push changes
297+
- name: Commit changes
298+
id: commit
277299
run: |
278300
CHART="${{ steps.payload.outputs.chart }}"
279301
UPDATED_COMPONENTS="${{ steps.process.outputs.updated_components }}"
@@ -284,9 +306,12 @@ jobs:
284306
# Check if there are changes to commit
285307
if git diff --staged --quiet; then
286308
echo "No changes to commit"
309+
echo "has_changes=false" >> $GITHUB_OUTPUT
287310
exit 0
288311
fi
289312
313+
echo "has_changes=true" >> $GITHUB_OUTPUT
314+
290315
# Determine commit message based on env_vars
291316
# feat: when new environment variables are added (requires attention)
292317
# fix: when it's just a version bump (routine update)
@@ -296,18 +321,88 @@ jobs:
296321
COMMIT_MSG="fix(${CHART}): update ${UPDATED_COMPONENTS}"
297322
fi
298323
324+
echo "commit_msg=${COMMIT_MSG}" >> $GITHUB_OUTPUT
299325
echo "Committing with message: ${COMMIT_MSG}"
300326
git commit -m "${COMMIT_MSG}"
301-
git push
327+
328+
- name: Push branch and create PR
329+
if: steps.commit.outputs.has_changes == 'true'
330+
env:
331+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
332+
run: |
333+
CHART="${{ steps.payload.outputs.chart }}"
334+
BRANCH_NAME="${{ steps.payload.outputs.branch_name }}"
335+
BASE_BRANCH="${{ inputs.base_branch }}"
336+
COMMIT_MSG="${{ steps.commit.outputs.commit_msg }}"
337+
HAS_NEW_ENV_VARS="${{ steps.payload.outputs.has_new_env_vars }}"
338+
UPDATED_COMPONENTS="${{ steps.process.outputs.updated_components }}"
339+
340+
# Push the branch
341+
git push -u origin "${BRANCH_NAME}"
342+
343+
# Build PR body
344+
COMPONENTS=$(cat /tmp/components.json)
345+
346+
# Determine PR title prefix
347+
if [ "${HAS_NEW_ENV_VARS}" = "true" ]; then
348+
PR_TITLE="feat(${CHART}): update ${UPDATED_COMPONENTS}"
349+
ATTENTION_NOTE="> ⚠️ **Attention:** This PR includes new environment variables that may require configuration."
350+
else
351+
PR_TITLE="fix(${CHART}): update ${UPDATED_COMPONENTS}"
352+
ATTENTION_NOTE=""
353+
fi
354+
355+
# Build components table
356+
COMPONENTS_TABLE=$(echo "$COMPONENTS" | jq -r '
357+
["| Component | Version | New Env Vars |", "|-----------|---------|--------------|"] +
358+
[.[] | "| \(.name) | \(.version) | \(.env_vars | if . == {} then "-" else (. | keys | join(", ")) end) |"]
359+
| .[]
360+
')
361+
362+
# Create PR
363+
PR_URL=$(gh pr create \
364+
--base "${BASE_BRANCH}" \
365+
--head "${BRANCH_NAME}" \
366+
--title "${PR_TITLE}" \
367+
--body "$(cat <<EOF
368+
## Summary
369+
370+
Automated update of Helm chart \`${CHART}\` components.
371+
372+
${ATTENTION_NOTE}
373+
374+
## Components Updated
375+
376+
${COMPONENTS_TABLE}
377+
378+
---
379+
*This PR was automatically generated by the CI/CD pipeline.*
380+
EOF
381+
)")
382+
383+
echo "PR created: ${PR_URL}"
384+
echo "pr_url=${PR_URL}" >> $GITHUB_OUTPUT
302385

303386
- name: Summary
304387
run: |
305388
COMPONENTS=$(cat /tmp/components.json)
306389
CHART="${{ steps.payload.outputs.chart }}"
390+
BRANCH_NAME="${{ steps.payload.outputs.branch_name }}"
391+
HAS_CHANGES="${{ steps.commit.outputs.has_changes }}"
307392
308393
echo "### Helm Chart Update Summary" >> $GITHUB_STEP_SUMMARY
309394
echo "" >> $GITHUB_STEP_SUMMARY
310395
echo "**Chart:** \`${CHART}\`" >> $GITHUB_STEP_SUMMARY
396+
echo "**Branch:** \`${BRANCH_NAME}\`" >> $GITHUB_STEP_SUMMARY
397+
echo "**Base:** \`${{ inputs.base_branch }}\`" >> $GITHUB_STEP_SUMMARY
398+
echo "" >> $GITHUB_STEP_SUMMARY
399+
400+
if [ "${HAS_CHANGES}" = "true" ]; then
401+
echo "✅ **PR created successfully**" >> $GITHUB_STEP_SUMMARY
402+
else
403+
echo "ℹ️ **No changes detected**" >> $GITHUB_STEP_SUMMARY
404+
fi
405+
311406
echo "" >> $GITHUB_STEP_SUMMARY
312407
echo "**Components:**" >> $GITHUB_STEP_SUMMARY
313408
echo "" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)