forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
253 lines (227 loc) · 8.43 KB
/
utils.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
#!/bin/bash
#
# Copyright (C) 2015 Red Hat, Inc.
#
# 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
#
# http://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.
#
function get_project_version() {
project_version=$(mvn -q \
-Dexec.executable="echo" \
-Dexec.args='${project.version}' \
--non-recursive \
org.codehaus.mojo:exec-maven-plugin:1.3.1:exec)
echo $project_version
}
function setup_git_ssh() {
git config --global user.email fabric8cd@gmail.com
git config --global user.name fabric8-cd
}
function push_tag() {
release_version=$1
git tag -fa v${release_version} -m 'Release version ${release_version}'
git push origin v${release_version}
}
function setup_workspace_for_release() {
git tag -d $(git tag)
git fetch --tags
if [ ! -z $use_git_tag_next_version ]; then
new_version=$(get_new_ver_from_tag)
echo "New release version $new_version"
mvn -B -U versions:set -DnewVersion=${new_version} $mvnExtraArgs
git commit -a -m "release ${new_version}"
push_tag $new_version
else
mvn -B build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} $mvnExtraArgs
fi
git checkout -b release-v${new_version}
echo "$new_version"
}
function semver_parse_into() {
local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
#MAJOR
eval $2=`echo $1 | sed -e "s#$RE#\1\#"`
#MINOR
eval $3=`echo $1 | sed -e "s#$RE#\2\#"`
#PATCH
eval $4=`echo $1 | sed -e "s#$RE#\3\#"`
#SPECIAL
eval $5=`echo $1 | sed -e "s#$RE#\4\#"`
}
function get_new_ver_from_tag() {
version='1.0.0'
# Set known prerelease prefixes, needed for the proper sort order in the next command
git config versionsort.prereleaseSuffix -RC
git config versionsort.prereleaseSuffix -M
# if the repo has no tags this command will fail
git tag --sort version:refname | tail -1 > version.tmp
tag=$(cat version.tmp)
if [ ! -z "$tag" ]; then
local MAJOR=0
local MINOR=0
local PATCH=0
local SPECIAL=""
semver_parse_into $tag MAJOR MINOR PATCH SPECIAL
MAJOR_VERSION=$MAJOR
MINOR_VERSION=$MINOR
PATCH_VERSION=$(($PATCH + 1))
echo "$MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION"
else
echo $version
fi
}
# check the logic of this for getting repo_id
function get_repo_ids() {
find target/ -maxdepth 1 -name "*.properties" > target/repos.txt
filename=$(cat target/repos.txt)
repo_id=$filename
#repo_id=$(cat $filename)
echo repo_id
exit 0
}
# check this
function stage_sonatype_repo() {
mvn clean -B
mvn -V -B -e -U install org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy -P release -DnexusUrl=https://oss.sonatype.org -DserverId=oss-sonatype-staging
get_repo_ids
}
function stage_project() {
setup_git_ssh
if [ -z $GH_USER ]; then
GH_USER=fabric8cd
echo $GH_USER
fi
git remote set-url origin https://${GH_USER}:${GH_TOKEN}@github.com/${project}.git
current_project_version=$(get_project_version)
#version=$(setup_workspace_for_release)
version=4.0.7
mvn versions:set -DnewVersion=$version
cd kubernetes-model/
mvn versions:set -DnewVersion=$version
cd -
repo_ids=$(stage_sonatype_repo)
echo "$project,$version,$repo_ids"
}
# check how to handle if there is a failure in the release
function release_sonatype_repo() {
repo_id=$1
mvn -B org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-release -DserverId=oss-sonatype-staging -DnexusUrl=https://oss.sonatype.org -DstagingRepositoryId=${repo_id} -Ddescription=\"Next release is ready\" -DstagingProgressTimeoutMinutes=60
}
function wait_until_artifacts_are_synced_in_central() {
version=$1
NEXT_WAIT_TIME=0
until [ $(is_artifact_available_in_repo $version) == "success" ] || [ $NEXT_WAIT_TIME -eq 10 ]; do
echo "File yet not present in maven central"
sleep $(( NEXT_WAIT_TIME++ ))
done
}
function is_artifact_available_in_repo() {
repo="http://central.maven.org/maven2"
group_id="io/fabric8"
artifact_id="$artifact_id_to_watch_in_central"
version=$1
ext="jar"
url="${repo}/${group_id}/${artifact_id}/${version}/${artifact_id}-${version}.${ext}"
if curl --output /dev/null --silent --head --fail "$url"; then
echo "success"
else
echo "failure"
fi
}
function update_downstream_dependencies() {
# array of projects projects=("fabric8io/kubernetes-client" "fabric8io/kubernetes-model")
projects=$1
property_name=$2
version=$3
if [ -z $GH_USER ]; then
GH_USER=fabric8cd
echo $GH_USER
fi
for project in ${projects[@]}; do
repo=$(echo "$project" | cut -d "/" -f2)
rm -rf ${repo}
git clone https://github.com/${project}.git
pushd $repo
git remote set-url origin https://${GH_USER}:${GH_TOKEN}@github.com/${project}.git
uid=$(uuidgen)
git checkout -b versionUpdate${uid}
# read pom.xml file
pom_file='pom.xml'
cat $pom_file
update_version $pom_file $property_name $version
commit_message="Update pom property ${propertyName} to ${version}"
git add $pom_file
output=$(git commit -m "${commit_message}")
if [[ $output = *"nothing to commit"* ]]; then
echo "There was no version update hence no change, skip commit"
else
echo "Version updated pushing changes"
git push origin versionUpdate${uid}
body="{
\"title\": \"${commit_message}\",
\"head\": \"versionUpdate${uid}\",
\"base\": \"master\"
}"
pr_id=$(create_pull_request "${project}" "${body}")
wait_until_success "${pr_id}" "${project}"
merge_pr "${pr_id}" "${project}"
fi
popd
done
}
function update_version() {
# eg: sed -i -e 's/<kubernetes.model.version>3.0.2<\/kubernetes.model.version>/
# <kubernetes.model.version>3.0.3<\/kubernetes.model.version>/g' pom.xml
# updating the latest version in dependent projects pom.xml
# params: pom_file, property_name - property to be updated in pom.xml, version is the latest version
pom_file=$1
property_name=$2
version=$3
previous_version=$(mvn -q -Dexec.executable="echo" \
-Dexec.args='${'${property_name}'}' \
--non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec)
original_string="<$property_name>$previous_version<\/$property_name>"
replace_string="<$property_name>$version<\/$property_name>"
sed -i -e "s/$original_string/$replace_string/g" $pom_file
cat $pom_file
}
function create_pull_request() {
project=$1
body=$2
api_url="https://api.github.com/repos/${project}/pulls"
pr_id=$(curl --silent -X POST -H "Authorization: Bearer $GH_TOKEN" -d "${body}" "${api_url}" \
| sed -n 's/.*"number": \(.*\),/\1/p' )
echo "$pr_id"
}
function merge_pr() {
# Merge PR
pr_id=$1
project=$2
api_url="https://api.github.com/repos/${project}/pulls/${pr_id}/merge"
echo "Merging PR ${pr_id}"
curl --silent -X PUT -H "Authorization: Bearer $GH_TOKEN" "${api_url}"
}
function wait_until_success {
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
}