diff --git a/.github/workflows/argocd-promote-helm.yaml b/.github/workflows/argocd-promote-helm.yaml index 8d9a7b6..0d527d6 100644 --- a/.github/workflows/argocd-promote-helm.yaml +++ b/.github/workflows/argocd-promote-helm.yaml @@ -51,6 +51,12 @@ on: required: false default: "" + merge_existing_values: + type: boolean + required: false + default: true + description: "Merge previously deployed runtime values. Set false for authoritative values, so omitted keys are removed." + preview: type: boolean required: false @@ -444,23 +450,26 @@ jobs: - name: Merge Argo Application Values id: merge_values + env: + MERGE_EXISTING_VALUES: ${{ inputs.merge_existing_values }} run: | - # Merge the values, with new values taking precedence - MERGED_VALUES=$(yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' existing_values.yaml new_values.yaml | yq '.' --prettyPrint --no-colors) - + if [ "$MERGE_EXISTING_VALUES" = "true" ]; then + # Preserve existing callers' behavior: new values override matching keys. + MERGED_VALUES=$(yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' existing_values.yaml new_values.yaml | yq '.' --prettyPrint --no-colors) + else + # A complete preview render is authoritative. Do not resurrect keys removed by reset. + MERGED_VALUES=$(yq '.' new_values.yaml --prettyPrint --no-colors) + fi if [ -z "$MERGED_VALUES" ] || [ "$MERGED_VALUES" = "null" ]; then - echo "{}" > merged_values.yaml - echo "has_values=false" >> $GITHUB_OUTPUT + MERGED_VALUES="{}" + fi + echo "$MERGED_VALUES" > merged_values.yaml + if [ "$MERGE_EXISTING_VALUES" = "false" ] || [ "$MERGED_VALUES" != "{}" ]; then + # Replacement must also write {}, otherwise a reset to defaults retains old values. + echo "has_values=true" >> "$GITHUB_OUTPUT" else - echo "$MERGED_VALUES" > merged_values.yaml - # Check if merged values is just an empty object - if [ "$MERGED_VALUES" = "{}" ]; then - echo "has_values=false" >> $GITHUB_OUTPUT - else - echo "has_values=true" >> $GITHUB_OUTPUT - fi + echo "has_values=false" >> "$GITHUB_OUTPUT" fi - echo "Merged values:" cat merged_values.yaml diff --git a/.github/workflows/on-pr.yaml b/.github/workflows/on-pr.yaml index e8ef395..9a3c686 100644 --- a/.github/workflows/on-pr.yaml +++ b/.github/workflows/on-pr.yaml @@ -9,6 +9,18 @@ permissions: pull-requests: write jobs: + test-values-merge: + name: Values merge and replacement regression + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v7 + - name: Exercise the workflow's actual values step + run: | + yq -o=json .github/workflows/argocd-promote-helm.yaml > "$RUNNER_TEMP/promotion.json" + python3 tests/test_values_merge.py "$RUNNER_TEMP/promotion.json" + # Release promotions would not normally go in PRs, but we include them here for testing purposes test-release--promotion--dry-run: diff --git a/tests/test_values_merge.py b/tests/test_values_merge.py new file mode 100644 index 0000000..5be8ba4 --- /dev/null +++ b/tests/test_values_merge.py @@ -0,0 +1,32 @@ +"""Run the actual promotion workflow's merge step against persisted-value regressions.""" +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +workflow = json.loads(Path(sys.argv[1]).read_text()) +assert workflow['on']['workflow_call']['inputs']['merge_existing_values']['default'] is True +step = next(s for s in workflow['jobs']['argocd-promote-helm']['steps'] if s.get('id') == 'merge_values') +assert step['env']['MERGE_EXISTING_VALUES'] == '${{ inputs.merge_existing_values }}' + +cases = [ + ('default merge preserves older overrides', True, {'env': {'OLD': 'keep', 'SAME': 'before'}}, {'env': {'SAME': 'after'}}, {'env': {'OLD': 'keep', 'SAME': 'after'}}, True), + ('replacement removes omitted override', False, {'env': {'SMOKE': 'remove', 'SAME': 'before'}}, {'env': {'SAME': 'after'}}, {'env': {'SAME': 'after'}}, True), + ('replacement clears all values', False, {'env': {'SMOKE': 'remove'}}, {}, {}, True), + ('empty default merge stays a no-op', True, {}, {}, {}, False), +] +for name, merge, before, new, expected, has_values in cases: + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + (root / 'existing_values.yaml').write_text(json.dumps(before)) + (root / 'new_values.yaml').write_text(json.dumps(new)) + output = root / 'outputs' + subprocess.run(['bash', '-e', '-c', step['run']], cwd=root, + env=dict(os.environ, MERGE_EXISTING_VALUES=str(merge).lower(), GITHUB_OUTPUT=str(output)), + check=True, capture_output=True, text=True) + rendered = json.loads(subprocess.check_output(['yq', '-o=json', '.', 'merged_values.yaml'], cwd=root)) + assert rendered == expected, (name, rendered) + assert f'has_values={str(has_values).lower()}' in output.read_text(), name + print('PASS:', name)