This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
cico_release.sh
executable file
·104 lines (89 loc) · 3.75 KB
/
cico_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
#!/bin/bash
# Show command before executing
set -x
# Exit on error
set -e
# This option sets the exit code of a pipeline to that of the rightmost command to exit with a
# non-zero status, or to zero if all commands of the pipeline exit successfully.
set -o pipefail
function main() {
# Enable verbose output
npm config set loglevel verbose
# Build and Release Planner (It will update the tag on github and push fabric8-planner to npmjs.org)
npm run semantic-release
}
# This function raises a PR against fabric8-npm-dependencies
function create_merge_PR {
# Fetch latest tags
git pull --tags origin master
# extract version number from latest git tag
new_planner_version=$(git tag --sort=-v:refname | head -1 | cut -d'v' -f 2)
# Create PR on fabric8-npm-dependencies and merge it
repo="fabric8-npm-dependencies"
org="fabric8-ui"
project="${org}/${repo}"
baseUrl="https://api.github.com/repos"
id=$(uuidgen)
git clone "https://github.com/${project}.git"
cd ${repo} && git checkout -b versionUpdate"${id}"
# find fabric8-planner > extract version number > remove ", char > trim whitespacs
current_planner_version=$( grep fabric8-planner package.json \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| tr -d '[:space:]' )
echo "New Planner version:" $new_planner_version
echo "Current Planner version:" $current_planner_version
if [ "$new_planner_version" == "$current_planner_version" ]; then
echo "Skippping as fabric8-planner is already on version $new_planner_version"
exit 0
fi
git config --global user.email [email protected]
git config --global user.name fabric8-cd
# Set authentication credentials to allow "git push"
git remote set-url origin https://fabric8cd:${GH_TOKEN}@github.com/${project}.git
message="fix(version): update package.json fabric8-planner to ${new_planner_version}"
updatePackageJSONVersion "$new_planner_version"
git add package.json
git commit -m "${message}"
git push origin versionUpdate"${id}"
local body="{
\"title\": \"${message}\",
\"head\": \"versionUpdate${id}\",
\"base\": \"master\"
}"
apiUrl="${baseUrl}/${project}/pulls"
echo "Creating PR for ${apiUrl}"
PR_id=$(curl --silent -X POST -H "Authorization: Bearer $GH_TOKEN" -d "${body}" "${apiUrl}" \
| sed -n 's/.*"number": \(.*\),/\1/p' )
echo "Received PR id: ${PR_id}"
# Wait for all CI checks on PR to be successful
waitUntilSuccess "${PR_id}" "${project}"
# Merge PR
apiUrl="${baseUrl}/${project}/pulls/${PR_id}/merge"
echo "Merging PR ${PR_id}"
curl --silent -X PUT -H "Authorization: Bearer $GH_TOKEN" "${apiUrl}"
}
# Updates fabric8-planner's version in package.json file
function updatePackageJSONVersion {
local f="package.json"
local p="fabric8-planner"
local v=$1
sed -i -r "s/\"${p}\": \"[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?(-development)?\"/\"${p}\": \"${v}\"/g" ${f}
}
# Wait for all CI checks to pass
function waitUntilSuccess {
pr_id=$1
project=$2
ref=$( curl --silent -X GET https://api.github.com/repos/"${project}"/pulls/"${pr_id}" \
| sed -n 's/.*"ref": "\(.*\)",/\1/p' | head -1) # Extract "ref" value from the response
status="NA"
NEXT_WAIT_TIME=0
# Max wait 720 seconds
until [ "$status" == "success" ] || [ $NEXT_WAIT_TIME -eq 7 ]; do
status=$( curl --silent -X GET https://api.github.com/repos/"${project}"/commits/"${ref}"/status \
| sed -n 's/.*"state": "\(.*\)",/\1/p') # Extract "state" value from the response
echo "Pull Request status: ${status}. Waiting to merge..."
sleep $(( NEXT_WAIT_TIME++ ))
done
}
main;