forked from helm/chart-releaser-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr.sh
executable file
·232 lines (188 loc) · 5.86 KB
/
cr.sh
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env bash
# Copyright The Helm Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
DEFAULT_CHART_RELEASER_VERSION=v0.2.3
: "${CR_TOKEN:?Environment variable CR_TOKEN must be set}"
show_help() {
cat << EOF
Usage: $(basename "$0") <options>
-h, --help Display help
-v, --version The chart-releaser version to use (default: v0.2.3)"
-d, --charts-dir The charts directory (defaut: charts)
-u, --charts-repo-url The GitHub Pages URL to the charts repo (default: https://<owner>.github.io/<repo>)
-o, --owner The repo owner
-r, --repo The repo name
EOF
}
main() {
local version="$DEFAULT_CHART_RELEASER_VERSION"
local charts_dir=charts
local owner=
local repo=
local charts_repo_url=
parse_command_line "$@"
echo "$repo"
local repo_root
repo_root=$(git rev-parse --show-toplevel)
pushd "$repo_root" > /dev/null
echo 'Looking up latest tag...'
local latest_tag
latest_tag=$(lookup_latest_tag)
echo "Discovering changed charts since '$latest_tag'..."
local changed_charts=()
readarray -t changed_charts <<< "$(lookup_changed_charts "$latest_tag")"
if [[ -n "${changed_charts[*]}" ]]; then
install_chart_releaser
rm -rf .cr-release-packages
mkdir -p .cr-release-packages
rm -rf .cr-index
mkdir -p .cr-index
for chart in "${changed_charts[@]}"; do
package_chart "$chart"
done
release_charts
update_index
else
echo "Nothing to do. No chart changes detected."
fi
popd > /dev/null
}
parse_command_line() {
while :; do
case "${1:-}" in
-h|--help)
show_help
exit
;;
-v|--version)
if [[ -n "${2:-}" ]]; then
version="$2"
shift
else
echo "ERROR: '-v|--version' cannot be empty." >&2
show_help
exit 1
fi
;;
-d|--charts-dir)
if [[ -n "${2:-}" ]]; then
charts_dir="$2"
shift
else
echo "ERROR: '-d|--charts-dir' cannot be empty." >&2
show_help
exit 1
fi
;;
-u|--charts-repo-url)
if [[ -n "${2:-}" ]]; then
charts_repo_url="$2"
shift
else
echo "ERROR: '-u|--charts-repo-url' cannot be empty." >&2
show_help
exit 1
fi
;;
-o|--owner)
if [[ -n "${2:-}" ]]; then
owner="$2"
shift
else
echo "ERROR: '--owner' cannot be empty." >&2
show_help
exit 1
fi
;;
-r|--repo)
if [[ -n "${2:-}" ]]; then
repo="$2"
shift
else
echo "ERROR: '--repo' cannot be empty." >&2
show_help
exit 1
fi
;;
*)
break
;;
esac
shift
done
if [[ -z "$owner" ]]; then
echo "ERROR: '-o|--owner' is required." >&2
show_help
exit 1
fi
if [[ -z "$repo" ]]; then
echo "ERROR: '-r|--repo' is required." >&2
show_help
exit 1
fi
if [[ -z "$charts_repo_url" ]]; then
charts_repo_url="https://$owner.github.io/$repo"
fi
}
install_chart_releaser() {
echo "Installing chart-releaser..."
curl -sSLo cr.tar.gz "https://github.com/helm/chart-releaser/releases/download/$version/chart-releaser_${version#v}_linux_amd64.tar.gz"
tar -xzf cr.tar.gz
sudo mv cr /usr/local/bin/cr
}
lookup_latest_tag() {
git fetch --tags > /dev/null 2>&1
if ! git describe --tags --abbrev=0 2> /dev/null; then
git rev-list --max-parents=0 --first-parent HEAD
fi
}
lookup_changed_charts() {
local commit="$1"
local changed_files
changed_files=$(git diff --find-renames --name-only "$commit" -- "$charts_dir")
local fields
if [[ "$charts_dir" == '.' ]]; then
fields='1'
else
fields='1,2'
fi
cut -d '/' -f "$fields" <<< "$changed_files" | uniq
}
package_chart() {
local chart="$1"
echo "Packaging chart '$chart'..."
helm package "$chart" --destination .cr-release-packages --dependency-update --save=false
}
release_charts() {
echo 'Releasing charts...'
cr upload -o "$owner" -r "$repo"
}
update_index() {
echo 'Updating charts repo index...'
set -x
cr index -o "$owner" -r "$repo" -c "$charts_repo_url"
gh_pages_worktree=$(mktemp -d)
git worktree add "$gh_pages_worktree" gh-pages
cp --force .cr-index/index.yaml "$gh_pages_worktree/index.yaml"
pushd "$gh_pages_worktree" > /dev/null
git add index.yaml
git commit --message="Update index.yaml" --signoff
local repo_url="https://x-access-token:[email protected]/$owner/$repo"
git push "$repo_url" gh-pages
popd > /dev/null
}
main "$@"