-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.sh
executable file
·235 lines (197 loc) · 7.45 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env bash
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
publish=""
main() {
pushd "$ROOT" &> /dev/null
while getopts "hpw" opt; do
case $opt in
h) usage && exit 0;;
p) publish="true";;
w) review_web="true";;
\?) usage_error "Invalid option: -$OPTARG";;
esac
done
shift $((OPTIND-1))
verify_github_token
verify_gh
verify_skip
# We do not sign releases anymore because they are done in a Docker env now
# so some adaptation is required, probably GPG support
#verify_keybase
version="$1"; shift
# 'shift' can fail above, so we put bash in non failure mode after calling it
set -e
while true; do
if [[ "$version" == "" ]]; then
printf "What version do you want to release (current latest is `git describe --tags --abbrev=0`)? "
read version
fi
if [[ ! "$version" =~ ^v ]]; then
echo "Version $version is invalid, must start with a 'v'"
else
break
fi
done
echo "Pushing to ensure GitHub knowns about the latest commit(s)"
git push
echo ""
mode="Dry run, builds artifact and creates a GitHub release in draft mode, use -p flag to publish release right now"
if [[ "$publish" == "true" ]]; then
mode="Publishing"
fi
echo "About to release version $version ($mode)"
sleep 3
# We tag the version because goreleaser needs it to perform its work properly,
# but we delete it at the end of the script because we will let GitHub create
# the tag when the release is performed
git tag "$version"
trap "git tag -d $version > /dev/null" EXIT
## Substreams .spkg building
substreams pack -o "build/monitoring-smoke-test-$version.spkg"
echo ""
## Release Notes Generation
start_at=$(grep -n -m 1 -E '^## .+' CHANGELOG.md | cut -f 1 -d :)
changelod_trimmed=$(skip $start_at CHANGELOG.md | skip 1)
# It's important to work on trimmed content to determine end because `head -n$end_at`
# below is applied to trimmed content and thus line number found must be relative to it
end_at=$(printf "$changelod_trimmed" | grep -n -m 1 -E '^## .+' | cut -f 1 -d :)
printf "$changelod_trimmed" | head -n$end_at | skip -2 > .release_notes.md
args="--rm-dist --release-notes=.release_notes.md"
package_name="github.com/streamingfast/substreams-monitoring-smoke-test"
golang_cross_version="v1.19.4"
docker run \
--rm \
-e CGO_ENABLED=1 \
--env-file .env.release \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "`pwd`:/go/src/${package_name}" \
-w "/go/src/${package_name}" \
"goreleaser/goreleaser-cross:${golang_cross_version}" \
$args
echo "Release draft has been created succesfully, but it's not published"
echo "yet. You must now review the release and publish it if everything is"
echo "correct."
echo ""
echo "Showing you the release in the terminal..."
sleep 0.5
args=""
if [[ "$review_web" == "true" ]]; then
args="--web"
fi
gh release view "$version" $args
if [[ "$force" == "true" ]]; then
publish_now="yes"
else
echo ""
printf "Would you like to publish it right now? "
read publish_now
echo ""
fi
if [[ "$publish_now" == "Y" || "$publish_now" == "y" || "$publish_now" == "Yes" || "$publish_now" == "yes" ]]; then
gh release edit "$version" --draft=false
echo ""
echo "Release published at $(gh release view $version --json url -q '.url')"
else
echo "If something is wrong, you can delete the release from GitHub"
echo "and try again, delete the release by doing the command:"
echo ""
echo " gh release delete $version"
echo ""
echo "You can also publish it from the GitHub UI directly, on the release"
echo "page, press the small pencil button in the right corner to edit the release"
echo "and then press the 'Publish release' green button (scroll down to the bottom"
echo "of the page."
echo ""
printf "Do you want to open it right now? "
read answer
echo ""
if [[ "$answer" == "Y" || "$answer" == "y" || "$answer" == "Yes" || "$answer" == "yes" ]]; then
gh release view "$version" --web
fi
fi
}
verify_github_token() {
release_env_file="$ROOT/.env.release"
if [[ "$GITHUB_TOKEN" != "" && ! -f "$release_env_file" ]]; then
echo "GITHUB_TOKEN=${GITHUB_TOKEN}" > "$release_env_file"
fi
if [ ! -f "$ROOT/.env.release" ] || ! grep -q "GITHUB_TOKEN=gh" "$release_env_file"; then
echo "A '.env.release' file must be found at the root of the project and it must contain"
echo "definition of 'GITHUB_TOKEN' variable. You need to create this file locally and the"
echo "content should be:"
echo ""
echo "GITHUB_TOKEN=<your_github_token>"
echo ""
echo "You will need to create your own GitHub Token on GitHub website and make it available through"
echo "the file mentioned above."
if [[ -f "$ROOT/.env.release" ]]; then
echo ""
echo "Actual content of '$release_env_file' is:"
echo ""
cat "$release_env_file"
fi
exit 1
fi
}
verify_gh() {
if ! command -v gh &> /dev/null; then
echo "The GitHub CLI utility (https://cli.github.com/) is required to obtain"
echo "information about the current draft release."
echo ""
echo "Install via brew with 'brew install gh' or refer https://github.com/cli/cli#installation"
echo "otherwise."
echo ""
echo "Don't forget to activate link with GitHub by doing 'gh auth login'."
echo ""
exit 1
fi
}
verify_skip() {
if ! command -v skip &> /dev/null; then
echo "The 'skip' utility is required to generate the release notes from the"
echo "changelog file."
echo ""
echo "Install from source using 'go install github.com/streamingfast/tooling/cmd/skip@latest'"
echo ""
exit 1
fi
}
usage_error() {
message="$1"
exit_code="$2"
echo "ERROR: $message"
echo ""
usage
exit ${exit_code:-1}
}
usage() {
echo "usage: release.sh [-h] [-p] [-w] [<version>]"
echo ""
echo "Perform the necessary commands to perform a release of the project, builds"
echo "artifacts through Docker and publish a draft GitHub release by default."
echo "You can use '-p' to automatically publish the release instead of in draft mode."
echo ""
echo "The <version> is optional, if not provided, you'll be asked the question."
echo ""
echo "The release being performed against GitHub, you need a valid GitHub API token"
echo "with the necessary rights to upload release and push to repositories. It needs to"
echo "be provided in file named '.env.release' or through an environment"
echo "variable GITHUB_TOKEN (in which case the file is automtically created from it)."
echo ""
echo "The GitHub CLI utility (https://cli.github.com/) is required to obtain"
echo "information about the current draft release. Install via brew with "
echo "'brew install gh' or refer https://github.com/cli/cli#installation"
echo "otherwise."
echo ""
echo "StreamingFast 'skip' utility is required, you can use the command"
echo "'go install github.com/streamingfast/tooling/cmd/skip@latest' to install it"
echo ""
echo "You will need to have it available ('brew install keybase' on Mac OS X) and"
echo "configure it, just setting your Git username and a password should be enough."
echo ""
echo "Options"
echo " -p Forcing the GitHub release to be published right away instead of leaving it in draft mode"
echo " -w Review the draft release within the browser instead of through the CLI"
echo " -h Display help about this script"
}
main "$@"