-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.sh
executable file
·199 lines (169 loc) · 5.37 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
#
# This script will be used for creating tags and releases for the dj-wasabi related repositories
# in an automated and (hopefully) structured way. :)
set -euo pipefail
function help() {
echo -e "This script will either provide the last created tag."
echo -e "or will create a new tag and push this to Github."
echo -e "\n\t-c <tag>\tCreate a tag named <tag> and pushes this"
echo -e "\t\t\tinformation to Github and creates a release."
echo -e "\t-d\t\tWill generate CHANGELOG.md."
echo -e "\t-h\t\tWill print this help message."
echo -e "\t-l\t\tWill print last created tag."
echo -e "\nNote:"
echo -e "\tPlease make sure that Docker is running and the environment"
echo -e "\tvariable \"CHANGELOG_GITHUB_TOKEN\" is set with correct value."
exit 1
}
function getLatestTag() {
# Print the latest created git tag.
git describe --abbrev=0
}
function verifyGitTag(){
# Verify if the provided tag locally exist (1) or not (0).
local TAG=$1
git tag | grep -c "^${TAG}$" || true
}
function createRelease(){
# The "main" function to start creating everything for a single release.
local VERSION=$1
local TAG_ALREAD_EXIST
TAG_ALREAD_EXIST=$(verifyGitTag "${VERSION}")
if [[ $TAG_ALREAD_EXIST -eq 1 ]]
then echo "ERROR - Release already exist"
exit 1
fi
pullGit
fetchGit
createGitTag "${VERSION}"
pushGitTag
createGithubRelease "${VERSION}"
createGitContributors "${VERSION}"
updateChangelogMd "${VERSION}"
pushGit
}
function fetchGit(){
echo "INFO - Prune remote-tracking references that no longer exist on the remote."
git fetch -p > /dev/null 2>&1
}
function pullGit(){
# Do a git pull
echo "INFO - Pulling changes from Github."
git pull origin "$(git rev-parse --abbrev-ref HEAD)" > /dev/null 2>&1
}
function pushGit() {
# Push the changes to Github.
echo "INFO - Pushing changes to Github."
git push > /dev/null 2>&1
}
function pushGitTag() {
# Push the just created git tag.
echo "INFO - Push tag to Github."
git push --tags > /dev/null 2>&1
}
function createGitContributors() {
# Create/Update and commit file with git contributors.
local VERSION=$1
if [[ ! -f CONTRIBUTORS ]]
then touch CONTRIBUTORS
fi
git shortlog -s -n --all --no-merges | awk '{$1=""}1' | sort -u > CONTRIBUTORS
if [[ $(git status | grep -c 'CONTRIBUTORS' || true) -gt 0 ]]
then echo "INFO - Updating CONTRIBUTORS file"
git add CONTRIBUTORS
git commit -m "Updating CONTRIBUTORS file for release ${VERSION}" CONTRIBUTORS
fi
}
function updateChangelogMd() {
# Update the CHANGELOG.md by running a generator command via Docker.
local VERSION=${1:-null}
echo "INFO - Writing CHANGELOG.md file."
docker run --rm -e CHANGELOG_GITHUB_TOKEN="${GITHUB_TOKEN}" -v "$(pwd)":/usr/local/src/your-app ferrarimarco/github-changelog-generator -u "${GITHUB_USER}" -p "${GITHUB_PROJECT}" > /dev/null 2>&1
if [[ "${VERSION}" != "null" ]];then
if [[ $(git status | grep -c 'CHANGELOG.md' || true) -gt 0 ]]
then echo "INFO - Updating CHANGELOG.md file"
git add CHANGELOG.md
git commit -m "Updating CHANGELOG.md file for release ${VERSION}" CHANGELOG.md
fi
fi
}
function createGitTag() {
# Create a git tag locally.
local VERSION=$1
echo "INFO - Create the tag \"${VERSION}\" locally."
git tag -a "${VERSION}" -m "${VERSION}"
}
function generateGithubReleaseData() {
# Generate json data to be POST'ed to Github.
local VERSION=$1
cat <<EOF
{
"tag_name": "${VERSION}",
"target_commitish": "$(git rev-parse --abbrev-ref HEAD)",
"name": "${VERSION}",
"body": "",
"draft": false,
"prerelease": false
}
EOF
}
function createGithubRelease() {
# Create release in Github.
local VERSION=$1
local JSON_DATA
JSON_DATA=$(generateGithubReleaseData "${VERSION}")
echo "INFO - Create release on Github"
curl -s -H "Authorization: token ${GITHUB_TOKEN}" --data "${JSON_DATA}" "https://api.github.com/repos/${GITHUB_USER}/${GITHUB_PROJECT}/releases" > /dev/null
}
function getGithubUser() {
# Find username
local GITHUB_URL=$1
if [[ $(echo "${GITHUB_URL}" | grep -c '^https:' ) -eq 1 ]]
then GITHUB_USER=$(echo "${GITHUB_URL}" | awk -F '/' '{print $4}')
else GITHUB_USER=$(echo "${GITHUB_URL}" | awk -F ':' '{print $2}' | awk -F '/' '{print $1}')
fi
echo "${GITHUB_USER}"
}
# Some checks we need to do to make sure we don't run into errors.
# We need at an argument, otherwise we just print help and stop working.
if [[ $# -eq 0 ]]
then help
fi
# Validate if we have a "CHANGELOG_GITHUB_TOKEN" environment variable already in our current env
if [[ -z $CHANGELOG_GITHUB_TOKEN ]]
then echo "ERROR - We don't have the environment \"CHANGELOG_GITHUB_TOKEN\" set."
exit 1
fi
# Verify that Docker is runnig.
if [[ $(docker ps > /dev/null 2>&1;echo $?) -ne 0 ]]
then echo "ERROR - Docker is not running"
exit 1
fi
# Get GIT related information
GITHUB_URL=$(git config --get remote.origin.url)
GITHUB_USER=$(getGithubUser "${GITHUB_URL}")
GITHUB_PROJECT=$(echo "${GITHUB_URL}" | xargs basename | sed 's/.git//g')
GITHUB_TOKEN="${CHANGELOG_GITHUB_TOKEN}"
while getopts 'c:dlh' OPTION; do
case "$OPTION" in
c)
createRelease "${OPTARG}"
;;
d)
updateChangelogMd
;;
l)
getLatestTag
;;
h)
help
exit 0
;;
?)
help
exit 1
;;
esac
done
shift "$(( OPTIND - 1))"