forked from fabianonline/telegram_backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·93 lines (64 loc) · 3.39 KB
/
deploy.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
#!/bin/bash
error() {
echo "Error: $1"
exit 1
}
[ -z "$1" ] && error "Parameter's missing. Expecting version number like '1.2.3' as first and only parameter."
if [ "$1" == "--help" ]; then
echo "Usage: `basename "$0"` 1.2.3"
exit 1
fi
release_notes="$(cat release_notes.txt 2>/dev/null)"
[ -z "$release_notes" ] && error "release_notes.txt is empty"
VERSION="$1"
source "deploy.secret.sh"
[ -z "$BOT_TOKEN" ] && error "BOT_TOKEN is not set or empty."
[ -z "$CHAT_ID" ] && error "CHAT_ID is not set or empty."
[ -z "$TOKEN" ] && error "TOKEN is not set or empty."
CURL_OPTS="-u fabianonline:$TOKEN"
git diff-files --quiet --ignore-submodules -- || error "You have changes in your working tree."
git diff-index --cached --quiet HEAD --ignore-submodules -- || error "You have uncommited changes."
branch_name=$(git symbolic-ref HEAD 2>/dev/null)
branch_name=${branch_name##refs/heads/}
[ "$branch_name" == "master" ] || error "Current branch is $branch_name, not master."
echo "Updating the Dockerfile..."
sed -i "s/ENV JAR_VERSION .\+/ENV JAR_VERSION $VERSION/g" Dockerfile || error "Couldn't modify Dockerfile."
echo "Committing the new Dockerfile..."
git commit -m "Bumping the version to $VERSION" Dockerfile
echo "Tagging the new version..."
git tag -a "$VERSION" -m "Version $VERSION" || error
echo "Building it..."
gradle build || error "Build failed. What did you do?!"
echo "Getting git stats..."
git_stats=$(git diff --shortstat stable..)
echo "Checking out stable..."
git checkout stable || error
echo "Merging master into stable..."
git merge --no-ff -m "Merging master into stable for version $VERSION" master || error
echo "Checking out master again..."
git checkout master || error
echo "Pushing all to Github..."
git push --all || error
echo "Pushing tags to Github..."
git push --tags || error
echo "Generating a release on Github..."
json=$(ruby -e "require 'json'; puts({tag_name: '$VERSION', name: '$VERSION', body: \$stdin.read}.to_json)" <<< "$release_notes") || error "Couldn't generate JSON for Github"
json=$(curl $CURL_OPTS https://api.github.com/repos/fabianonline/telegram_backup/releases -XPOST -d "$json") || error "Github failure"
echo "Uploading telegram_backup.jar to Github..."
upload_url=$(jq -r ".upload_url" <<< "$json") || error "Could not parse JSON from Github"
upload_url=$(sed 's/{.*}//' <<< "$upload_url")
release_url=$(jq -r ".html_url" <<< "$json") || error "Could not parse JSON from Github"
curl $CURL_OPTS --header "Content-Type: application/zip" "${upload_url}?name=telegram_backup.jar" --upload-file build/libs/telegram_backup.jar || error "Asset upload to github failed"
echo "Building the docker image..."
docker build -t fabianonline/telegram_backup:$VERSION -t fabianonline/telegram_backup:latest - < Dockerfile
echo "Pushing the docker image..."
docker push fabianonline/telegram_backup
echo "Notifying the Telegram group..."
release_notes=$(sed 's/\* /• /' | sed 's/&/&/g' | sed 's/</\</g' | sed 's/>/\>/g' <<< "$release_notes")
message="<b>Version $VERSION was just released</b>"$'\n'"$git_stats"$'\n'$'\n'"$release_notes"$'\n'$'\n'"$release_url"
curl https://api.telegram.org/bot${BOT_TOKEN}/sendMessage -XPOST --form "text=<-" --form-string "chat_id=${CHAT_ID}" --form-string "parse_mode=HTML" --form-string "disable_web_page_preview=true" <<< "$message"
echo "Cleaning release_notes.txt..."
> release_notes.txt
echo "Checking out master..."
git checkout master
echo "Done."