-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·225 lines (191 loc) · 5.17 KB
/
Copy pathrelease.sh
File metadata and controls
executable file
·225 lines (191 loc) · 5.17 KB
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
#!/usr/bin/env bash
set -euo pipefail
if [ "${DEBUG:-false}" = "true" ]; then
set -x
fi
VERSION=""
LOCAL_BUILD=false
SKIP_BUILD=false
INSTALL_TARGETS=true
DIST_DIR="${DIST_DIR:-dist}"
TARGETS=(
"x86_64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
)
APPS=(
"demo-customer-profile-api:apps/demo-customer-profile-api"
"demo-insurance-claim-mcp-server:apps/demo-insurance-claim-mcp-server"
"demo-offer-decision-api:apps/demo-offer-decision-api"
)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="${SCRIPT_DIR}"
show_help() {
local error="${1:-}"
echo " "
if [[ -n "$error" ]]; then
echo "Error: ${error}"
echo " "
fi
echo " release.sh [VERSION] [-l|--local] [--skip-build] [--no-target-add] [--dist DIR]"
echo " "
echo " where [VERSION] is the GitHub release tag to build and publish"
echo " [-l|--local] builds and packages locally without uploading to GitHub"
echo " [--skip-build] packages existing target binaries without rebuilding"
echo " [--no-target-add] skips rustup target installation"
echo " [--dist DIR] writes release archives to DIR instead of dist"
echo " "
echo " targets:"
echo " x86_64-unknown-linux-gnu"
echo " x86_64-unknown-linux-musl"
echo " "
echo " examples:"
echo " ./release.sh v0.1.0"
echo " ./release.sh v0.1.0 --local"
echo " ./release.sh v0.1.0 --skip-build"
echo " "
}
fail() {
echo "Error: $*" >&2
exit 1
}
require_command() {
local command_name="$1"
command -v "$command_name" >/dev/null 2>&1 || fail "Missing required command: ${command_name}"
}
require_musl_toolchain() {
command -v x86_64-linux-musl-gcc >/dev/null 2>&1 && return 0
fail "Missing x86_64-linux-musl-gcc. Install the musl toolchain first, for example: sudo apt-get install -y musl-tools pkg-config"
}
setup_build_env() {
if command -v ccache >/dev/null 2>&1; then
export CCACHE_DIR="${CCACHE_DIR:-${REPO_ROOT}/target/ccache}"
mkdir -p "$CCACHE_DIR"
fi
}
build_target() {
local target="$1"
local cargo_args=()
for app_spec in "${APPS[@]}"; do
IFS=":" read -r app_name _app_dir <<<"${app_spec}"
cargo_args+=(--bin "$app_name")
done
if $INSTALL_TARGETS; then
rustup target add "$target"
fi
if [[ "$target" == "x86_64-unknown-linux-musl" ]]; then
require_musl_toolchain
fi
echo "Building light-example-rs apps for ${target}"
cargo build --locked --release --target "$target" "${cargo_args[@]}"
}
package_target() {
local target="$1"
local artifact="light-example-rs-${VERSION}-${target}"
local staging_dir="${DIST_DIR}/${artifact}"
local archive="${DIST_DIR}/${artifact}.tar.gz"
echo "Packaging ${archive}"
rm -rf -- "$staging_dir" "$archive"
mkdir -p "$staging_dir/bin" "$staging_dir/config"
for app_spec in "${APPS[@]}"; do
IFS=":" read -r app_name app_dir <<<"${app_spec}"
local binary="target/${target}/release/${app_name}"
[[ -x "$binary" ]] || fail "Missing release binary: ${binary}"
cp "$binary" "$staging_dir/bin/"
mkdir -p "$staging_dir/config/${app_name}"
cp -R "${app_dir}/config/." "$staging_dir/config/${app_name}/"
done
cp README.md LICENSE "$staging_dir/"
tar -C "$DIST_DIR" -czf "$archive" "$artifact"
ARCHIVES+=("$archive")
}
publish_release() {
require_command gh
if gh release view "$VERSION" >/dev/null 2>&1; then
echo "Uploading artifacts to existing GitHub release ${VERSION}"
gh release upload "$VERSION" "${ARCHIVES[@]}" --clobber
else
echo "Creating GitHub release ${VERSION}"
gh release create "$VERSION" "${ARCHIVES[@]}" \
--title "$VERSION" \
--notes "light-example-rs Linux release binaries"
fi
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-l|--local)
LOCAL_BUILD=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--no-target-add)
INSTALL_TARGETS=false
shift
;;
--dist)
[[ $# -ge 2 ]] || fail "--dist requires a directory"
DIST_DIR="$2"
shift 2
;;
-*)
show_help "Invalid option: $1"
exit 1
;;
*)
if [[ -z "$VERSION" ]]; then
VERSION="$1"
else
show_help "Invalid option: $1"
exit 1
fi
shift
;;
esac
done
if [[ -z "$VERSION" ]]; then
show_help "[VERSION] parameter is missing"
exit 1
fi
require_command cargo
require_command cp
require_command mkdir
require_command rm
require_command tar
if ! $LOCAL_BUILD; then
require_command gh
fi
if $INSTALL_TARGETS && ! $SKIP_BUILD; then
require_command rustup
fi
if ! $SKIP_BUILD; then
for target in "${TARGETS[@]}"; do
if [[ "$target" == "x86_64-unknown-linux-musl" ]]; then
require_musl_toolchain
fi
done
fi
cd "$REPO_ROOT"
mkdir -p "$DIST_DIR"
setup_build_env
declare -a ARCHIVES=()
for target in "${TARGETS[@]}"; do
if ! $SKIP_BUILD; then
build_target "$target"
fi
package_target "$target"
done
echo "Release archives:"
for archive in "${ARCHIVES[@]}"; do
echo " ${archive}"
done
if $LOCAL_BUILD; then
echo "Skipping GitHub release upload due to local build flag (-l or --local)"
else
publish_release
fi