build-binary 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env bash
  2. set -e
  3. VER=$1
  4. PROJ="etcd"
  5. if [ -z "$1" ]; then
  6. echo "Usage: ${0} VERSION" >> /dev/stderr
  7. exit 255
  8. fi
  9. set -u
  10. function setup_env {
  11. local proj=${1}
  12. local ver=${2}
  13. if [ ! -d "${proj}" ]; then
  14. git clone https://github.com/etcd-io/"${proj}"
  15. fi
  16. pushd "${proj}" >/dev/null
  17. git checkout master
  18. git fetch --all
  19. git reset --hard origin/master
  20. git checkout "${ver}"
  21. popd >/dev/null
  22. }
  23. function package {
  24. local target=${1}
  25. local srcdir="${2}/bin"
  26. local ccdir="${srcdir}/${GOOS}_${GOARCH}"
  27. if [ -d "${ccdir}" ]; then
  28. srcdir="${ccdir}"
  29. fi
  30. local ext=""
  31. if [ "${GOOS}" == "windows" ]; then
  32. ext=".exe"
  33. fi
  34. for bin in etcd etcdctl; do
  35. cp "${srcdir}/${bin}" "${target}/${bin}${ext}"
  36. done
  37. cp etcd/README.md "${target}"/README.md
  38. cp etcd/etcdctl/README.md "${target}"/README-etcdctl.md
  39. cp etcd/etcdctl/READMEv2.md "${target}"/READMEv2-etcdctl.md
  40. cp -R etcd/Documentation "${target}"/Documentation
  41. }
  42. function main {
  43. mkdir release
  44. cd release
  45. setup_env "${PROJ}" "${VER}"
  46. tarcmd=tar
  47. if [[ $(go env GOOS) == "darwin" ]]; then
  48. echo "Please use linux machine for release builds."
  49. exit 1
  50. fi
  51. for os in darwin windows linux; do
  52. export GOOS=${os}
  53. TARGET_ARCHS=("amd64")
  54. if [ ${GOOS} == "linux" ]; then
  55. TARGET_ARCHS+=("arm64")
  56. TARGET_ARCHS+=("ppc64le")
  57. fi
  58. for TARGET_ARCH in "${TARGET_ARCHS[@]}"; do
  59. export GOARCH=${TARGET_ARCH}
  60. pushd etcd >/dev/null
  61. GO_LDFLAGS="-s" ./build
  62. popd >/dev/null
  63. TARGET="etcd-${VER}-${GOOS}-${GOARCH}"
  64. mkdir "${TARGET}"
  65. package "${TARGET}" "${PROJ}"
  66. if [ ${GOOS} == "linux" ]; then
  67. ${tarcmd} cfz "${TARGET}.tar.gz" "${TARGET}"
  68. echo "Wrote release/${TARGET}.tar.gz"
  69. else
  70. zip -qr "${TARGET}.zip" "${TARGET}"
  71. echo "Wrote release/${TARGET}.zip"
  72. fi
  73. done
  74. done
  75. }
  76. main