-
Notifications
You must be signed in to change notification settings - Fork 94
130 lines (114 loc) · 5.09 KB
/
unit-test.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
name: Unit Test
# yamllint disable-line rule:truthy
on:
push:
branches: ["main"]
paths:
- 'charts/**'
- '!charts/k8s-monitoring-v1/**'
pull_request:
paths:
- 'charts/**'
- '!charts/k8s-monitoring-v1/**'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
detect-changed-charts:
name: Detect Changed Charts
runs-on: ubuntu-latest
outputs:
changed_charts: ${{ steps.changed_charts.outputs.changed_charts }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Detect Changed Charts
id: changed_charts
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
# In pull request, compare against the base branch (upstream)
base_branch="${{ github.event.pull_request.base.ref }}"
echo "Comparing against base branch: $base_branch"
git fetch origin $base_branch
base_commit="origin/$base_branch"
elif [ "${{ github.event_name }}" == "push" ]; then
# In push to main, compare the last commit with HEAD^
base_commit="HEAD^"
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
# In manual trigger, run for all charts
echo "Manual dispatch detected, running tests for all charts"
# shellcheck disable=SC2010
changed_charts=$(ls charts | grep -v "k8s-monitoring-v1" | sort -u)
echo "changed_charts=$(echo "$changed_charts" | jq --raw-input --slurp --compact-output 'split("\n") | map(select(. != ""))')" >> "${GITHUB_OUTPUT}"
exit 0
fi
# Check if base commit exists, fallback to empty tree if none
if ! git rev-parse --verify "$base_commit" >/dev/null 2>&1; then
base_commit=$(git hash-object -t tree /dev/null)
fi
# Detect modified files
modified_charts=$(git diff --name-only "$base_commit" HEAD -- 'charts/*' | grep "^charts/" | cut -d "/" -f2 | sort -u)
# Detect newly added files (untracked files)
added_charts=$(git ls-files --others --exclude-standard -- 'charts/*' | grep "^charts/" | cut -d "/" -f2 | sort -u)
# Combine both added and modified charts
changed_charts=$(echo -e "$modified_charts\n$added_charts" | grep -v "k8s-monitoring-v1" | sort -u)
if [ -z "$changed_charts" ]; then
echo "No changes detected in charts"
else
echo "Changed charts: $changed_charts"
echo "changed_charts=$(echo "$changed_charts" | jq --raw-input --slurp --compact-output 'split("\n") | map(select(. != ""))')" >> "${GITHUB_OUTPUT}"
fi
run-tests:
name: Testing
needs: detect-changed-charts
runs-on: ubuntu-latest
strategy:
matrix:
dir: ${{ fromJson(needs.detect-changed-charts.outputs.changed_charts) }}
fail-fast: false
if: ${{ needs.detect-changed-charts.outputs.changed_charts != 'none' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up chart-testing
uses: helm/chart-testing-action@v2
# Installing Grafana Alloy because we need it to lint the generated alloy config files.
# https://grafana.com/docs/alloy/latest/get-started/install/linux/
- name: Install Grafana Alloy
run: |
sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install -y alloy
- name: Run tests
run: make -C "charts/${{ matrix.dir }}" test
check-generated-files:
name: Check Generated Files
needs: detect-changed-charts
runs-on: ubuntu-latest
strategy:
matrix:
dir: ${{ fromJson(needs.detect-changed-charts.outputs.changed_charts) }}
fail-fast: false
if: ${{ needs.detect-changed-charts.outputs.changed_charts != 'none' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Regenerate files
run: make -C "charts/${{ matrix.dir }}" clean build
- name: Check for changes in generated files
run: |
cd charts/${{ matrix.dir }}
if [ "${{ matrix.dir }}" == "k8s-monitoring" ]; then
# Skip checking subchart files for k8s-monitoring, which are always modified, even if the contents are identical
if ! git diff --exit-code -- ':!charts/*.tgz'; then
echo "Generated files in charts/${{ matrix.dir }} are not up to date. Please run 'make all' and commit the changes."
exit 1
fi
elif ! git diff --exit-code .; then
echo "Generated files in charts/${{ matrix.dir }} are not up to date. Please run 'make all' and commit the changes."
exit 1
else
echo "Generated files in charts/${{ matrix.dir }} are up to date."
fi