Skip to content

Commit d51ee5d

Browse files
merllsvcAPLBot
andauthored
ci: compare using separate working copies (#2909)
Co-authored-by: svcAPLBot <174728082+svcAPLBot@users.noreply.github.com>
1 parent 11f4fc7 commit d51ee5d

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

.github/workflows/svcaplbot-run-dyff.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ permissions:
1616
jobs:
1717
check-commit:
1818
runs-on: ubuntu-latest
19+
# Skip this pre-check check when triggered manually
20+
if: github.event_name != 'workflow_dispatch'
1921
outputs:
2022
skip: ${{ steps.check.outputs.skip }}
2123
steps:
@@ -40,7 +42,7 @@ jobs:
4042
run-compare:
4143
runs-on: ubuntu-latest
4244
needs: check-commit
43-
if: ${{ needs.check-commit.outputs.skip != 'true' }}
45+
if: ${{ github.event_name == 'workflow_dispatch' || (success() && needs.check-commit.outputs.skip != 'true') }}
4446
steps:
4547
- name: Debug
4648
run: |
@@ -49,17 +51,22 @@ jobs:
4951
run: |
5052
sudo apt update
5153
sudo apt install gh -y
52-
- name: Checkout repository
54+
- name: Checkout PR branch
5355
uses: actions/checkout@v6
5456
with:
5557
ref: ${{ github.event.pull_request.head.sha }}
56-
fetch-depth: 0
58+
path: pr
59+
- name: Checkout base branch
60+
uses: actions/checkout@v6
61+
with:
62+
ref: ${{ github.event.pull_request.base.ref || 'main' }}
63+
path: base
5764
- name: Set up Node.js
5865
uses: actions/setup-node@v6
5966
with:
60-
node-version-file: '.nvmrc'
67+
node-version-file: 'pr/.nvmrc'
6168
- name: Install npm dependencies
62-
run: npm install --no-save
69+
run: cd "$GITHUB_WORKSPACE/pr" && npm install --no-save && cd "$GITHUB_WORKSPACE/base" && npm install --no-save
6370
- name: Install Helm and Helmfile
6471
uses: helmfile/helmfile-action@v2.1.1
6572
with:
@@ -79,12 +86,13 @@ jobs:
7986
env:
8087
GH_TOKEN: ${{ secrets.BOT_TOKEN }}
8188
run: |
82-
bin/compare.sh --diff-output tmp/diff-output.txt
89+
mkdir -p tmp
90+
"$GITHUB_WORKSPACE/pr/bin/compare.sh" "$GITHUB_WORKSPACE/base" "$GITHUB_WORKSPACE/pr" --diff-output tmp/diff-output.txt
8391
diff_text=$(cat tmp/diff-output.txt)
8492
format_code_start='```diff'
8593
format_code_end='```'
8694
comment_text="Comparison of Helm chart templating output:
8795
$format_code_start
8896
$diff_text
8997
$format_code_end"
90-
gh pr comment ${{ github.event.pull_request.number }} --body "$comment_text"
98+
cd "$GITHUB_WORKSPACE/pr" && gh pr comment ${{ github.event.pull_request.number }} --body "$comment_text"

bin/compare.sh

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,86 @@
22
set -ue
33

44
# Usage: bin/compare.sh -l name=loki
5+
# Alternative: bin/compare.sh base-branch-copy feat-branch-copy -l name=loki
56
# Optional: --diff-output <filename> writes the final diff to a file and skips common chart version information
67
# Deps: dyff, helmfile
78
# brew install homeport/tap/dyff
89
# brew install helmfile
910

10-
export ENV_DIR=$PWD/tests/fixtures
11-
1211
diffOutput=
1312
templateArgs=
13+
srcDirA=
14+
srcDirB=
15+
targetDirA=
16+
targetDirB=
1417
# Process arguments
1518
while [ $# -ne 0 ]; do
1619
case "$1" in
1720
--diff-output)
1821
diffOutput="$2"
1922
shift
2023
;;
21-
*)
22-
# Forward everything else to the template command
24+
-*)
25+
# Forward other options to the template command
2326
templateArgs=("${templateArgs[@]}" "$1")
2427
;;
28+
*)
29+
# Process first two positional arguments as potential working directories
30+
if [ -z "$srcDirA" ]; then
31+
srcDirA=$(realpath "$1")
32+
elif [ -z "$srcDirB" ]; then
33+
srcDirB=$(realpath "$1")
34+
else
35+
# Forward everything else to the template command
36+
templateArgs=("${templateArgs[@]}" "$1")
37+
fi
38+
;;
2539
esac
2640
shift
2741
done
2842

2943
readonly script_dir=$(dirname "$0")
30-
readonly branchA='main'
31-
# branchB current branch
32-
readonly branchB=$(git rev-parse --abbrev-ref HEAD)
3344

34-
targetDirA="tmp/${branchA}"
35-
targetDirB="tmp/${branchB}"
45+
generate_helm_templates() {
46+
local target_dir="$1"
47+
rm -rf "$target_dir"
48+
node --no-warnings --import tsx src/otomi.ts -- values
49+
helmfile template "${templateArgs[@]}" --output-dir-template="$target_dir/{{.Release.Namespace}}-{{.Release.Name}}"
50+
mv tests/fixtures/values-repo.yaml "$target_dir/values-repo.yaml"
51+
}
3652

3753
export NODE_ENV=test
38-
node --no-warnings --import tsx src/otomi.ts -- values
39-
helmfile template "${templateArgs[@]}" --output-dir-template="$targetDirB/{{.Release.Namespace}}-{{.Release.Name }}"
40-
mv tests/fixtures/values-repo.yaml "$targetDirB/values-repo.yaml"
41-
git -c core.hooksPath=/dev/null checkout -f $branchA
42-
# we remove previously rendered manifests so they are not mixed up with newly rendered
43-
rm -rf $targetDirA
44-
node --no-warnings --import tsx src/otomi.ts -- values
45-
helmfile template "${templateArgs[@]}" --output-dir-template="$targetDirA/{{.Release.Namespace}}-{{.Release.Name}}"
46-
mv tests/fixtures/values-repo.yaml "$targetDirA/values-repo.yaml"
47-
git -c core.hooksPath=/dev/null checkout -f $branchB
54+
if [ -z "$srcDirA" ]; then
55+
branchA='main'
56+
# branchB current branch
57+
branchB=$(git rev-parse --abbrev-ref HEAD)
58+
59+
targetDirA="$PWD/tmp/${branchA}"
60+
targetDirB="$PWD/tmp/${branchB}"
61+
export ENV_DIR="$PWD/tests/fixtures"
62+
generate_helm_templates "$targetDirB"
63+
git -c core.hooksPath=/dev/null checkout -f $branchA
64+
generate_helm_templates "$targetDirA"
65+
git -c core.hooksPath=/dev/null checkout -f $branchB
66+
else
67+
if [ -z "$srcDirB" ]; then
68+
echo "Only one directory passed for comparison"
69+
exit 1
70+
fi
71+
startDir=$PWD
72+
pushd "$srcDirB"
73+
export ENV_DIR="$srcDirB/tests/fixtures"
74+
branchB=$(git rev-parse --abbrev-ref HEAD)
75+
targetDirB="$startDir/tmp/${branchB}"
76+
generate_helm_templates "$targetDirB"
77+
popd
78+
pushd "$srcDirA"
79+
export ENV_DIR="$srcDirA/tests/fixtures"
80+
branchA=$(git rev-parse --abbrev-ref HEAD)
81+
targetDirA="$startDir/tmp/${branchA}"
82+
generate_helm_templates "$targetDirA"
83+
popd
84+
fi
4885

4986
# order of arguments matters so new changes are green color
5087
echo "Comparing $targetDirB with $targetDirA"

bin/dyff.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ while [ $# -ne 0 ]
99
do
1010
case "$1" in
1111
--exclude-chart-versions)
12-
miscArgs=( "${miscArgs[@]}" --exclude-regexp '.*metadata.labels.helm.sh/chart' --exclude-regexp '.*metadata.labels.app.kubernetes.io/version' )
12+
miscArgs=( "${miscArgs[@]}" --exclude-regexp '.*metadata.labels.helm.sh/chart' --exclude-regexp '.*metadata.labels.chart' --exclude-regexp '.*metadata.labels.app.kubernetes.io/version' )
1313
;;
1414
*)
1515
if [ -z "$targetDirA" ]; then
@@ -18,18 +18,18 @@ do
1818
targetDirB=$1
1919
else
2020
echo "Extra argument $1"
21-
return 1
21+
exit 1
2222
fi
2323
;;
2424
esac
2525
shift
2626
done
2727
if [ -z "$targetDirA" ]; then
2828
echo "Missing first argument"
29-
return 1
29+
exit 1
3030
elif [ -z "$targetDirB" ]; then
3131
echo "Missing second argument"
32-
return 1
32+
exit 1
3333
fi
3434

3535
set +e

0 commit comments

Comments
 (0)