-
Notifications
You must be signed in to change notification settings - Fork 2
/
js-release.sh
executable file
·436 lines (327 loc) · 15.9 KB
/
js-release.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/bin/bash
set -e
echo "Starting javascript release script"
if [[ $GIT_NAME == "volto-addon-template" ]]; then
echo "No release flow for templates, skipping all steps"
exit 0
fi
if [ -z "$GIT_NAME" ] || [ -z "$GIT_BRANCH" ]; then
echo "GIT repo name and branch not given"
exit 1
fi
if [ -z "$GIT_TOKEN" ]; then
echo "GIT token not given"
exit 1
fi
if [ -f /common_functions ]; then
. /common_functions
elif [ -f ./common_functions ]; then
. ./common_functions
fi
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"
if [ -n "$NODEJS_VERSION" ]; then
echo "Received NODEJS_VERSION $NODEJS_VERSION, will use it instead of default version"
if [ $(nvm list "$NODEJS_VERSION" | grep "$NODEJS_VERSION" | wc -l) -eq 0 ]; then
echo "Did not find this version installed, will install it"
nvm install $NODEJS_VERSION
npm install -g yarn release-it yarn-deduplicate husky
fi
nvm use $NODEJS_VERSION
fi
GIT_ORG=${GIT_ORG:-'eea'}
GIT_USER=${GIT_USER:-'eea-jenkins'}
GIT_USERNAME=${GIT_USERNAME:-'EEA Jenkins'}
GIT_EMAIL=${GIT_EMAIL:-'[email protected]'}
export GITHUB_TOKEN="${GIT_TOKEN}"
GIT_SRC=https://$GIT_USER:[email protected]/${GIT_ORG}/${GIT_NAME}.git
check_and_push()
{
if [[ "$1" == "yes" ]]; then
echo "RESOLUTIONS - Updating version on $2@$3 in package.json"
git diff
git add package.json
git commit -m "Release $2@$3 - resolutions"
git push
fi
}
update_package_json()
{
UPDATE_BRANCH="${5:-master}"
retry=${6:-0}
if [ $retry -gt 3 ]; then
echo "Aborting run, there is a problem"
echo "Error after $retry tries to update dependency in $2 on gitrepo $1 for package $3 version $4 on branch $UPDATE_BRANCH"
exit 1
fi
let retry=retry+1
if [[ "$1" == "eea/volto-frontend-template" ]]; then
echo "Skipping frontend template"
return
fi
echo "Running update dependency in $2 on gitrepo $1 for package $3 version $4 on branch $UPDATE_BRANCH for try $retry"
curl -s -X GET -H "Authorization: bearer $GIT_TOKEN" -H "Accept: application/vnd.github.VERSION.raw" "https://api.github.com/repos/$1/contents/package.json?ref=$UPDATE_BRANCH" > /tmp/package.json
if [ -f /tmp/package.json ] && [ $(grep 'dependencies' /tmp/package.json | wc -l) -gt 0 ]; then
echo "Got package.json file, will now check if $3 is present"
if [ $(grep $3 /tmp/package.json | wc -l) -eq 0 ]; then
echo "Did not find $3 in package.json, skipping $1"
return
fi
fi
cd /
rm -rf /frontend
git clone https://$GIT_USER:[email protected]/$1.git /frontend
cd /frontend
if [ $(git branch --all | grep origin/${UPDATE_BRANCH}$ | wc -l) -eq 0 ]; then
echo "Repository does not contain branch $UPDATE_BRANCH, skipping"
return
fi
git checkout $UPDATE_BRANCH
if [ ! -f "package.json" ]; then
echo "Repository does not contain package.json, skipping"
return
fi
# check resolutions
old_version=$(jq -r ".resolutions | .\"$3\"" package.json )
biggest_version=$(echo "$4
$old_version" | sort --sort=version | tail -n 1 )
to_push=""
if [ -z "$old_version" ] || [[ "$old_version" == "None" ]] || [[ "$old_version" == "null" ]] ; then
echo "No RESOLUTIONS to update, skipping"
elif [ "$4" == "$old_version" ]; then
echo "RESOLUTIONS - The released $3 version is already updated"
elif [ $(echo $old_version | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | wc -l) -eq 0 ]; then
echo "RESOLUTIONS - Version format ($old_version) is not fixed to major.minor.patch, will not automatically upgrade it"
elif [[ "$biggest_version" == "$old_version" ]]; then
echo "RESOLUTIONS - The released $3 version is bigger than the released one, skipping"
else
echo "RESOLUTIONS - Old version $old_version is smaller than the released version"
echo "RESOLUTIONS - Will now update the package.json file"
jq ".resolutions[\"$3\"] = \"$4\"" package.json > newpackage.json
mv newpackage.json package.json
to_push="yes"
fi
old_version=$(jq -r ".dependencies | .\"$3\"" package.json )
if [ -z "$old_version" ] || [[ "$old_version" == "None" ]] || [[ "$old_version" == "null" ]] ; then
check_and_push $to_push $3 $4
echo "DEPENDENCIES - Did not find the package in dependecies list, skipping"
return
fi
echo "DEPENDENCIES - Found package version - $old_version"
if [ "$4" == "$old_version" ]; then
check_and_push $to_push $3 $4
echo "DEPENDENCIES - The released $3 version is already updated, finishing"
return
fi
echo "Checking prerequisites"
if [ $(echo $old_version | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | wc -l) -eq 0 ]; then
check_and_push $to_push $3 $4
echo "DEPENDENCIES - Version format ($old_version) is not fixed to major.minor.patch, will not automatically upgrade it, finishing"
return
fi
biggest_version=$(echo "$4
$old_version" | sort --sort=version | tail -n 1 )
if [[ "$biggest_version" == "$old_version" ]]; then
check_and_push $to_push $3 $4
echo "DEPENDENCIES - The released $3 version is bigger than the released one, finishing"
return
fi
echo "DEPENDENCIES - Old version $old_version is smaller than the released version"
echo "DEPENDENCIES - Will now update the version file and yarn.lock"
#does not support node 16, only 14
if [ $(cat package.json | jq '.engines.node' | grep -v 16 | grep 14 | wc -l) -eq 1 ]; then
nvm use 14
fi
#does not support node 16, only 18
#volto-eea-kitkat needs node 18
if [ $(cat package.json | jq '.engines.node' | grep -v 16 | grep 18 | wc -l) -eq 1 ] || [ "$1" == "eea/volto-eea-kitkat" ]; then
nvm use 18
fi
if [ $(yarn -v | grep ^1 | wc -l) -eq 1 ]; then
yarn add -W $3@$4
echo "DEPENDENCIES - Also run deduplicate to fix broken yarn.lock file"
yarn-deduplicate yarn.lock
else
yarn add $3@$4
fi
#back to default 16
if [ $(cat package.json | jq '.engines.node' | grep -v 16 | grep 14 | wc -l) -eq 1 ]; then
nvm use 16
fi
#Yarn takes a lot of time, will try pull, if it fails because of conflicts, start over"
pull_error=$(git pull 2>&1 | grep Aborting | wc -l)
if [ $pull_error -ne 0 ]; then
echo "There is a concurrency problem on repo $1, will cleanup and retry again"
update_package_json $1 $2 $3 $4 $UPDATE_BRANCH $retry
return
fi
git status
git diff
git add package.json
if [ $(grep "yarn.lock" .gitignore | wc -l ) -eq 0 ]; then
git add yarn.lock
fi
commit_ok=$(git commit -m "Release $3@$4" | grep -i "changed" | wc -l)
if [ $commit_ok -eq 1 ]; then
push_ok=$(git push || echo "Error")
if [[ "$push_ok" == "Error" ]]; then
echo "There was a problem with the commit on repo $1, will cleanup and retry again"
update_package_json $1 $2 $3 $4 $UPDATE_BRANCH $retry
return
fi
echo -e "$push_ok"
else
echo "There was a problem with the commit on repo $1, will cleanup and retry again"
update_package_json $1 $2 $3 $4 $UPDATE_BRANCH $retry
return
fi
}
git config --global user.name "${GIT_USERNAME}"
git config --global user.email "${GITHUB_USER}@users.noreply.github.com"
git clone $GIT_SRC
if [ ! -f $GIT_NAME/package.json ] || [ -f $GIT_NAME/setup.py ] || [[ "${GIT_NAME,,}" =~ ^.*frontend$ ]] || [[ "${GIT_NAME,,}" =~ ^.*storybook$ ]]; then
rm -rf $GIT_NAME
echo "Not a javascript package, the check was wrong"
/docker-entrypoint.sh $@
exit 0
fi
cd "$GIT_NAME"
#if PR
if [ -n "$GIT_CHANGE_ID" ] && [[ "$GIT_CHANGE_TARGET" == "master" ]] && [[ "$GIT_CHANGE_BRANCH" == "develop" ]]; then
echo "Starting pre-release on PULL REQUEST"
/wait_jenkins_branch_status.sh
git checkout $GIT_CHANGE_BRANCH
existing_tags=$(git tag)
if [ -z "$existing_tags" ]; then
echo "There are no tags, we need to first release initial version"
git checkout master
version=$(grep '"version"' package.json | awk -F'"' '{print $4}')
git tag -a $version -m "Initial release"
git push origin tag $version
git checkout $GIT_CHANGE_BRANCH
fi
version=$(grep '"version"' package.json | awk -F'"' '{print $4}')
#check if version was already updated
git fetch --tags
if [ $(git diff --name-status ${GIT_CHANGE_BRANCH}..${GIT_CHANGE_TARGET} | wc -l) -eq 0 ]; then
echo "There are no changes to release"
exit 0
fi
if [ $(git tag | grep ^${version}$ | wc -l) -eq 1 ]; then
echo "Start release with changelog update on new version"
if [ -z "$GIT_CHANGE_TITLE" ]; then
valid_curl_get_result https://api.github.com/repos/$GIT_ORG/$GIT_NAME/pulls/$GIT_CHANGE_ID title
GIT_CHANGE_TITLE=$(echo $curl_result | jq -r ".title")
echo "Extracted PR title - $GIT_CHANGE_TITLE"
fi
RELEASE_TYPE="patch"
if [ $(echo "$GIT_CHANGE_TITLE" | grep "^MINOR:" | wc -l ) -eq 1 ]; then
echo "Will use a MINOR version for release, title of PR is $GIT_CHANGE_TITLE"
RELEASE_TYPE="minor"
fi
if [ $(echo "$GIT_CHANGE_TITLE" | grep "^MAJOR:" | wc -l ) -eq 1 ]; then
echo "Will use a MAJOR version for release, title of PR is $GIT_CHANGE_TITLE"
RELEASE_TYPE="major"
fi
release-it --config /release-it.json --no-git.tag -i $RELEASE_TYPE --ci
else
echo "Existing version is not yet released, will only auto-update changelog"
npx_command=$(grep after:bump /release-it.json | awk -F'"' '{print $4}' | awk -F';' '{print $1}' )
sh -c "$npx_command"
sed -i '/ Automated release [0-9\.]\+ \|Add Sonarqube tag using .* addons list\|\[[jJ][eE][nN][kK][iI][nN][sS]\|[yY][aA][rR][nN]/d' CHANGELOG.md
if [ $(git diff CHANGELOG.md | tail -n +5 | grep ^+ | grep -Eiv '\- Automated release [0-9\.]+|Add Sonarqube tag using .* addons list|\[jenkins|yarn' | wc -l ) -gt 0 ]; then
# there were other commits besides the automated release ones"
git add CHANGELOG.md
git commit -m "Automated release $version"
git push
else
echo "Did not find any new commits beside the automated ones, will not add them"
git checkout -- CHANGELOG.md
fi
fi
fi
if [ -z "$GIT_CHANGE_ID" ] && [[ "$GIT_BRANCH" == "master" ]] ; then
echo "Starting release on github and npm"
if [ -n "$NPM_TOKEN" ]; then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
else
echo "Did no receive NPM_TOKEN variable, necessary for npm release"
exit 1
fi
# git checkout $GIT_BRANCH
if [ -n "$GIT_COMMIT" ]; then
echo "Received commit as a variable, will checkout on it instead of the $GIT_BRANCH branch"
git checkout $GIT_COMMIT
fi
version=$(grep '"version"' package.json | awk -F'"' '{print $4}')
#check if version was already updated
git fetch --tags
if [ $(git tag | grep ^${version}$ | wc -l) -eq 1 ]; then
echo "GitHub release already done, skipping tag creation"
else
#echo "Starting GitHub release of version ${version}"
#release-it -v
#sed -i 's/"release": false,/"release": true,/' /release-it.json
#release-it --no-increment --no-git --github.release --config /release-it.json --ci
echo "Create release on $GIT_BRANCH using GitHub API"
if [[ "$GIT_NAME" == "volto-eea-kitkat" ]]; then
/releaseChangelog.sh
cat releasefile
body=$(cat releasefile | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' | sed 's/"/\\\"/g' )
echo "{\"tag_name\": \"$version\",\"name\": \"$version\", \"target_commitish\":\"${GIT_BRANCH}\", \"body\": \"$body\"}"
else
body=$(npx auto-changelog --stdout --sort-commits date-desc --commit-limit false -u --template /release.hbs --ignore-commit-pattern 'Automated release [0-9\.]+ |Add Sonarqube tag using .* addons list|\[[jJ][eE][nN][kK][iI][nN][sS]|[yY][aA][rR][nN]' | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' | sed 's/"/\\\"/g')
fi
body=$(echo "$body" | sed 's/[R|r]efs #\([0-9]*\)/Refs \[#\1\]\(https:\/\/taskman.eionet.europa.eu\/issues\/\1\)/g' )
echo "{\"tag_name\": \"$version\",\"name\": \"$version\", \"target_commitish\":\"${GIT_BRANCH}\", \"body\": \"$body\"}"
curl -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: bearer $GITHUB_TOKEN" https://api.github.com/repos/${GIT_ORG}/${GIT_NAME}/releases -d "{\"tag_name\": \"$version\",\"name\": \"$version\", \"target_commitish\":\"${GIT_BRANCH}\", \"body\": \"$body\"}"
fi
#check if released
package_name=$(grep '"name"' package.json | awk -F'"' '{print $4}')
if [ $(npm search $package_name --json | grep "\"$package_name\"" | wc -l) -ne 0 ]; then
#check if already published
if [ $(npm view ${package_name}@$version | wc -l) -ne 0 ]; then
echo "NPM package already published"
already_published='yes'
fi
fi
if [ -z "$already_published" ]; then
echo "Publishing npm package"
echo "Checking if prepublish script exist"
if [ $(cat package.json | jq '.scripts.prepublish | length') -gt 0 ]; then
echo "Found prepublish script, running it"
yarn
yarn prepublish
fi
npm publish --access=public
echo "Waiting for npm to sync their data for yarn before updating frontends"
sleep 60
fi
echo "Check if format is x.y.z"
if [ $(echo $version | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | wc -l) -eq 0 ]; then
echo "Version format is not major.minor.patch, will skip frontend and kitkat update"
exit 0
fi
check_frontend=""
page=1
result=$(curl -s -H "Authorization: bearer $GIT_TOKEN" "https://api.github.com/orgs/eea/repos?type=all&per_page=100&page=$page")
while [ $(echo -e "$result" | grep name | wc -l ) -ne 0 ]; do
check_frontend="$check_frontend "$(echo -e "$result"| jq -r ".[] | select(.archived|not) | select(.name | endswith(\"-frontend\")) | .full_name")
check_frontend="$check_frontend "$(echo -e "$result"| jq -r ".[] | select(.archived|not) | select(.name | endswith(\"-kitkat\")) | select(.name | startswith(\"volto\")) | .full_name")
check_frontend="$check_frontend "$(echo -e "$result"| jq -r ".[] | select(.archived|not) | select(.name | endswith(\"-policy\")) | select(.name | startswith(\"volto\")) | .full_name")
page=$(($page+1))
result=$(curl -s -H "Authorization: bearer $GIT_TOKEN" "https://api.github.com/orgs/eea/repos?type=all&per_page=100&page=$page")
done
echo "List of non-archived frontends, kitkat & policy: $check_frontend"
echo "Checking and updating frontend dependencies in org:eea"
#check_frontend=$(curl -s -H "Accept: application/vnd.github.v3+json" -G --data-urlencode "q=org:eea frontend in:name" "https://api.github.com/search/repositories?per_page=100" | jq -r .items[].full_name )
current_pwd="$(pwd)"
for i in $( echo "$check_frontend" ); do
if [[ ! "$i" == "eea/fise-frontend" ]] && [[ ! "$i" == "eea/semanticsearch-frontend" ]]; then
update_package_json $i package.json $package_name $version develop
fi
done
#remove when migrated to develop branch
update_package_json eea/fise-frontend package.json $package_name $version eea-design-system
cd $current_pwd
fi