-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-publish-release.sh
executable file
·183 lines (154 loc) · 5.31 KB
/
create-publish-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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash
# set -o pipefail # exit if pipe command fails
[ -z "$DEBUG" ] || set -x
set -e
##
NAME="openzwave2mqtt"
DOCKER_TAG="jriguera/$NAME"
RELEASE="openzwave2mqtt"
DESCRIPTION="OpenZwave to MQTT bridge, ozwdaemon"
GITHUB_REPO="jriguera/docker-rpi-openzwave2mqtt"
###
DOCKER="docker"
JQ="jq"
CURL="curl -s"
RE_VERSION_NUMBER='^[0-9]+([0-9\.]*[0-9]+)*$'
###
ARCH=""
case "$(uname -m)" in
armv7l)
ARCH='arm'
;;
x86_64|amd64)
ARCH='amd64'
;;
*)
echo "ERROR: unsupported architecture: $(uname -m)"
exit 1
;;
esac
VERSION=""
case $# in
0)
echo "*** Creating a new release. Automatically calculating version number"
;;
1)
if [ $1 == "-h" ] || [ $1 == "--help" ]
then
echo "Usage: $0 [version-number]"
echo " Creates a release, commits the changes to this repository using tags and uploads "
echo " the release to Github Releases and the final Docker image to Docker Hub. "
echo " It also adds comments based on previous git commits."
exit 0
else
VERSION=$1
if ! [[ $VERSION =~ $RE_VERSION_NUMBER ]]
then
echo "ERROR: Incorrect version number!"
exit 1
fi
echo "*** Creating a new release. Using release version number $VERSION."
fi
;;
*)
echo "ERROR: incorrect argument. See '$0 --help'"
exit 1
;;
esac
# Create a personal github token to use this script
if [ -z "$GITHUB_TOKEN" ]
then
echo "Github TOKEN not defined!"
echo "See https://help.github.com/articles/creating-an-access-token-for-command-line-use/"
exit 1
fi
# You need bosh installed and with you credentials
if ! [ -x "$(command -v $DOCKER)" ]
then
echo "ERROR: $DOCKER command not found! Please install it and make it available in the PATH"
exit 1
fi
# You need jq installed
if ! [ -x "$(command -v $JQ)" ]
then
echo "ERROR: $JQ command not found! Please install it and make it available in the PATH"
exit 1
fi
DOCKER_USER=$(docker info 2> /dev/null | sed -ne 's/Username: \(.*\)/\1/p')
if [ -z "$DOCKER_USER" ]
then
echo "ERROR: Not logged in Docker Hub!"
echo "Please perform 'docker login' with your credentials in order to push images there."
exit 1
fi
# Creating the release
if [ -z "$VERSION" ]
then
VERSION=$(sed -ne 's/^ARG.* VERSION=\(.*\)/\1/p' docker/Dockerfile)
MYVERSION=$(sed -ne 's/^ARG.* MYVERSION=\(.*\)/\1/p' docker/Dockerfile)
[ -n "$MYVERSION" ] && VERSION="$VERSION-$MYVERSION"
echo "* Creating final release version $VERSION (from Dockerfile) ..."
else
echo "* Creating final release version $VERSION (from input)..."
fi
# Get the last git commit made by this script
LASTCOMMIT=$(git show-ref --tags -d | tail -n 1)
if [ -z "$LASTCOMMIT" ]
then
echo "* Changes since the beginning: "
CHANGELOG=$(git log --pretty="%h %aI %s (%an)" | sed 's/^/- /')
else
echo "* Changes since last version with commit $LASTCOMMIT: "
CHANGELOG=$(git log --pretty="%h %aI %s (%an)" "$(echo $LASTCOMMIT | cut -d' ' -f 1)..@" | sed 's/^/- /')
fi
if [ -z "$CHANGELOG" ]
then
echo "ERROR: no commits since last release with commit $LASTCOMMIT!. Please "
echo "commit your changes to create and publish a new release!"
exit 1
fi
echo "$CHANGELOG"
pushd docker
echo "* Building Docker image with tag $NAME:$VERSION ..."
$DOCKER build \
--build-arg ARCH=${ARCH} \
--build-arg TZ=$(timedatectl | awk '/Time zone:/{ print $3 }') \
. -t $NAME
$DOCKER tag $NAME $DOCKER_TAG
# Uploading docker image
echo "* Pusing Docker image to Docker Hub ..."
$DOCKER push $DOCKER_TAG
$DOCKER tag $NAME $DOCKER_TAG:$VERSION
$DOCKER push $DOCKER_TAG
$DOCKER save -o "/tmp/$NAME-$VERSION.tgz" $DOCKER_TAG:$VERSION
popd
# Create annotated tag
echo "* Creating a git tag ... "
git tag -a "v$VERSION" -m "$RELEASE v$VERSION"
git push --tags
# Create a release in Github
echo "* Creating a new release in Github ... "
DESC=$(cat <<EOF
# $RELEASE version $VERSION
$DESCRIPTION
## Changes since last version
$CHANGELOG
## Using it
Given the docker image with name 'jriguera/openzwave2mqtt':
docker run --privileged --name openzwave -e TZ=Europe/Amsterdam -v /dev/ttyUSB0:/dev/ttyUSB0 -v $(pwd)/config:/config -e MQTT_SERVER="127.0.0.1" -e MQTT_USER=ozw -e MQTT_PASS="pass" -d jriguera/openzwave2mqtt
EOF
)
printf -v data '{"tag_name": "v%s","target_commitish": "master","name": "v%s","body": %s,"draft": false, "prerelease": false}' "$VERSION" "$VERSION" "$(echo "$DESC" | $JQ -R -s '@text')"
releaseid=$($CURL -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -XPOST --data "$data" "https://api.github.com/repos/$GITHUB_REPO/releases" | $JQ '.id')
# Upload the release
echo "* Uploading image to Github releases section ... "
echo -n " URL: "
$CURL -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/octet-stream" --data-binary @"/tmp/$NAME-$VERSION.tgz" "https://uploads.github.com/repos/$GITHUB_REPO/releases/$releaseid/assets?name=$NAME-$VERSION.tgz" | $JQ -r '.browser_download_url'
echo
echo "*** Description https://github.com/$GITHUB_REPO/releases/tag/v$VERSION: "
echo
echo "$DESC"
# Delete the release
rm -f "/tmp/$NAME-$VERSION.tgz"
git fetch --tags
exit 0