Skip to content

Add release automation script. #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions RELEASING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Releasing
=========

1. Update the version in Sources/Version.swift
2. `git commit -am "Version X.Y.Z"`
3. `git tag -a X.Y.Z -m "Version X.Y.Z"`
4. `git push && git push --tags`
5. Create a new github release at https://github.com/segmentio/analytics-swift/releases
* Summarize change history since last release in the notes.
Use `release.sh` to perform releases. This script will perform all the safety checks as well
as update Version.swfit, commit the change, and create tag + release. History since the last
released version will be used as the changelog for the release.

ex: $ ./release.sh 1.1.1

7 changes: 7 additions & 0 deletions Sources/Segment/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@

// Referred to by Analytics.swift

// DO NOT MODIFY THIS FILE BY HAND!!
// DO NOT MODIFY THIS FILE BY HAND!!
// DO NOT MODIFY THIS FILE BY HAND!!
// DO NOT MODIFY THIS FILE BY HAND!!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂


// Use release.sh's automation.

// BREAKING.FEATURE.FIX
internal let __segment_version = "1.0.1"
110 changes: 110 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

vercomp () {
if [[ $1 == $2 ]]
then
return 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the new version is the same as old version, we should error out, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}

# check if `gh` tool is installed.
if ! command -v gh &> /dev/null
then
echo "Github CLI tool is required, but could not be found."
echo "Install it via: $ brew install gh"
exit 1
fi

# check that we're on the `main` branch
branch=$(git rev-parse --abbrev-ref HEAD)
if [ branch != 'main' ]
then
echo "The 'main' branch must be the current branch out to make a release."
echo "You are currently on: $branch"
exit 1
fi

versionFile="./sources/Segment/Version.swift"

# get last line in version.swift
versionLine=$(tail -n 1 $versionFile)
# split at the =
version=$(cut -d "=" -f2- <<< "$versionLine")
# remove quotes and spaces
version=$(sed "s/[' \"]//g" <<< "$version")

echo "Analytics-Swift current version: $version"

# no args, so give usage.
if [ $# -eq 0 ]
then
echo "Release automation script"
echo ""
echo "Usage: $ ./release.sh <version>"
echo " ex: $ ./release.sh \"1.0.2\""
exit 0
fi

newVersion="${1%.*}.$((${1##*.}))"
echo "$version"
echo "$newVersion"

vercomp $newVersion $version
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac

if [ $op != '>' ]
then
echo "New version must be greater than previous version ($version)."
exit 1
fi

# get the commits since the last release...
# note: we do this here so the "Version x.x.x" commit doesn't show up in logs.
changelog=git log --pretty=format:"- (%an) %s" $(git describe --tags --abbrev=0 @^)..@
tempFile=$(mktemp)
#write changelog to temp file.
echo $changelog >> $tempFile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the reason for a temp file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I write the changelog out there, and then gh release will read it in from there.


# update sources/Segment/Version.swift
# - remove last line...
sed -i '' -e '$ d' $versionFile
# - add new line w/ new version
echo "internal let __segment_version = \"$newVersion\"" >> $versionFile

# commit the version change.
git commit -am "Version $newVersion" && git push
# gh release will make both the tag and the release itself.
gh release create $newVersion -F $tempFile -t "Version $newVersion"

# remove the tempfile.
rm $tempFile