Skip to content

Commit a1b9ed7

Browse files
committed
Set up continuous delivery with CircleCI.
- All successful CircleCI builds save a copy of the JAR in the CircleCI artifacts system. - Successful builds on master trigger a GitHub release which tags the relevant revision with the version number and saves a copy of the JAR in the releases section of the GitHub project page.
1 parent d0424e4 commit a1b9ed7

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

circle.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
deployment:
2+
all:
3+
branch: /.*/
4+
commands:
5+
- bash release.sh

release.sh

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/bin/bash
2+
3+
set -o nounset
4+
5+
export GITHUB_USER=conormcd
6+
export GITHUB_REPO=clj-libssh2
7+
8+
current_version() {
9+
grep -m1 '(defproject' project.clj | \
10+
sed -e 's,^.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$,\1,'
11+
}
12+
13+
major() {
14+
current_version | cut -d. -f1
15+
}
16+
17+
minor() {
18+
current_version | cut -d. -f2
19+
}
20+
21+
patch() {
22+
if [ -n "${CIRCLE_BUILD_NUM:-}" ]; then
23+
echo "${CIRCLE_BUILD_NUM}"
24+
else
25+
current_version | cut -d. -f3
26+
fi
27+
}
28+
29+
branch() {
30+
if [ -n "${CIRCLE_BRANCH:-}" ]; then
31+
echo "${CIRCLE_BRANCH}"
32+
else
33+
git rev-parse --abbrev-ref HEAD
34+
fi
35+
}
36+
37+
sha() {
38+
if [ -n "${CIRCLE_SHA1:-}" ]; then
39+
echo "${CIRCLE_SHA1}"
40+
else
41+
git rev-parse HEAD
42+
fi
43+
}
44+
45+
sha_short() {
46+
sha | sed -e 's,^\(........\).*,\1,'
47+
}
48+
49+
new_version() {
50+
if [ "$(branch)" != "master" ]; then
51+
echo "$(major).$(minor).$(patch)-$(branch)-$(sha_short)"
52+
else
53+
echo "$(major).$(minor).$(patch)"
54+
fi
55+
}
56+
57+
sed_inplace() {
58+
if sed --help 2>&1 | grep -q -m 1 GNU; then
59+
sed -i -e "$@"
60+
else
61+
sed -i '' -e "$@"
62+
fi
63+
}
64+
65+
update_project_clj() {
66+
local _v=$1
67+
echo "Updating project.clj to version ${_v}"
68+
sed_inplace "s,\((defproject clj-libssh2 \)\"[^\"]*\",\1\"${_v}\"," project.clj
69+
}
70+
71+
build_jars() {
72+
echo "Building JARs..."
73+
lein jar 2>&1 | sed -e 's,^, ,'
74+
}
75+
76+
save_artifacts() {
77+
if [ -n "${CIRCLE_ARTIFACTS:-}" ]; then
78+
echo "Copying JARs to CircleCI artifacts..."
79+
find . -name '*.jar' -exec cp {} "${CIRCLE_ARTIFACTS}" \;
80+
fi
81+
}
82+
83+
ensure_github_release_tool_installed() {
84+
local _cwd
85+
_cwd=$(pwd)
86+
87+
if [ -z "${GOPATH:-}" ]; then
88+
export GOPATH=${_cwd}/.go
89+
fi
90+
export PATH="${PATH}:${GOPATH}/bin"
91+
if ! which github-release > /dev/null 2>&1; then
92+
echo "Installing github-release..."
93+
go get github.com/aktau/github-release
94+
fi
95+
}
96+
97+
github_release() {
98+
local _version
99+
_version=$1
100+
101+
echo "Releasing ${_version}"
102+
github-release release \
103+
--user "${GITHUB_USER}" \
104+
--repo "${GITHUB_REPO}" \
105+
--tag "${_version}" \
106+
--target "$(sha)" \
107+
--description "Release version ${_version}"
108+
find . -name '*.jar' | while read -r _jar; do
109+
github-release upload \
110+
--user "${GITHUB_USER}" \
111+
--repo "${GITHUB_REPO}" \
112+
--tag "${_version}" \
113+
--name "$(basename "${_jar}")" \
114+
--file "${_jar}"
115+
done
116+
}
117+
118+
v=$(new_version)
119+
update_project_clj "${v}"
120+
build_jars
121+
save_artifacts
122+
if [ "$(branch)" = "master" ]; then
123+
# TODO: Make the lack of a GitHub token fatal.
124+
if [ -n "${GITHUB_TOKEN:-}" ]; then
125+
ensure_github_release_tool_installed
126+
github_release "${v}"
127+
fi
128+
# TODO: Push to clojars
129+
fi

0 commit comments

Comments
 (0)