fix: Improve values.yaml update mechanism with sed instead of awk
Some checks failed
Helm Chart Release / release-chart (push) Successful in 13s
Update Docker Images and Helm Chart / update (push) Failing after 16s

Replace complex awk-based parsing with simpler sed-based approach for updating
Docker image tags in values.yaml. The previous awk logic had issues detecting
changes properly.

Changes:
- Use sed for both WordPress and Nginx tag replacements (more reliable)
- Simplify version extraction using sed and cut commands
- Fix backfile creation to avoid overwriting during edits
- Add debug output showing current and new versions
- Improve error handling in diff command (redirect stderr)

The sed approach:
1. Matches the image section (wordpress: or nginx:)
2. Performs substitution until the next top-level key
3. Directly replaces the entire tag value with quotes

This ensures:
- Correct YAML structure preservation
- Reliable change detection
- Accurate version comparison for Chart.yaml update decision

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-12 09:22:47 +09:00
parent d970cfa84a
commit 0a1f08aa79

View File

@@ -159,47 +159,31 @@ jobs:
cp values.yaml values.yaml.bak cp values.yaml values.yaml.bak
# 現在のWordPressバージョンを取得Chart.yamlの更新判定用 # 現在のWordPressバージョンを取得Chart.yamlの更新判定用
CURRENT_WP_VERSION=$(grep -A 3 'wordpress:' values.yaml.bak | grep 'tag:' | awk -F'"' '{print $2}') CURRENT_WP_VERSION=$(grep -A 1 'wordpress:' values.yaml | grep 'tag:' | sed 's/.*tag: "\([^-]*\).*/\1/')
NEW_WP_VERSION="${{ steps.wordpress.outputs.version }}" NEW_WP_VERSION=$(echo "${{ steps.wordpress.outputs.version }}" | cut -d'-' -f1)
# 変更前のバージョンを表示
CURRENT_NGINX=$(grep -A 1 'nginx:' values.yaml | grep 'tag:' | sed 's/.*tag: "\(.*\)".*/\1/')
echo "Current versions:"
echo " WordPress: $CURRENT_WP_VERSION"
echo " Nginx: $CURRENT_NGINX"
echo "New versions:"
echo " WordPress: $NEW_WP_VERSION"
echo " Nginx: ${{ steps.nginx.outputs.version }}"
# WordPressのtagを更新 # WordPressのtagを更新
# image.wordpress.tagの行を特定して置換 sed -i "/^ wordpress:/,/^ [a-z]/s|tag: \"[^\"]*\"|tag: \"${{ steps.wordpress.outputs.version }}\"|" values.yaml
awk -v new_tag="${{ steps.wordpress.outputs.version }}" '
/^image:/ { in_image=1 }
in_image && /^ wordpress:/ { in_wordpress=1; print; next }
in_wordpress && /^ tag:/ {
print " tag: \"" new_tag "\""
in_wordpress=0
next
}
in_wordpress && /^ [a-z]/ { in_wordpress=0 }
in_image && /^[a-z]/ { in_image=0 }
{ print }
' values.yaml.bak > values.yaml.tmp
mv values.yaml.tmp values.yaml
# Nginxのtagを更新 # Nginxのtagを更新
cp values.yaml values.yaml.tmp sed -i "/^ nginx:/,/^ [a-z]/s|tag: \"[^\"]*\"|tag: \"${{ steps.nginx.outputs.version }}\"|" values.yaml
awk -v new_tag="${{ steps.nginx.outputs.version }}" '
/^image:/ { in_image=1 }
in_image && /^ nginx:/ { in_nginx=1; print; next }
in_nginx && /^ tag:/ {
print " tag: \"" new_tag "\""
in_nginx=0
next
}
in_nginx && /^ [a-z]/ { in_nginx=0 }
in_image && /^[a-z]/ { in_image=0 }
{ print }
' values.yaml.tmp > values.yaml
rm values.yaml.tmp
# 変更内容を表示 # 変更内容を表示
echo ""
echo "=== Changes in values.yaml ===" echo "=== Changes in values.yaml ==="
diff values.yaml.bak values.yaml || true diff values.yaml.bak values.yaml || true
# 実際に変更されたか確認 # 実際に変更されたか確認
if diff -q values.yaml.bak values.yaml > /dev/null; then if diff -q values.yaml.bak values.yaml > /dev/null 2>&1; then
echo "INFO: No changes were made to values.yaml (versions already up to date)" echo "INFO: No changes were made to values.yaml (versions already up to date)"
cat values.yaml | grep -A 5 "image:" cat values.yaml | grep -A 5 "image:"
echo "chart_version_update_needed=false" >> $GITHUB_OUTPUT echo "chart_version_update_needed=false" >> $GITHUB_OUTPUT
@@ -216,8 +200,9 @@ jobs:
fi fi
fi fi
echo ""
echo "=== Updated values.yaml (image section) ===" echo "=== Updated values.yaml (image section) ==="
grep -A 10 "^image:" values.yaml grep -A 5 "^image:" values.yaml
- name: Increment chart version - name: Increment chart version
if: steps.update_values.outputs.chart_version_update_needed == 'true' if: steps.update_values.outputs.chart_version_update_needed == 'true'