Skip to content

Instantly share code, notes, and snippets.

@phbergsmann
Last active August 29, 2015 13:58
Show Gist options
  • Save phbergsmann/10008430 to your computer and use it in GitHub Desktop.
Save phbergsmann/10008430 to your computer and use it in GitHub Desktop.
Automatic release-script specific for chef cookbooks (replaces version in metadata.rb)
cd ${Name}
# make sure to have a clean repository
git reset ${Revision} --hard
# retrieve latest tagged version. if no version has been tagged
# set latest verson to 0.0.0
TAGCOUNT=`git ls-remote --tags -q | wc -l | tr -d ' '`
echo $TAGCOUNT
if [ $TAGCOUNT -gt 0 ]
then
LASTREVISION=$(git rev-list --tags --max-count=1)
LASTTAG=$(git describe --tags "$LASTREVISION")
LASTVERSION=$(echo "$LASTTAG" | cut -c 2-)
else
LASTVERSION=0.0.0
LASTREVISION=$(git rev-list HEAD | tail -n 1)
fi
echo "Last version: $LASTVERSION"
# retrieve the log since the last tagged version
LOG=$(git log --format=format:"%s" "$LASTREVISION"..HEAD)
# when there is a commit with [TASK] or [FEATURE] in the commit
# message increment the minor version-number. if the last commits
# don't include a feature or a task increment the patchlevel
if [[ $LOG =~ ^\[(TASK|FEATURE)\].*$ ]]; then
MIDVERSION="${LASTVERSION%.*}"
NEWVERSION="${MIDVERSION%.*}.$((${MIDVERSION##*.}+1)).0"
else
NEWVERSION="${LASTVERSION%.*}.$((${LASTVERSION##*.}+1))"
fi
echo "New version: $NEWVERSION"
# branch a new release
git checkout -b release/$NEWVERSION
# replace the versionnumber in metadata.rb and commit the file
$(perl -pi -e "s/version\ \"[0-9\.]*\"/version \"${NEWVERSION}\"/g" metadata.rb)
git add -A
git commit -m "[TASK] Raises versionnumber"
# generate a ChangeLog and commit it
git log --date=short --format=format:"%ad %h %s (%aN)" > ChangeLog
git add -A
git commit -m "[TASK] Generates ChangeLog"
# merge the release back to master and develop
git checkout master
git merge --no-ff -X theirs release/$NEWVERSION
git checkout develop
git merge --no-ff -X theirs release/$NEWVERSION
git branch -d release/$NEWVERSION
# amend a new ChangeLog so it includes the merge-commit with the
# version-number in develop and master
git checkout master
git log --date=short --format=format:"%ad %h %s (%aN)" > ChangeLog
git add -A
git commit --amend --no-edit
git tag -a v$NEWVERSION -m "Automatic release of v$NEWVERSION by Jenkins CI"
git push origin master --tags
git checkout develop
git log --date=short --format=format:"%ad %h %s (%aN)" > ChangeLog
git add -A
git commit --amend --no-edit
git push origin develop --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment