Skip to content

Commit

Permalink
Add files to automate releasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Bakken committed Jun 10, 2016
1 parent 42a8ccd commit f4cc89c
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 6 deletions.
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.PHONY: release

unexport LANG
unexport LC_ADDRESS
unexport LC_COLLATE
unexport LC_CTYPE
unexport LC_IDENTIFICATION
unexport LC_MEASUREMENT
unexport LC_MESSAGES
unexport LC_MONETARY
unexport LC_NAME
unexport LC_NUMERIC
unexport LC_PAPER
unexport LC_TELEPHONE
unexport LC_TIME

# NB:
# VERSION does NOT include the v suffix

release:
ifeq ($(VERSION),)
$(error VERSION must be set to build a release and deploy this package)
endif
ifeq ($(RELEASE_GPG_KEYNAME),)
$(error RELEASE_GPG_KEYNAME must be set to build a release and deploy this package)
endif
@rm -rf pkg
@bash ./build/publish $(VERSION) validate
@sed -i'' -e 's/VERSION.*/VERSION = "$(VERSION)"/' ./lib/riak/version.rb
@rake package
@git commit -a -m "riak-client $(VERSION)"
@git tag --sign -a "v$(VERSION)" -m "riak-client $(VERSION)" --local-user "$(RELEASE_GPG_KEYNAME)"
@git push --tags master
@gem push "pkg/riak-client-$(VERSION).gem"
@bash ./build/publish $(VERSION)
5 changes: 0 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ task :gemspec do
gemspec.validate
end

desc %{Release the gem to RubyGems.org}
task :release => :gem do
system "gem push pkg/#{gemspec.name}-#{gemspec.version}.gem"
end

desc "Cleans up white space in source files"
task :clean_whitespace do
no_file_cleaned = true
Expand Down
198 changes: 198 additions & 0 deletions build/publish
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset

declare -r debug='false'
declare -r tmpfile_file="/tmp/publish.$$.tmpfiles"

function make_temp_file
{
local template="${1:-publish.$$.XXXXXX}"
if [[ $template != *XXXXXX ]]
then
template="$template.XXXXXX"
fi
local tmp=$(mktemp -t "$template")
echo "$tmp" >> "$tmpfile_file"
echo "$tmp"
}

function now
{
date '+%Y-%m-%d %H:%M:%S'
}

function pwarn
{
echo "$(now) [warning]: $@" 1>&2
}

function perr
{
echo "$(now) [error]: $@" 1>&2
}

function pinfo
{
echo "$(now) [info]: $@"
}

function pdebug
{
if [[ $debug == 'true' ]]
then
echo "$(now) [debug]: $@"
fi
}

function errexit
{
perr "$@"
exit 1
}

function onexit
{
if [[ -f $tmpfile_file ]]
then
for tmpfile in $(< $tmpfile_file)
do
pdebug "removing temp file $tmpfile"
rm -f $tmpfile
done
rm -f $tmpfile_file
fi
}

function gh_publish {
if [[ -z $version_string ]]
then
errexit 'gh_publish: version_string required'
fi

local -r gem_file="riak-client-$version_string.gem"
local -r gem_file_path="pkg/$gem_file"
if [[ ! -s $gem_file_path ]]
then
errexit "gh_publish: expected to find $gem_file in $gem_file_path"
fi

local -r release_json="{
\"tag_name\" : \"v$version_string\",
\"name\" : \"Riak Ruby Client $version_string\",
\"body\" : \"riak-ruby-client $version_string\nhttps://github.com/basho/riak-ruby-client/blob/master/RELNOTES.md\",
\"draft\" : false,
\"prerelease\" : $is_prerelease
}"

pdebug "Release JSON: $release_json"

local curl_content_file="$(make_temp_file)"
local curl_stdout_file="$(make_temp_file)"
local curl_stderr_file="$(make_temp_file)"

curl -4so $curl_content_file -w '%{http_code}' -XPOST \
-H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/json' \
'https://api.github.com/repos/basho/riak-ruby-client/releases' -d "$release_json" 1> "$curl_stdout_file" 2> "$curl_stderr_file"
if [[ $? != 0 ]]
then
errexit "curl error exited with code: '$?' see '$curl_stderr_file'"
fi

local -i curl_rslt="$(< $curl_stdout_file)"
if (( curl_rslt == 422 ))
then
pwarn "Release in GitHub already exists! (http code: '$curl_rslt')"
curl -4so $curl_content_file -w '%{http_code}' -XGET \
-H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/json' \
"https://api.github.com/repos/basho/riak-ruby-client/releases/tags/v$version_string" 1> "$curl_stdout_file" 2> "$curl_stderr_file"
if [[ $? != 0 ]]
then
errexit "curl error exited with code: '$?' see '$curl_stderr_file'"
fi
elif (( curl_rslt != 201 ))
then
errexit "Creating release in GitHub failed with http code '$curl_rslt'"
fi

if [[ ! -s $curl_content_file ]]
then
errexit 'no release info to parse for asset uploads'
fi

# "upload_url": "https://uploads.github.com/repos/basho/riak-ruby-client/releases/1115734/assets{?name,label}"
# https://uploads.github.com/repos/basho/riak-ruby-client/releases/1115734/assets{?name,label}
local -r upload_url_with_name=$(perl -ne 'print qq($1\n) and exit if /"upload_url"[ :]+"(https:\/\/[^"]+)"/' "$curl_content_file")
local -r upload_url="${upload_url_with_name/\{?name,label\}/?name=$gem_file}"

local curl_content_file="$(make_temp_file)"
local curl_stdout_file="$(make_temp_file)"
local curl_stderr_file="$(make_temp_file)"

curl -4so $curl_content_file -w '%{http_code}' -XPOST \
-H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/x-compressed, application/x-tar' \
"$upload_url" --data-binary "@$gem_file_path" 1> "$curl_stdout_file" 2> "$curl_stderr_file"
if [[ $? != 0 ]]
then
errexit "curl error exited with code: '$?' see '$curl_stderr_file'"
fi

curl_rslt="$(< $curl_stdout_file)"
if (( curl_rslt != 201 ))
then
errexit "Uploading release assets to GitHub failed with http code '$curl_rslt'"
fi
}

trap onexit EXIT

declare -r version_string="${1:-unknown}"

if [[ ! $version_string =~ ^[0-9].[0-9].[0-9](.pre[0-9]+)?$ ]]
then
errexit 'first argument must be valid version string in vX.Y.Z format'
fi

is_prerelease='false'
if [[ $version_string =~ ^[0-9].[0-9].[0-9].pre[0-9]+$ ]]
then
pinfo "publishing pre-release version: $version_string"
is_prerelease='true'
else
pinfo "publishing version $version_string"
fi

declare -r github_api_key_file="$HOME/.ghapi"
if [[ ! -s $github_api_key_file ]]
then
errexit "please save your GitHub API token in $github_api_key_file"
fi

# Validate commands
if ! hash curl 2>/dev/null
then
errexit "'curl' must be in your PATH"
fi

republish=${2:-''}
if [[ $republish == 'republish' ]]
then
gh_publish
exit 0
fi

declare -r current_branch="$(git rev-parse --abbrev-ref HEAD)"

if [[ $debug == 'false' && $is_prerelease == 'false' && $current_branch != 'master' ]]
then
errexit 'publish must be run on master branch'
fi

validate=${2:-''}
if [[ $validate == 'validate' ]]
then
exit 0
fi

gh_publish
2 changes: 1 addition & 1 deletion riak-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
gem.email = ['[email protected]']
gem.homepage = "http://github.com/basho/riak-ruby-client"
gem.authors = ['Bryce Kerley']
gem.license = 'Apache 2.0'
gem.license = 'Apache-2.0'

gem.required_ruby_version = '>= 1.9.3'

Expand Down

0 comments on commit f4cc89c

Please sign in to comment.