Skip to content

Commit 0dde9d3

Browse files
author
Luke Bakken
committed
Automate releases
1 parent abf7a25 commit 0dde9d3

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ test: compile eunit ct
2323
clean:
2424
@$(REBAR) clean
2525

26+
release: compile
27+
ifeq ($(VERSION),)
28+
$(error VERSION must be set to build a release and deploy this package)
29+
endif
30+
ifeq ($(RELEASE_GPG_KEYNAME),)
31+
$(error RELEASE_GPG_KEYNAME must be set to build a release and deploy this package)
32+
endif
33+
@echo "==> Tagging version $(VERSION)"
34+
@bash ./build/publish $(VERSION) validate
35+
@git tag --sign -a "$(VERSION)" -m "erlang_protobuffs $(VERSION)" --local-user "$(RELEASE_GPG_KEYNAME)"
36+
@git push --tags
37+
@bash ./build/publish $(VERSION)
38+
2639
APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
2740
xmerl webtool snmp public_key mnesia eunit syntax_tools compiler
2841
COMBO_PLT = $(HOME)/.$(REPO)_combo_dialyzer_plt

build/publish

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
declare -r debug='false'
7+
declare -r tmpfile_file="/tmp/publish.$$.tmpfiles"
8+
9+
function make_temp_file
10+
{
11+
local template="${1:-publish.$$.XXXXXX}"
12+
if [[ $template != *XXXXXX ]]
13+
then
14+
template="$template.XXXXXX"
15+
fi
16+
local tmp=$(mktemp -t "$template")
17+
echo "$tmp" >> "$tmpfile_file"
18+
echo "$tmp"
19+
}
20+
21+
function now
22+
{
23+
date '+%Y-%m-%d %H:%M:%S'
24+
}
25+
26+
function pwarn
27+
{
28+
echo "$(now) [warning]: $@" 1>&2
29+
}
30+
31+
function perr
32+
{
33+
echo "$(now) [error]: $@" 1>&2
34+
}
35+
36+
function pinfo
37+
{
38+
echo "$(now) [info]: $@"
39+
}
40+
41+
function pdebug
42+
{
43+
if [[ $debug == 'true' ]]
44+
then
45+
echo "$(now) [debug]: $@"
46+
fi
47+
}
48+
49+
function errexit
50+
{
51+
perr "$@"
52+
exit 1
53+
}
54+
55+
function onexit
56+
{
57+
if [[ -f $tmpfile_file ]]
58+
then
59+
for tmpfile in $(< $tmpfile_file)
60+
do
61+
pdebug "removing temp file $tmpfile"
62+
rm -f $tmpfile
63+
done
64+
rm -f $tmpfile_file
65+
fi
66+
}
67+
68+
function gh_publish {
69+
if [[ -z $version_string ]]
70+
then
71+
errexit 'gh_publish: version_string required'
72+
fi
73+
74+
# NB: we use a X.Y.Z tag
75+
local -r release_json="{
76+
\"tag_name\" : \"$version_string\",
77+
\"name\" : \"Erlang Protobuffs $version_string\",
78+
\"body\" : \"erlang_protobuffs $version_string\nhttps://github.com/basho/erlang_protobuffs/blob/master/RELNOTES.md\",
79+
\"draft\" : false,
80+
\"prerelease\" : $is_prerelease
81+
}"
82+
83+
pdebug "Release JSON: $release_json"
84+
85+
local curl_content_file="$(make_temp_file)"
86+
local curl_stdout_file="$(make_temp_file)"
87+
local curl_stderr_file="$(make_temp_file)"
88+
89+
curl -4so $curl_content_file -w '%{http_code}' -XPOST \
90+
-H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/json' \
91+
'https://api.github.com/repos/basho/erlang_protobuffs/releases' -d "$release_json" 1> "$curl_stdout_file" 2> "$curl_stderr_file"
92+
if [[ $? != 0 ]]
93+
then
94+
errexit "curl error exited with code: '$?' see '$curl_stderr_file'"
95+
fi
96+
97+
local -i curl_rslt="$(< $curl_stdout_file)"
98+
if (( curl_rslt == 422 ))
99+
then
100+
pwarn "Release in GitHub already exists! (http code: '$curl_rslt')"
101+
curl -4so $curl_content_file -w '%{http_code}' -XGET \
102+
-H "Authorization: token $(< $github_api_key_file)" -H 'Content-type: application/json' \
103+
"https://api.github.com/repos/basho/erlang_protobuffs/releases/tags/$version_string" 1> "$curl_stdout_file" 2> "$curl_stderr_file"
104+
if [[ $? != 0 ]]
105+
then
106+
errexit "curl error exited with code: '$?' see '$curl_stderr_file'"
107+
fi
108+
elif (( curl_rslt != 201 ))
109+
then
110+
errexit "Creating release in GitHub failed with http code '$curl_rslt'"
111+
fi
112+
}
113+
114+
trap onexit EXIT
115+
116+
declare -r version_string="${1:-unknown}"
117+
118+
if [[ ! $version_string =~ ^[0-9].[0-9].[0-9](-[a-z]+[0-9]+)?$ ]]
119+
then
120+
errexit 'first argument must be valid version string in X.Y.Z format'
121+
fi
122+
123+
is_prerelease='false'
124+
if [[ $version_string =~ ^[0-9].[0-9].[0-9]-[a-z]+[0-9]+$ ]]
125+
then
126+
pinfo "publishing pre-release version: $version_string"
127+
is_prerelease='true'
128+
else
129+
pinfo "publishing version $version_string"
130+
fi
131+
132+
declare -r current_branch="$(git rev-parse --abbrev-ref HEAD)"
133+
134+
if [[ $debug == 'false' && $is_prerelease == 'false' && $current_branch != 'master' ]]
135+
then
136+
errexit 'publish must be run on master branch'
137+
fi
138+
139+
declare -r github_api_key_file="$HOME/.ghapi"
140+
if [[ ! -s $github_api_key_file ]]
141+
then
142+
errexit "please save your GitHub API token in $github_api_key_file"
143+
fi
144+
145+
# Validate commands
146+
if ! hash curl 2>/dev/null
147+
then
148+
errexit "'curl' must be in your PATH"
149+
fi
150+
151+
validate=${2:-''}
152+
if [[ $validate == 'validate' ]]
153+
then
154+
exit 0
155+
fi
156+
157+
gh_publish

0 commit comments

Comments
 (0)