forked from kubesphere/helm-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·202 lines (169 loc) · 5.33 KB
/
build.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
#!/usr/bin/env bash
set -e
ensureVars() {
echo "Checking required variables ..."
local var
for var in $@; do
if [ -z "${!var}" ]; then
echo "Environment variable '$var' should be defined." && exit 1
fi
done
}
helm() {
./helm $@
}
helmVersion=3.2.4
prepareHelm() {
local helmUrl=https://get.helm.sh/helm-v$helmVersion-linux-amd64.tar.gz
echo "Downloading Helm Client from '$helmUrl' ..."
curl -sL $helmUrl | tar --strip-components=1 -xzf - linux-amd64/helm
}
findUpdatedCharts() {
local repoDir=$1 indexFile=$2
local existingCharts="$(grep -oE "[^/]+\.tgz$" $indexFile)"
local srcFiles="$(ls $repoDir/*/Chart.yaml)" srcFile
for srcFile in $srcFiles; do
local chartName=$(awk '/^name:/ {name=$2} /^version:/ {version=$2} END {printf "%s-%s.tgz", name, version}' $srcFile)
echo $existingCharts | grep -q $chartName || echo $(dirname $srcFile)
done
}
addDependencyRepos() {
local knownRepos="$(helm repo list | awk 'NR>1 {print $2}')" chartDir
for chartDir in $@; do
local reqFile="$chartDir/requirements.yaml"
if [ -f "$reqFile" ]; then
local depRepos="$(awk '$1=="repository:" {print $2}' $reqFile | uniq)" depRepo
[ -z "$depRepos" ] || for depRepo in $depRepos; do
depRepo=${depRepo%/}
echo $knownRepos | grep -q $depRepo || helm repo add ${depRepo//[.:\/]/-} $depRepo
done
fi
done
}
updateDependencies() {
for chartDir in $@; do
helm dependency update $chartDir
done
}
srcDir=src
buildDir=build
mkdir -p $buildDir
updatedRepos=
updateRepo() {
local repo=$1 repoUrl repoDir indexFile httpCode updatedCharts
repoUrl=$BASE_URL/$repo
indexUrl=$repoUrl/index.yaml
repoSrcDir=$srcDir/$repo
repoDir=$buildDir/$repo
indexFile=$repoDir/index.yaml
echo "Updating repo [repo=$repo repoUrl=$repoUrl indexUrl=$indexUrl srcDir=$repoSrcDir repoDir=$repoDir indexFile=$indexFile] ..."
mkdir -p $repoDir
echo "Downloading current Helm index '$indexUrl' ..."
httpCode=$(curl -s -m 5 -o $indexFile -w '%{http_code}' "$indexUrl") || {
echo "Failed to download current Helm index '$indexUrl' ($?)."
return 1
}
[[ "$httpCode" =~ ^200|404$ ]] || {
echo "Error occurred when fetching '$repoUrl': code=$httpCode content=$(cat $indexFile)."
return 1
}
[ "$httpCode" = "404" ] && rm -f $indexFile
echo "Finding updated charts ..."
updatedCharts="$(findUpdatedCharts $repoSrcDir $indexFile)"
updatedCharts="$(echo $updatedCharts)"
if [ -z "$updatedCharts" ]; then
echo "No charts added or updated in repo '$repo'."
return 0
fi
echo Adding dependency repos for [$updatedCharts] ...
addDependencyRepos $updatedCharts
echo Updating dependencies for [$updateCharts] ...
updateDependencies $updatedCharts
echo Linting charts [$updatedCharts] ...
helm lint $updatedCharts
echo Packaging charts [$updatedCharts] ...
helm package $updatedCharts --destination $repoDir -u
local mergeOpts
[ -f "$indexFile" ] && mergeOpts="--merge $indexFile"
echo Generating $repo/index.yaml ...
helm repo index $repoDir --url $repoUrl $mergeOpts
updatedRepos="$repo:$updatedRepos"
}
updateRepos() {
ensureVars BASE_URL
echo "Updating Helm repos ..."
local repo
for repo in $(ls $srcDir); do
[ -f "$srcDir/$repo" ] || updateRepo $repo
done
}
injectGithubToken() {
local url
url=$(git remote -v | awk '$3=="(push)" {print $2}' | sed -E "s|^https://[^/]*(.+)$|https://$GITHUB_USER:[email protected]\1|")
echo "Injecting GitHub token ..."
git remote set-url origin $url
}
pushUpdates() {
ensureVars GITHUB_USER GITHUB_TOKEN
git commit -m "Update" --author="KubeSphere CI Bot <[email protected]>"
injectGithubToken
echo "Pushing updates to GitHub ..."
git push origin HEAD:gh-pages
}
pushRemote() {
ensureVars GITEE_USER GITEE_TOKEN
git remote add gitee https://$GITEE_USER:[email protected]/kubesphere/helm-charts.git
git push gitee HEAD:${1:-master} ${@:2}
git remote rm gitee
}
installQsctl() {
ensureVars QSCTL_URL
curl -L $QSCTL_URL -o /tmp/qsctl.tgz
tar --transform="s|^.*$|qsctl|" -xzf /tmp/qsctl.tgz
./qsctl --version
}
pushRemoteDeployment() {
ensureVars BASE_URL BASE_URL_CN QS_ACCESS_KEY_ID QS_SECRET_KEY_ID QS_ZONE QS_BUCKET
rm -rf src/ build/
files="$(find . -type f -name index.html -or -name index.yaml)"
sed -i "s#$BASE_URL#$BASE_URL_CN#g" $files
installQsctl
mkdir ~/.qingstor
echo -e "access_key_id: $QS_ACCESS_KEY_ID\nsecret_access_key: $QS_SECRET_KEY_ID\nzone: $QS_ZONE" > ~/.qingstor/config.yaml
for updatedRepo in ${updatedRepos//:/ }; do
./qsctl sync $updatedRepo/ qs://$QS_BUCKET/$updatedRepo/ --update
done
rm -rf ~/.qingstor/config.yaml
}
verify() {
prepareHelm
updateRepos
}
deploy() {
pushRemote
verify
[ -z "$updatedRepos" ] || {
git fetch
git checkout --track origin/gh-pages
local repo
for repo in $(ls $buildDir); do
mkdir -p $repo && mv $buildDir/$repo/* $repo/ && git add $repo
done
pushUpdates
pushRemote gh-pages
pushRemoteDeployment
}
}
mirror() {
local original=https://charts.helm.sh/stable
local mirrored=https://helm-chart-repo.pek3a.qingstor.com/kubernetes-charts
git fetch
git checkout --track origin/gh-pages
mkdir -p mirror
cd mirror
curl -L $original/index.yaml -o index.yaml
sed -i "s#$original/packages#$mirrored#g" index.yaml
git add index.yaml
pushUpdates
}
$@