(feat) CI: adding version incrementer (#21)

* Adds version incrementer
This commit is contained in:
Raj Babu Das
2019-10-10 15:16:51 +05:30
committed by Karthik Satchitanand
parent de971bb1fa
commit e74a8be549
11 changed files with 134 additions and 106 deletions

23
scripts/version/push.sh Normal file
View File

@@ -0,0 +1,23 @@
#!/bin/sh
setup_git() {
git config --global user.email "travis@travis-ci.org"
git config --global user.name "Travis CI"
git remote set-url origin https://${GITHUB_TOKEN}@github.com/litmuschaos/community-charts.git > /dev/null 2>&1
}
commit_updated_changes() {
git checkout master
git status
git add .
git commit --message " $TRAVIS_BUILD_NUMBER: version upgraded for chaos-charts"
git status
}
upload_files() {
git remote -v
git push origin master
}
setup_git
commit_updated_changes
upload_files

11
scripts/version/readme.md Normal file
View File

@@ -0,0 +1,11 @@
## Version
## Implementation
Whenever there is any commit in charts it will increment the patch version by one unit
Steps:
1. version_maker.sh will take the second last commit because the latest commit is the changes which are done by the user by which Travis is triggered.
2. It compares the changes with the second last commit and stores the changed file to an array.
3. It will go through each file and increment a patch version to each changed file

View File

@@ -0,0 +1,78 @@
#! /bin/sh
# Retrive the last pushed commit from the repo
second_last_commit_hash=`git log -n 2 --pretty=format:"%H" | tail -1`
echo "Second Last commit hash: $second_last_commit_hash"
# # This function is used to parse the yaml file.
function yaml_parser() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
# This function takes the old version from the last commit
# and increments the existing version by one unit.
versionInc(){
echo "version inc"
file=$1
eval $(yaml_parser $file)
if [[ $? == 0 ]]
then
existing_version=$metadata_version
echo "Existing version: $existing_version"
# stores the last pushed committed file as temp.yaml in the root directory
# and will be deleted after the job
temp_file=`git show $second_last_commit_hash:$1 >> temp.yaml`
eval $(yaml_parser './temp.yaml')
if [[ $? == 0 ]]
then
oldversion=$metadata_version
echo "Oldversion : $oldversion"
sudo python3 scripts/version/version_validator.py $existing_version $oldversion
ret_code=$?
if [[ $ret_code == 0 ]]; then
echo "$file's version updated from $oldversion to $existing_version"
elif [[ $ret_code == 2 ]]; then
# storing version to an array
versions=( ${oldversion//./ } )
((versions[2]++)) # Increment the patch version by one unit
newversion="${versions[0]}.${versions[1]}.${versions[2]}"
`sed -i "s/$existing_version/$newversion/" $file`
echo "$file's version updated from $oldversion to $existing_version"
fi
fi
# deleting the temporary file
rm './temp.yaml'
fi
}
# compare and retrive the changed files
check_diff=`git diff ${second_last_commit_hash} --name-only`
files=$(echo $check_diff | tr " " "\n")
for file in $files
do
echo $file
# For chart service version or experiment
if [[ "$file" =~ \version.yaml$ ]] || [[ "$file" =~ \experiment.yaml$ ]]; then
versionInc $file
fi
done

View File

@@ -0,0 +1,22 @@
import re
import sys
from packaging import version
# semantic version regex
regex = "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
existing_version = sys.argv[1]
old_version = sys.argv[2]
result = re.search(regex, existing_version)
if result:
if version.parse(existing_version) > version.parse(old_version):
print("version validation successful")
sys.exit(0) # on success
elif version.parse(existing_version) == version.parse(old_version):
print("versions are equal")
sys.exit(2) # on success
else:
print("version validation failed")
sys.exit(1) #on failure
else:
print("version validation failed")
sys.exit(1) #on failure