Skip to content

implement diff with diffoscope #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: test-action
uses: ./
- run: echo "${{ steps.test-action.outputs.diff }}"
with:
base_ref: 027b33661ae3211230e8cd03d19df49ba620b379
head_ref: 182a672418c5bae4d6b7ca1244da5f28bf5ff6f0
kustomizations: |-
testdata/prod
testdata/stage
- uses: mshick/add-pr-comment@v2
with:
message: ${{ steps.test-action.outputs.diff }}
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM debian:bookworm-slim
FROM registry.k8s.io/kustomize/kustomize:v5.0.0

COPY entrypoint.sh .
RUN apk add --no-cache \
sed \
diffoscope

COPY entrypoint.sh .
ENTRYPOINT [ "./entrypoint.sh" ]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# GitHub Action for Kustomize Build+Diff
# GitHub Action for Kustomize Build+Diff
18 changes: 15 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ name: "kustomize-diff"
description: "Kustomize build and diff action"
inputs:
base_ref:
description: "Ref for PR base"
description: "Git ref for PR base"
required: true
default: ${{ github.base_ref }}
head_ref:
description: "Ref for PR head"
description: "Git ref for PR head"
required: true
default: ${{ github.head_ref }}
kustomizations:
description: |-
Kustomization directories

You can specify multiple directories by separating them with a newline:
```yaml
kustomizations: |-
testdata/prod
testdata/stage
```
required: true
default: ""
outputs:
diff:
description: "Git diff"
description: "Kustomize diff in markdown"
runs:
using: "docker"
image: "Dockerfile"
42 changes: 38 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
#!/bin/sh

set -x
set -eux

echo "Hello World"
echo "head $INPUT_HEAD_REF base $INPUT_BASE_REF"
build_dir=$(mktemp -d)

echo "diff=foo" >>"$GITHUB_OUTPUT"
build() {
ref="$1"
git checkout "$1" --quiet

for target in $INPUT_KUSTOMIZATIONS; do
echo "Building $target" to "$build_dir/$ref/$target"
mkdir -p "$build_dir/$ref/$target"
kustomize build "$target" -o "$build_dir/$ref/$target/"
done
}

git config --global --add safe.directory "$GITHUB_WORKSPACE"

build "$INPUT_BASE_REF"
build "$INPUT_HEAD_REF"

base_ref_build_dir="$build_dir/$INPUT_BASE_REF"
head_ref_build_dir="$build_dir/$INPUT_HEAD_REF"

set +e
for target in $INPUT_KUSTOMIZATIONS; do
diffoscope "$base_ref_build_dir/$target" "$head_ref_build_dir/$target" \
--markdown - \
--exclude-directory-metadata=yes | tee -a diff.md
done

set -e

# Formatting hacks
output=$(cat diff.md | sed "s|$base_ref_build_dir/||" | sed '/Comparing/ s/&.*$//' | sed "s|^#\{2,\} Comparing| Comparing|" | sed "s|^# Comparing|## Comparing|")

{
echo 'diff<<EOF'
echo "$output"
echo 'EOF'
} >>"$GITHUB_OUTPUT"

exit 0
4 changes: 4 additions & 0 deletions testdata/base/kustomization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- release.yml
15 changes: 15 additions & 0 deletions testdata/base/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
name: testapp
spec:
releaseName: testapp
chart:
spec:
chart: charts/internal-service
sourceRef:
kind: GitRepository
name: gitops
interval: 1h
values:
foo: true
6 changes: 6 additions & 0 deletions testdata/prod/kustomization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
patches:
- path: patch.yml
8 changes: 8 additions & 0 deletions testdata/prod/patch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
name: testapp
spec:
values:
deployment:
useServiceAccount: false
6 changes: 6 additions & 0 deletions testdata/stage/kustomization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
patches:
- path: patch.yml
8 changes: 8 additions & 0 deletions testdata/stage/patch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
name: testapp
spec:
values:
deployment:
useServiceAccount: true