From f4cc89c12bc8159fee68a3346e452344190ec81d Mon Sep 17 00:00:00 2001 From: Luke Bakken Date: Fri, 10 Jun 2016 13:59:16 -0700 Subject: [PATCH] Add files to automate releasing --- Makefile | 35 ++++++++ Rakefile | 5 -- build/publish | 198 ++++++++++++++++++++++++++++++++++++++++++++ riak-client.gemspec | 2 +- 4 files changed, 234 insertions(+), 6 deletions(-) create mode 100644 Makefile create mode 100755 build/publish diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..09793873 --- /dev/null +++ b/Makefile @@ -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) diff --git a/Rakefile b/Rakefile index aed7012a..c56e53fb 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/build/publish b/build/publish new file mode 100755 index 00000000..e12da190 --- /dev/null +++ b/build/publish @@ -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 diff --git a/riak-client.gemspec b/riak-client.gemspec index 0439dd50..6e477ab4 100644 --- a/riak-client.gemspec +++ b/riak-client.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |gem| gem.email = ['bryce@basho.com'] 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'