Skip to content

Commit e31f174

Browse files
committed
Initial commit.
1 parent 13e5530 commit e31f174

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

Diff for: Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM alpine:3.14.3
2+
3+
RUN apk add --no-cache \
4+
curl \
5+
bash \
6+
git \
7+
&& rm -rf /var/cache/apk/*
8+
9+
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
10+
11+
RUN curl -sL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv3.8.7/kustomize_v3.8.7_linux_amd64.tar.gz \
12+
| tar xz -C /usr/local/bin
13+
14+
COPY kustdiff /kustdiff
15+
16+
ENTRYPOINT ["/kustdiff"]

Diff for: README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
# github-action-kustomize-diff
1+
# GitHub Action for kustomize-diff
2+
3+
This [action](https://help.github.com/en/actions) can be used in any repository that uses [kustomize](https://kustomize.io/).
4+
5+
# Summary
6+
7+
The steps the action takes are as follows:
8+
9+
- Store the output of `kustomize build` (for each environment) on the current branch in a temporary location.
10+
- Store the output of `kustomize build` (for each environment) on the master branch in a temporary location.
11+
- Based on the two outputs above it performs a git diff and stores the output in a variable called `escaped_output`.
12+
13+
This action can be combined with [unsplash/comment-on-pr](https://github.com/unsplash/comment-on-pr) to comment the output to the PR.
14+
15+
# Example configuration
16+
17+
The below example will run `kustomize-diff` against your branch and commit the changes due to be applied back to your Pull Request.
18+
19+
```
20+
name: kustomize-diff
21+
on:
22+
pull_request:
23+
paths:
24+
- 'kustomize/**'
25+
26+
jobs:
27+
kustomize-diff:
28+
runs-on: ubuntu-latest
29+
steps:
30+
- id: checkout
31+
uses: actions/checkout@v2
32+
with:
33+
fetch-depth: 0
34+
- id: kustomize-diff
35+
uses: swade1987/github-action-kustomize-diff@master
36+
- id: comment
37+
uses: unsplash/comment-on-pr@master
38+
env:
39+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
with:
41+
msg: ${{ steps.kustomize-diff.outputs.diff }}
42+
check_for_duplicate_msg: false
43+
```

Diff for: action.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: "kustomize-diff"
2+
description: "Kustomize build head and base of PR, add diff as comment"
3+
inputs:
4+
base_ref:
5+
description: "Ref for PR base"
6+
required: true
7+
default: ${{ github.base_ref }}
8+
head_ref:
9+
description: "Ref for PR head"
10+
required: true
11+
default: ${{ github.head_ref }}
12+
pr_num:
13+
description: "PR number / ID"
14+
required: true
15+
default: ${{ github.event.number }}
16+
token:
17+
description: "Token for GitHub authentication"
18+
required: true
19+
default: ${{ github.token }}
20+
outputs:
21+
diff:
22+
description: "Diff between kustomize built head and base"
23+
runs:
24+
using: "docker"
25+
image: "Dockerfile"

Diff for: kustdiff

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
set -eux
4+
5+
TMP_DIR="$(mktemp -d)"
6+
7+
function get_targets {
8+
find . -maxdepth 3 -name kustomization.yaml -exec dirname {} \;
9+
find . -mindepth 4 -maxdepth 4 -name kustomization.yaml -exec dirname {} \; | sort | uniq | grep variant
10+
}
11+
12+
function build {
13+
local ref="$1"
14+
printf "\n\nChecking out ref: %s\n" "$ref"
15+
git checkout "$ref" --quiet
16+
for envpath in $(get_targets); do
17+
local build_dir
18+
if ! [ -d "$envpath" ]; then continue; fi
19+
build_dir="$TMP_DIR/$ref/${envpath#*kustomize/}"
20+
printf "\n\nCreating build directory: %s\n" "$build_dir"
21+
mkdir -p "$build_dir"
22+
echo "Running kustomize"
23+
kustomize build "$envpath" -o "$build_dir"
24+
done
25+
}
26+
27+
function main {
28+
local diff escaped_output output
29+
build "$INPUT_HEAD_REF"
30+
build "$INPUT_BASE_REF"
31+
set +e
32+
diff=$(git diff --no-index "$TMP_DIR/$INPUT_BASE_REF" "$TMP_DIR/$INPUT_HEAD_REF")
33+
set -e
34+
output=$(printf "\`\`\` diff\n%s\n\`\`\`\n" "$diff")
35+
escaped_output=${output//$'\n'/'%0A'}
36+
echo "::set-output name=diff::$escaped_output"
37+
printf "\n\nOutput: %s\n" "$escaped_output"
38+
}
39+
40+
main

0 commit comments

Comments
 (0)