Skip to content

Commit dfb6d59

Browse files
ci: support worktree (#3425)
Co-authored-by: svcAPLBot <174728082+svcAPLBot@users.noreply.github.com>
1 parent 247155f commit dfb6d59

3 files changed

Lines changed: 49 additions & 27 deletions

File tree

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ on:
55
pull_request:
66
types: [opened, synchronize, reopened, ready_for_review]
77
paths:
8-
- "charts/**"
9-
- "values/**"
10-
- "tests/fixtures/**"
11-
- "helmfile.d/**"
8+
- 'charts/**'
9+
- 'values/**'
10+
- 'tests/fixtures/**'
11+
- 'helmfile.d/**'
1212

1313
permissions:
1414
contents: read
@@ -64,24 +64,25 @@ jobs:
6464
- name: Checkout PR branch
6565
uses: actions/checkout@v6
6666
with:
67-
ref: ${{ github.event.pull_request.head.sha }}
67+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
68+
fetch-depth: 0
6869
path: pr
69-
- name: Checkout base branch
70-
uses: actions/checkout@v6
71-
with:
72-
ref: ${{ github.event.pull_request.base.ref || 'main' }}
73-
path: base
7470
- name: Set up Node.js
7571
uses: actions/setup-node@v6
7672
with:
7773
node-version-file: 'pr/.nvmrc'
7874
check-latest: true
7975
- name: Install npm dependencies
80-
run: cd "$GITHUB_WORKSPACE/pr" && npm install --no-save && cd "$GITHUB_WORKSPACE/base" && npm install --no-save
76+
run: cd "$GITHUB_WORKSPACE/pr" && npm install --no-save
77+
- name: Prepare base branch ref for worktree
78+
run: |
79+
cd "$GITHUB_WORKSPACE/pr"
80+
base_ref="${{ github.event.pull_request.base.ref || 'main' }}"
81+
git fetch --no-tags --prune origin "$base_ref:$base_ref"
8182
- name: Install Helm and Helmfile
8283
uses: helmfile/helmfile-action@v2.4.6
8384
with:
84-
helmfile-args: version # In this step, we only want these tools to be installed
85+
helmfile-args: version # In this step, we only want these tools to be installed
8586
helm-plugins: >
8687
https://github.com/databus23/helm-diff,
8788
https://github.com/jkroepke/helm-secrets
@@ -96,12 +97,16 @@ jobs:
9697
id: run_script
9798
env:
9899
GH_TOKEN: ${{ secrets.BOT_TOKEN }}
100+
HUSKY: '0'
99101
run: |
102+
cd "$GITHUB_WORKSPACE/pr"
100103
mkdir -p tmp
101-
"$GITHUB_WORKSPACE/pr/bin/compare.sh" "$GITHUB_WORKSPACE/base" "$GITHUB_WORKSPACE/pr" --diff-output tmp/diff-output.txt
102-
comment_file="$PWD/tmp/pr-comment.txt"
104+
./bin/compare.sh --diff-output tmp/diff-output.txt
105+
comment_file="$GITHUB_WORKSPACE/pr/tmp/pr-comment.txt"
103106
echo "Comparison of Helm chart templating output:" > "$comment_file"
104107
echo '```diff' >> "$comment_file"
105-
cat tmp/diff-output.txt >> "$comment_file"
108+
cat "$GITHUB_WORKSPACE/pr/tmp/diff-output.txt" >> "$comment_file"
106109
echo '```' >> "$comment_file"
107-
cd "$GITHUB_WORKSPACE/pr" && gh pr comment ${{ github.event.pull_request.number }} --body-file "$comment_file" --create-if-none --edit-last
110+
if [ "${{ github.event_name }}" = "pull_request" ]; then
111+
gh pr comment ${{ github.event.pull_request.number }} --body-file "$comment_file" --create-if-none --edit-last
112+
fi

bin/compare.sh

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ srcDirA=
1414
srcDirB=
1515
targetDirA=
1616
targetDirB=
17+
tmpWorktreeA=
1718
# Process arguments
1819
while [ $# -ne 0 ]; do
1920
case "$1" in
@@ -42,45 +43,61 @@ done
4243

4344
readonly script_dir=$(dirname "$0")
4445

46+
cleanup() {
47+
if [ -n "${tmpWorktreeA}" ] && [ -d "${tmpWorktreeA}" ]; then
48+
git -c core.hooksPath=/dev/null worktree remove --force "${tmpWorktreeA}" >/dev/null 2>&1 || true
49+
rm -rf "${tmpWorktreeA}" || true
50+
fi
51+
}
52+
53+
trap cleanup EXIT
54+
4555
generate_helm_templates() {
4656
local target_dir="$1"
57+
local source_dir="$2"
58+
59+
pushd "$source_dir" >/dev/null
4760
rm -rf "$target_dir"
4861
node --no-warnings --import tsx src/otomi.ts -- values
4962
helmfile template "${templateArgs[@]}" --output-dir-template="$target_dir/{{.Release.Namespace}}-{{.Release.Name}}"
5063
mv tests/fixtures/values-repo.yaml "$target_dir/values-repo.yaml"
64+
popd >/dev/null
5165
}
5266

5367
export NODE_ENV=test
5468
if [ -z "$srcDirA" ]; then
5569
branchA='main'
5670
# branchB current branch
5771
branchB=$(git rev-parse --abbrev-ref HEAD)
72+
if [ "$branchB" = "HEAD" ]; then
73+
branchB="detached-$(git rev-parse --short HEAD)"
74+
fi
5875

5976
targetDirA="$PWD/tmp/${branchA}"
6077
targetDirB="$PWD/tmp/${branchB}"
78+
6179
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
80+
generate_helm_templates "$targetDirB" "$PWD"
81+
82+
tmpWorktreeA="$PWD/tmp/worktree-${branchA}-$$"
83+
git -c core.hooksPath=/dev/null worktree add --detach "$tmpWorktreeA" "$branchA" >/dev/null
84+
export ENV_DIR="$tmpWorktreeA/tests/fixtures"
85+
generate_helm_templates "$targetDirA" "$tmpWorktreeA"
6686
else
6787
if [ -z "$srcDirB" ]; then
6888
echo "Only one directory passed for comparison"
6989
exit 1
7090
fi
7191
startDir=$PWD
72-
pushd "$srcDirB"
7392
export ENV_DIR="$srcDirB/tests/fixtures"
7493
branchB=$(git rev-parse --abbrev-ref HEAD)
7594
targetDirB="$startDir/tmp/${branchB}"
76-
generate_helm_templates "$targetDirB"
77-
popd
78-
pushd "$srcDirA"
95+
generate_helm_templates "$targetDirB" "$srcDirB"
96+
7997
export ENV_DIR="$srcDirA/tests/fixtures"
8098
branchA=$(git rev-parse --abbrev-ref HEAD)
8199
targetDirA="$startDir/tmp/${branchA}"
82-
generate_helm_templates "$targetDirA"
83-
popd
100+
generate_helm_templates "$targetDirA" "$srcDirA"
84101
fi
85102

86103
# order of arguments matters so new changes are green color

tests/fixtures/env/settings/cluster.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ metadata:
33
name: cluster
44
spec:
55
apiServer: https://1.1.1.1:8443
6-
defaultStorageClass: ""
6+
defaultStorageClass: 'abc'
77
domainSuffix: dev.linode-apl.net
88
k8sContext: linode-dev
99
linode:

0 commit comments

Comments
 (0)