You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

release.sh 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #!/usr/bin/env bash
  2. # Copyright © 2023 OpenIM. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ###############################################################################
  16. # Most of the ::release:: namespace functions have been moved to
  17. # github.com/openim/release. Have a look in that repo and specifically in
  18. # lib/releaselib.sh for ::release::-related functionality.
  19. ###############################################################################
  20. # example: ./coscli cp/sync -r /home/off-line/docker-off-line/ cos://openim-1306374445/openim/image/amd/off-line/off-line/ -e cos.ap-guangzhou.myqcloud.com
  21. # https://cloud.tencent.com/document/product/436/71763
  22. # Tencent cos configuration
  23. readonly BUCKET="openim-1306374445"
  24. readonly REGION="ap-beijing"
  25. readonly COS_RELEASE_DIR="openim-release"
  26. # default cos command tool coscli or coscmd
  27. readonly COSTOOL="coscli"
  28. # This is where the final release artifacts are created locally
  29. readonly RELEASE_STAGE="${LOCAL_OUTPUT_ROOT}/release-stage"
  30. readonly RELEASE_TARS="${LOCAL_OUTPUT_ROOT}/release-tars"
  31. readonly RELEASE_IMAGES="${LOCAL_OUTPUT_ROOT}/release-images"
  32. # OpenIM github account info
  33. readonly OPENIM_GITHUB_ORG=OpenIMSDK
  34. readonly OPENIM_GITHUB_REPO=Open-IM-Server
  35. readonly ARTIFACT=openim.tar.gz
  36. readonly CHECKSUM=${ARTIFACT}.sha1sum
  37. OPENIM_BUILD_CONFORMANCE=${OPENIM_BUILD_CONFORMANCE:-y}
  38. OPENIM_BUILD_PULL_LATEST_IMAGES=${OPENIM_BUILD_PULL_LATEST_IMAGES:-y}
  39. # Validate a ci version
  40. #
  41. # Globals:
  42. # None
  43. # Arguments:
  44. # version
  45. # Returns:
  46. # If version is a valid ci version
  47. # Sets: (e.g. for '1.2.3-alpha.4.56+abcdef12345678')
  48. # VERSION_MAJOR (e.g. '1')
  49. # VERSION_MINOR (e.g. '2')
  50. # VERSION_PATCH (e.g. '3')
  51. # VERSION_PRERELEASE (e.g. 'alpha')
  52. # VERSION_PRERELEASE_REV (e.g. '4')
  53. # VERSION_BUILD_INFO (e.g. '.56+abcdef12345678')
  54. # VERSION_COMMITS (e.g. '56')
  55. function openim::release::parse_and_validate_ci_version() {
  56. # Accept things like "v1.2.3-alpha.4.56+abcdef12345678" or "v1.2.3-beta.4"
  57. local -r version_regex="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)-([a-zA-Z0-9]+)\\.(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*)\\+[0-9a-f]{7,40})?$"
  58. local -r version="${1-}"
  59. [[ "${version}" =~ ${version_regex} ]] || {
  60. openim::log::error "Invalid ci version: '${version}', must match regex ${version_regex}"
  61. return 1
  62. }
  63. # The VERSION variables are used when this file is sourced, hence
  64. # the shellcheck SC2034 'appears unused' warning is to be ignored.
  65. # shellcheck disable=SC2034
  66. VERSION_MAJOR="${BASH_REMATCH[1]}"
  67. # shellcheck disable=SC2034
  68. VERSION_MINOR="${BASH_REMATCH[2]}"
  69. # shellcheck disable=SC2034
  70. VERSION_PATCH="${BASH_REMATCH[3]}"
  71. # shellcheck disable=SC2034
  72. VERSION_PRERELEASE="${BASH_REMATCH[4]}"
  73. # shellcheck disable=SC2034
  74. VERSION_PRERELEASE_REV="${BASH_REMATCH[5]}"
  75. # shellcheck disable=SC2034
  76. VERSION_BUILD_INFO="${BASH_REMATCH[6]}"
  77. # shellcheck disable=SC2034
  78. VERSION_COMMITS="${BASH_REMATCH[7]}"
  79. }
  80. # ---------------------------------------------------------------------------
  81. # Build final release artifacts
  82. function openim::release::clean_cruft() {
  83. # Clean out cruft
  84. find "${RELEASE_STAGE}" -name '*~' -exec rm {} \;
  85. find "${RELEASE_STAGE}" -name '#*#' -exec rm {} \;
  86. find "${RELEASE_STAGE}" -name '.DS*' -exec rm {} \;
  87. }
  88. function openim::release::package_tarballs() {
  89. # Clean out any old releases
  90. rm -rf "${RELEASE_STAGE}" "${RELEASE_TARS}" "${RELEASE_IMAGES}"
  91. mkdir -p "${RELEASE_TARS}"
  92. openim::release::package_src_tarball &
  93. openim::release::package_client_tarballs &
  94. openim::release::package_iam_manifests_tarball &
  95. openim::release::package_server_tarballs &
  96. openim::util::wait-for-jobs || { openim::log::error "previous tarball phase failed"; return 1; }
  97. openim::release::package_final_tarball & # _final depends on some of the previous phases
  98. openim::util::wait-for-jobs || { openim::log::error "previous tarball phase failed"; return 1; }
  99. }
  100. function openim::release::updload_tarballs() {
  101. openim::log::info "upload ${RELEASE_TARS}/* to cos bucket ${BUCKET}."
  102. for file in $(ls ${RELEASE_TARS}/*)
  103. do
  104. if [ "${COSTOOL}" == "coscli" ];then
  105. coscli cp "${file}" "cos://${BUCKET}/${COS_RELEASE_DIR}/${OPENIM_GIT_VERSION}/${file##*/}"
  106. coscli cp "${file}" "cos://${BUCKET}/${COS_RELEASE_DIR}/latest/${file##*/}"
  107. else
  108. coscmd upload "${file}" "${COS_RELEASE_DIR}/${OPENIM_GIT_VERSION}/"
  109. coscmd upload "${file}" "${COS_RELEASE_DIR}/latest/"
  110. fi
  111. done
  112. }
  113. # Package the source code we built, for compliance/licensing/audit/yadda.
  114. function openim::release::package_src_tarball() {
  115. local -r src_tarball="${RELEASE_TARS}/openim-src.tar.gz"
  116. openim::log::status "Building tarball: src"
  117. if [[ "${OPENIM_GIT_TREE_STATE-}" = 'clean' ]]; then
  118. git archive -o "${src_tarball}" HEAD
  119. else
  120. find "${OPENIM_ROOT}" -mindepth 1 -maxdepth 1 \
  121. ! \( \
  122. \( -path "${OPENIM_ROOT}"/_\* -o \
  123. -path "${OPENIM_ROOT}"/.git\* -o \
  124. -path "${OPENIM_ROOT}"/.gitignore\* -o \
  125. -path "${OPENIM_ROOT}"/.gsemver.yaml\* -o \
  126. -path "${OPENIM_ROOT}"/.config\* -o \
  127. -path "${OPENIM_ROOT}"/.chglog\* -o \
  128. -path "${OPENIM_ROOT}"/.gitlint -o \
  129. -path "${OPENIM_ROOT}"/.golangci.yaml -o \
  130. -path "${OPENIM_ROOT}"/.goreleaser.yml -o \
  131. -path "${OPENIM_ROOT}"/.note.md -o \
  132. -path "${OPENIM_ROOT}"/.todo.md \
  133. \) -prune \
  134. \) -print0 \
  135. | "${TAR}" czf "${src_tarball}" --transform "s|${OPENIM_ROOT#/*}|openim|" --null -T -
  136. fi
  137. }
  138. # Package up all of the server binaries
  139. function openim::release::package_server_tarballs() {
  140. # Find all of the built client binaries
  141. local long_platforms=("${LOCAL_OUTPUT_BINPATH}"/*/*)
  142. if [[ -n ${OPENIM_BUILD_PLATFORMS-} ]]; then
  143. read -ra long_platforms <<< "${OPENIM_BUILD_PLATFORMS}"
  144. fi
  145. for platform_long in "${long_platforms[@]}"; do
  146. local platform
  147. local platform_tag
  148. platform=${platform_long##${LOCAL_OUTPUT_BINPATH}/} # Strip LOCAL_OUTPUT_BINPATH
  149. platform_tag=${platform/\//-} # Replace a "/" for a "-"
  150. openim::log::status "Starting tarball: server $platform_tag"
  151. (
  152. local release_stage="${RELEASE_STAGE}/server/${platform_tag}/openim"
  153. rm -rf "${release_stage}"
  154. mkdir -p "${release_stage}/server/bin"
  155. local server_bins=("${OPENIM_SERVER_BINARIES[@]}")
  156. # This fancy expression will expand to prepend a path
  157. # (${LOCAL_OUTPUT_BINPATH}/${platform}/) to every item in the
  158. # server_bins array.
  159. cp "${server_bins[@]/#/${LOCAL_OUTPUT_BINPATH}/${platform}/}" \
  160. "${release_stage}/server/bin/"
  161. openim::release::clean_cruft
  162. local package_name="${RELEASE_TARS}/openim-server-${platform_tag}.tar.gz"
  163. openim::release::create_tarball "${package_name}" "${release_stage}/.."
  164. ) &
  165. done
  166. openim::log::status "Waiting on tarballs"
  167. openim::util::wait-for-jobs || { openim::log::error "server tarball creation failed"; exit 1; }
  168. }
  169. # Package up all of the cross compiled clients. Over time this should grow into
  170. # a full SDK
  171. function openim::release::package_client_tarballs() {
  172. # Find all of the built client binaries
  173. local long_platforms=("${LOCAL_OUTPUT_BINPATH}"/*/*)
  174. if [[ -n ${OPENIM_BUILD_PLATFORMS-} ]]; then
  175. read -ra long_platforms <<< "${OPENIM_BUILD_PLATFORMS}"
  176. fi
  177. for platform_long in "${long_platforms[@]}"; do
  178. local platform
  179. local platform_tag
  180. platform=${platform_long##${LOCAL_OUTPUT_BINPATH}/} # Strip LOCAL_OUTPUT_BINPATH
  181. platform_tag=${platform/\//-} # Replace a "/" for a "-"
  182. openim::log::status "Starting tarball: client $platform_tag"
  183. (
  184. local release_stage="${RELEASE_STAGE}/client/${platform_tag}/openim"
  185. rm -rf "${release_stage}"
  186. mkdir -p "${release_stage}/client/bin"
  187. local client_bins=("${OPENIM_CLIENT_BINARIES[@]}")
  188. # This fancy expression will expand to prepend a path
  189. # (${LOCAL_OUTPUT_BINPATH}/${platform}/) to every item in the
  190. # client_bins array.
  191. cp "${client_bins[@]/#/${LOCAL_OUTPUT_BINPATH}/${platform}/}" \
  192. "${release_stage}/client/bin/"
  193. openim::release::clean_cruft
  194. local package_name="${RELEASE_TARS}/openim-client-${platform_tag}.tar.gz"
  195. openim::release::create_tarball "${package_name}" "${release_stage}/.."
  196. ) &
  197. done
  198. openim::log::status "Waiting on tarballs"
  199. openim::util::wait-for-jobs || { openim::log::error "client tarball creation failed"; exit 1; }
  200. }
  201. # Package up all of the server binaries in docker images
  202. function openim::release::build_server_images() {
  203. # Clean out any old images
  204. rm -rf "${RELEASE_IMAGES}"
  205. local platform
  206. for platform in "${OPENIM_SERVER_PLATFORMS[@]}"; do
  207. local platform_tag
  208. local arch
  209. platform_tag=${platform/\//-} # Replace a "/" for a "-"
  210. arch=$(basename "${platform}")
  211. openim::log::status "Building images: $platform_tag"
  212. local release_stage
  213. release_stage="${RELEASE_STAGE}/server/${platform_tag}/openim"
  214. rm -rf "${release_stage}"
  215. mkdir -p "${release_stage}/server/bin"
  216. # This fancy expression will expand to prepend a path
  217. # (${LOCAL_OUTPUT_BINPATH}/${platform}/) to every item in the
  218. # OPENIM_SERVER_IMAGE_BINARIES array.
  219. cp "${OPENIM_SERVER_IMAGE_BINARIES[@]/#/${LOCAL_OUTPUT_BINPATH}/${platform}/}" \
  220. "${release_stage}/server/bin/"
  221. openim::release::create_docker_images_for_server "${release_stage}/server/bin" "${arch}"
  222. done
  223. }
  224. function openim::release::md5() {
  225. if which md5 >/dev/null 2>&1; then
  226. md5 -q "$1"
  227. else
  228. md5sum "$1" | awk '{ print $1 }'
  229. fi
  230. }
  231. function openim::release::sha1() {
  232. if which sha1sum >/dev/null 2>&1; then
  233. sha1sum "$1" | awk '{ print $1 }'
  234. else
  235. shasum -a1 "$1" | awk '{ print $1 }'
  236. fi
  237. }
  238. function openim::release::build_conformance_image() {
  239. local -r arch="$1"
  240. local -r registry="$2"
  241. local -r version="$3"
  242. local -r save_dir="${4-}"
  243. openim::log::status "Building conformance image for arch: ${arch}"
  244. ARCH="${arch}" REGISTRY="${registry}" VERSION="${version}" \
  245. make -C cluster/images/conformance/ build >/dev/null
  246. local conformance_tag
  247. conformance_tag="${registry}/conformance-${arch}:${version}"
  248. if [[ -n "${save_dir}" ]]; then
  249. "${DOCKER[@]}" save "${conformance_tag}" > "${save_dir}/conformance-${arch}.tar"
  250. fi
  251. openim::log::status "Deleting conformance image ${conformance_tag}"
  252. "${DOCKER[@]}" rmi "${conformance_tag}" &>/dev/null || true
  253. }
  254. # This builds all the release docker images (One docker image per binary)
  255. # Args:
  256. # $1 - binary_dir, the directory to save the tared images to.
  257. # $2 - arch, architecture for which we are building docker images.
  258. function openim::release::create_docker_images_for_server() {
  259. # Create a sub-shell so that we don't pollute the outer environment
  260. (
  261. local binary_dir
  262. local arch
  263. local binaries
  264. local images_dir
  265. binary_dir="$1"
  266. arch="$2"
  267. binaries=$(openim::build::get_docker_wrapped_binaries "${arch}")
  268. images_dir="${RELEASE_IMAGES}/${arch}"
  269. mkdir -p "${images_dir}"
  270. # k8s.gcr.io is the constant tag in the docker archives, this is also the default for config scripts in GKE.
  271. # We can use OPENIM_DOCKER_REGISTRY to include and extra registry in the docker archive.
  272. # If we use OPENIM_DOCKER_REGISTRY="k8s.gcr.io", then the extra tag (same) is ignored, see release_docker_image_tag below.
  273. local -r docker_registry="k8s.gcr.io"
  274. # Docker tags cannot contain '+'
  275. local docker_tag="${OPENIM_GIT_VERSION/+/_}"
  276. if [[ -z "${docker_tag}" ]]; then
  277. openim::log::error "git version information missing; cannot create Docker tag"
  278. return 1
  279. fi
  280. # provide `--pull` argument to `docker build` if `OPENIM_BUILD_PULL_LATEST_IMAGES`
  281. # is set to y or Y; otherwise try to build the image without forcefully
  282. # pulling the latest base image.
  283. local docker_build_opts
  284. docker_build_opts=
  285. if [[ "${OPENIM_BUILD_PULL_LATEST_IMAGES}" =~ [yY] ]]; then
  286. docker_build_opts='--pull'
  287. fi
  288. for wrappable in $binaries; do
  289. local binary_name=${wrappable%%,*}
  290. local base_image=${wrappable##*,}
  291. local binary_file_path="${binary_dir}/${binary_name}"
  292. local docker_build_path="${binary_file_path}.dockerbuild"
  293. local docker_file_path="${docker_build_path}/Dockerfile"
  294. local docker_image_tag="${docker_registry}/${binary_name}-${arch}:${docker_tag}"
  295. openim::log::status "Starting docker build for image: ${binary_name}-${arch}"
  296. (
  297. rm -rf "${docker_build_path}"
  298. mkdir -p "${docker_build_path}"
  299. ln "${binary_file_path}" "${docker_build_path}/${binary_name}"
  300. ln "${OPENIM_ROOT}/build/nsswitch.conf" "${docker_build_path}/nsswitch.conf"
  301. chmod 0644 "${docker_build_path}/nsswitch.conf"
  302. cat <<EOF > "${docker_file_path}"
  303. FROM ${base_image}
  304. COPY ${binary_name} /usr/local/bin/${binary_name}
  305. EOF
  306. # ensure /etc/nsswitch.conf exists so go's resolver respects /etc/hosts
  307. if [[ "${base_image}" =~ busybox ]]; then
  308. echo "COPY nsswitch.conf /etc/" >> "${docker_file_path}"
  309. fi
  310. "${DOCKER[@]}" build ${docker_build_opts:+"${docker_build_opts}"} -q -t "${docker_image_tag}" "${docker_build_path}" >/dev/null
  311. # If we are building an official/alpha/beta release we want to keep
  312. # docker images and tag them appropriately.
  313. local -r release_docker_image_tag="${OPENIM_DOCKER_REGISTRY-$docker_registry}/${binary_name}-${arch}:${OPENIM_DOCKER_IMAGE_TAG-$docker_tag}"
  314. if [[ "${release_docker_image_tag}" != "${docker_image_tag}" ]]; then
  315. openim::log::status "Tagging docker image ${docker_image_tag} as ${release_docker_image_tag}"
  316. "${DOCKER[@]}" rmi "${release_docker_image_tag}" 2>/dev/null || true
  317. "${DOCKER[@]}" tag "${docker_image_tag}" "${release_docker_image_tag}" 2>/dev/null
  318. fi
  319. "${DOCKER[@]}" save -o "${binary_file_path}.tar" "${docker_image_tag}" "${release_docker_image_tag}"
  320. echo "${docker_tag}" > "${binary_file_path}.docker_tag"
  321. rm -rf "${docker_build_path}"
  322. ln "${binary_file_path}.tar" "${images_dir}/"
  323. openim::log::status "Deleting docker image ${docker_image_tag}"
  324. "${DOCKER[@]}" rmi "${docker_image_tag}" &>/dev/null || true
  325. ) &
  326. done
  327. if [[ "${OPENIM_BUILD_CONFORMANCE}" =~ [yY] ]]; then
  328. openim::release::build_conformance_image "${arch}" "${docker_registry}" \
  329. "${docker_tag}" "${images_dir}" &
  330. fi
  331. openim::util::wait-for-jobs || { openim::log::error "previous Docker build failed"; return 1; }
  332. openim::log::status "Docker builds done"
  333. )
  334. }
  335. # This will pack openim-system manifests files for distros such as COS.
  336. function openim::release::package_iam_manifests_tarball() {
  337. openim::log::status "Building tarball: manifests"
  338. local src_dir="${OPENIM_ROOT}/deployments"
  339. local release_stage="${RELEASE_STAGE}/manifests/openim"
  340. rm -rf "${release_stage}"
  341. local dst_dir="${release_stage}"
  342. mkdir -p "${dst_dir}"
  343. cp -r ${src_dir}/* "${dst_dir}"
  344. #cp "${src_dir}/openim-apiserver.yaml" "${dst_dir}"
  345. #cp "${src_dir}/openim-authz-server.yaml" "${dst_dir}"
  346. #cp "${src_dir}/openim-pump.yaml" "${dst_dir}"
  347. #cp "${src_dir}/openim-watcher.yaml" "${dst_dir}"
  348. #cp "${OPENIM_ROOT}/cluster/gce/gci/health-monitor.sh" "${dst_dir}/health-monitor.sh"
  349. openim::release::clean_cruft
  350. local package_name="${RELEASE_TARS}/openim-manifests.tar.gz"
  351. openim::release::create_tarball "${package_name}" "${release_stage}/.."
  352. }
  353. # This is all the platform-independent stuff you need to run/install openim.
  354. # Arch-specific binaries will need to be downloaded separately (possibly by
  355. # using the bundled cluster/get-openim-binaries.sh script).
  356. # Included in this tarball:
  357. # - Cluster spin up/down scripts and configs for various cloud providers
  358. # - Tarballs for manifest configs that are ready to be uploaded
  359. # - Examples (which may or may not still work)
  360. # - The remnants of the docs/ directory
  361. function openim::release::package_final_tarball() {
  362. openim::log::status "Building tarball: final"
  363. # This isn't a "full" tarball anymore, but the release lib still expects
  364. # artifacts under "full/openim/"
  365. local release_stage="${RELEASE_STAGE}/full/openim"
  366. rm -rf "${release_stage}"
  367. mkdir -p "${release_stage}"
  368. mkdir -p "${release_stage}/client"
  369. cat <<EOF > "${release_stage}/client/README"
  370. Client binaries are no longer included in the OpenIM final tarball.
  371. Run release/get-openim-binaries.sh to download client and server binaries.
  372. EOF
  373. # We want everything in /scripts.
  374. mkdir -p "${release_stage}/release"
  375. cp -R "${OPENIM_ROOT}/scripts/release" "${release_stage}/"
  376. cat <<EOF > "${release_stage}/release/get-openim-binaries.sh"
  377. #!/usr/bin/env bash
  378. # Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
  379. # Use of this source code is governed by a MIT style
  380. # license that can be found in the LICENSE file.
  381. # This file download openim client and server binaries from tencent cos bucket.
  382. os=linux arch=amd64 version=${OPENIM_GIT_VERSION} && wget https://${BUCKET}.cos.${REGION}.myqcloud.com/${COS_RELEASE_DIR}/\$version/{openim-client-\$os-\$arch.tar.gz,openim-server-\$os-\$arch.tar.gz}
  383. EOF
  384. chmod +x ${release_stage}/release/get-openim-binaries.sh
  385. mkdir -p "${release_stage}/server"
  386. cp "${RELEASE_TARS}/openim-manifests.tar.gz" "${release_stage}/server/"
  387. cat <<EOF > "${release_stage}/server/README"
  388. Server binary tarballs are no longer included in the OpenIM final tarball.
  389. Run release/get-openim-binaries.sh to download client and server binaries.
  390. EOF
  391. # Include hack/lib as a dependency for the cluster/ scripts
  392. #mkdir -p "${release_stage}/hack"
  393. #cp -R "${OPENIM_ROOT}/hack/lib" "${release_stage}/hack/"
  394. cp -R ${OPENIM_ROOT}/{docs,configs,scripts,deployments,init,README.md,LICENSE} "${release_stage}/"
  395. echo "${OPENIM_GIT_VERSION}" > "${release_stage}/version"
  396. openim::release::clean_cruft
  397. local package_name="${RELEASE_TARS}/${ARTIFACT}"
  398. openim::release::create_tarball "${package_name}" "${release_stage}/.."
  399. }
  400. # Build a release tarball. $1 is the output tar name. $2 is the base directory
  401. # of the files to be packaged. This assumes that ${2}/iamis what is
  402. # being packaged.
  403. function openim::release::create_tarball() {
  404. openim::build::ensure_tar
  405. local tarfile=$1
  406. local stagingdir=$2
  407. "${TAR}" czf "${tarfile}" -C "${stagingdir}" openim --owner=0 --group=0
  408. }
  409. function openim::release::install_github_release(){
  410. GO111MODULE=on go install github.com/github-release/github-release@latest
  411. }
  412. # Require the following tools:
  413. # - github-release
  414. # - gsemver
  415. # - git-chglog
  416. # - coscmd or coscli
  417. function openim::release::verify_prereqs(){
  418. if [ -z "$(which github-release 2>/dev/null)" ]; then
  419. openim::log::info "'github-release' tool not installed, try to install it."
  420. if ! openim::release::install_github_release; then
  421. openim::log::error "failed to install 'github-release'"
  422. return 1
  423. fi
  424. fi
  425. if [ -z "$(which git-chglog 2>/dev/null)" ]; then
  426. openim::log::info "'git-chglog' tool not installed, try to install it."
  427. if ! go install github.com/git-chglog/git-chglog/cmd/git-chglog@latest &>/dev/null; then
  428. openim::log::error "failed to install 'git-chglog'"
  429. return 1
  430. fi
  431. fi
  432. if [ -z "$(which gsemver 2>/dev/null)" ]; then
  433. openim::log::info "'gsemver' tool not installed, try to install it."
  434. if ! go install github.com/arnaud-deprez/gsemver@latest &>/dev/null; then
  435. openim::log::error "failed to install 'gsemver'"
  436. return 1
  437. fi
  438. fi
  439. if [ -z "$(which ${COSTOOL} 2>/dev/null)" ]; then
  440. openim::log::info "${COSTOOL} tool not installed, try to install it."
  441. if ! make -C "${OPENIM_ROOT}" tools.install.${COSTOOL}; then
  442. openim::log::error "failed to install ${COSTOOL}"
  443. return 1
  444. fi
  445. fi
  446. if [ -z "${TENCENT_SECRET_ID}" -o -z "${TENCENT_SECRET_KEY}" ];then
  447. openim::log::error "can not find env: TENCENT_SECRET_ID and TENCENT_SECRET_KEY"
  448. return 1
  449. fi
  450. if [ "${COSTOOL}" == "coscli" ];then
  451. if [ ! -f "${HOME}/.cos.yaml" ];then
  452. cat << EOF > "${HOME}/.cos.yaml"
  453. cos:
  454. base:
  455. secretid: ${TENCENT_SECRET_ID}
  456. secretkey: ${TENCENT_SECRET_KEY}
  457. sessiontoken: ""
  458. buckets:
  459. - name: ${BUCKET}
  460. alias: ${BUCKET}
  461. region: ${REGION}
  462. EOF
  463. fi
  464. else
  465. if [ ! -f "${HOME}/.cos.conf" ];then
  466. cat << EOF > "${HOME}/.cos.conf"
  467. [common]
  468. secret_id = ${TENCENT_SECRET_ID}
  469. secret_key = ${TENCENT_SECRET_KEY}
  470. bucket = ${BUCKET}
  471. region =${REGION}
  472. max_thread = 5
  473. part_size = 1
  474. schema = https
  475. EOF
  476. fi
  477. fi
  478. }
  479. # Create a github release with specified tarballs.
  480. # NOTICE: Must export 'GITHUB_TOKEN' env in the shell, details:
  481. # https://github.com/github-release/github-release
  482. function openim::release::github_release() {
  483. # create a github release
  484. openim::log::info "create a new github release with tag ${OPENIM_GIT_VERSION}"
  485. github-release release \
  486. --user ${OPENIM_GITHUB_ORG} \
  487. --repo ${OPENIM_GITHUB_REPO} \
  488. --tag ${OPENIM_GIT_VERSION} \
  489. --description "" \
  490. --pre-release
  491. # update openim tarballs
  492. openim::log::info "upload ${ARTIFACT} to release ${OPENIM_GIT_VERSION}"
  493. github-release upload \
  494. --user ${OPENIM_GITHUB_ORG} \
  495. --repo ${OPENIM_GITHUB_REPO} \
  496. --tag ${OPENIM_GIT_VERSION} \
  497. --name ${ARTIFACT} \
  498. --file ${RELEASE_TARS}/${ARTIFACT}
  499. openim::log::info "upload openim-src.tar.gz to release ${OPENIM_GIT_VERSION}"
  500. github-release upload \
  501. --user ${OPENIM_GITHUB_ORG} \
  502. --repo ${OPENIM_GITHUB_REPO} \
  503. --tag ${OPENIM_GIT_VERSION} \
  504. --name "openim-src.tar.gz" \
  505. --file ${RELEASE_TARS}/openim-src.tar.gz
  506. }
  507. function openim::release::generate_changelog() {
  508. openim::log::info "generate CHANGELOG-${OPENIM_GIT_VERSION#v}.md and commit it"
  509. git-chglog ${OPENIM_GIT_VERSION} > ${OPENIM_ROOT}/CHANGELOG/CHANGELOG-${OPENIM_GIT_VERSION#v}.md
  510. set +o errexit
  511. git add ${OPENIM_ROOT}/CHANGELOG/CHANGELOG-${OPENIM_GIT_VERSION#v}.md
  512. git commit -a -m "docs(changelog): add CHANGELOG-${OPENIM_GIT_VERSION#v}.md"
  513. git push -f origin master # 最后将 CHANGELOG 也 push 上去
  514. }