-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.sh
155 lines (131 loc) · 3.01 KB
/
release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
set -o nounset
export GITHUB_USER=conormcd
export GITHUB_REPO=clojure-test-junit-output
abort() {
echo "$@"
exit 1
}
current_version() {
grep -m1 '(defproject' project.clj | \
sed -e 's,^.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$,\1,'
}
major() {
current_version | cut -d. -f1
}
minor() {
current_version | cut -d. -f2
}
patch() {
if [ -n "${CIRCLE_BUILD_NUM:-}" ]; then
echo "${CIRCLE_BUILD_NUM}"
else
current_version | cut -d. -f3
fi
}
branch() {
if [ -n "${CIRCLE_BRANCH:-}" ]; then
echo "${CIRCLE_BRANCH}"
else
git rev-parse --abbrev-ref HEAD
fi
}
sha() {
if [ -n "${CIRCLE_SHA1:-}" ]; then
echo "${CIRCLE_SHA1}"
else
git rev-parse HEAD
fi
}
sha_short() {
sha | sed -e 's,^\(........\).*,\1,'
}
new_version() {
if [ "$(branch)" != "master" ]; then
echo "$(major).$(minor).$(patch)-$(branch)-$(sha_short)"
else
echo "$(major).$(minor).$(patch)"
fi
}
sed_inplace() {
if sed --help 2>&1 | grep -q -m 1 GNU; then
sed -i -e "$@"
else
sed -i '' -e "$@"
fi
}
update_project_clj() {
local _v=$1
echo "Updating project.clj to version ${_v}"
sed_inplace "s,\((defproject ${GITHUB_REPO} \)\"[^\"]*\",\1\"${_v}\"," project.clj
}
build_jars() {
echo "Building JARs..."
lein jar 2>&1 | sed -e 's,^, ,'
}
save_artifacts() {
if [ -n "${CIRCLE_ARTIFACTS:-}" ]; then
echo "Copying JARs to CircleCI artifacts..."
find . -name '*.jar' -exec cp {} "${CIRCLE_ARTIFACTS}" \;
echo "Copying junit.xml to CircleCI artifacts..."
cp test/junit.xml "${CIRCLE_ARTIFACTS}/"
fi
}
ensure_github_release_tool_installed() {
local _cwd
_cwd=$(pwd)
if [ -z "${GOPATH:-}" ]; then
export GOPATH=${_cwd}/.go
fi
export PATH="${PATH}:${GOPATH}/bin"
if ! which github-release > /dev/null 2>&1; then
echo "Installing github-release..."
go get github.com/aktau/github-release
fi
}
github_release() {
local _version
_version=$1
echo "Releasing ${_version}"
github-release release \
--user "${GITHUB_USER}" \
--repo "${GITHUB_REPO}" \
--tag "${_version}" \
--target "$(sha)" \
--description "Release version ${_version}"
find . -name '*.jar' | while read -r _jar; do
github-release upload \
--user "${GITHUB_USER}" \
--repo "${GITHUB_REPO}" \
--tag "${_version}" \
--name "$(basename "${_jar}")" \
--file "${_jar}"
done
}
clojars_deploy() {
lein deploy clojars 2>&1 | sed -e 's,^, ,'
}
cleanup() {
git clean -ffdx
}
v=$(new_version)
update_project_clj "${v}"
build_jars
save_artifacts
if [ "$(branch)" = "master" ]; then
if [ -z "${CLOJARS_USERNAME:-}" ]; then
abort "Missing environment variable: CLOJARS_USERNAME"
fi
if [ -z "${CLOJARS_PASSWORD:-}" ]; then
abort "Missing environment variable: CLOJARS_PASSWORD"
fi
if [ -z "${GITHUB_TOKEN:-}" ]; then
abort "Missing environment variable: GITHUB_TOKEN"
fi
# Make a GitHub release
ensure_github_release_tool_installed
github_release "${v}"
# Push to clojars
clojars_deploy
fi
cleanup