build-macos.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. if [[ "$(uname)" != "Darwin" ]] ; then
  3. echo "This should be run on macOS"
  4. exit 1
  5. fi
  6. go version
  7. export GO111MODULE=on
  8. # build 'cloudflared-darwin-amd64.tgz'
  9. mkdir -p artifacts
  10. FILENAME="$(pwd)/artifacts/cloudflared-darwin-amd64.tgz"
  11. PKGNAME="$(pwd)/artifacts/cloudflared-amd64.pkg"
  12. TARGET_DIRECTORY=".build"
  13. BINARY_NAME="cloudflared"
  14. VERSION=$(git describe --tags --always --dirty="-dev")
  15. PRODUCT="cloudflared"
  16. CODE_SIGN_PRIV="code_sign.p12"
  17. CODE_SIGN_CERT="code_sign.cer"
  18. INSTALLER_PRIV="installer.p12"
  19. INSTALLER_CERT="installer.cer"
  20. BUNDLE_ID="com.cloudflare.cloudflared"
  21. SEC_DUP_MSG="security: SecKeychainItemImport: The specified item already exists in the keychain."
  22. export PATH="$PATH:/usr/local/bin"
  23. mkdir -p ../src/github.com/cloudflare/
  24. cp -r . ../src/github.com/cloudflare/cloudflared
  25. cd ../src/github.com/cloudflare/cloudflared
  26. GOCACHE="$PWD/../../../../" GOPATH="$PWD/../../../../" CGO_ENABLED=1 make cloudflared
  27. # Add code signing private key to the key chain
  28. if [[ ! -z "$CFD_CODE_SIGN_KEY" ]]; then
  29. if [[ ! -z "$CFD_CODE_SIGN_PASS" ]]; then
  30. # write private key to disk and then import it keychain
  31. echo -n -e ${CFD_CODE_SIGN_KEY} | base64 -D > ${CODE_SIGN_PRIV}
  32. out=$(security import ${CODE_SIGN_PRIV} -A -P "${CFD_CODE_SIGN_PASS}" 2>&1)
  33. exitcode=$?
  34. if [ -n "$out" ]; then
  35. if [ $exitcode -eq 0 ]; then
  36. echo "$out"
  37. else
  38. if [ "$out" != "${SEC_DUP_MSG}" ]; then
  39. echo "$out" >&2
  40. exit $exitcode
  41. fi
  42. fi
  43. fi
  44. rm ${CODE_SIGN_PRIV}
  45. fi
  46. fi
  47. # Add code signing certificate to the key chain
  48. if [[ ! -z "$CFD_CODE_SIGN_CERT" ]]; then
  49. # write certificate to disk and then import it keychain
  50. echo -n -e ${CFD_CODE_SIGN_CERT} | base64 -D > ${CODE_SIGN_CERT}
  51. out1=$(security import ${CODE_SIGN_CERT} -A 2>&1)
  52. exitcode1=$?
  53. if [ -n "$out1" ]; then
  54. if [ $exitcode1 -eq 0 ]; then
  55. echo "$out1"
  56. else
  57. if [ "$out1" != "${SEC_DUP_MSG}" ]; then
  58. echo "$out1" >&2
  59. exit $exitcode1
  60. else
  61. echo "already imported code signing certificate"
  62. fi
  63. fi
  64. fi
  65. rm ${CODE_SIGN_CERT}
  66. fi
  67. # Add package signing private key to the key chain
  68. if [[ ! -z "$CFD_INSTALLER_KEY" ]]; then
  69. if [[ ! -z "$CFD_INSTALLER_PASS" ]]; then
  70. # write private key to disk and then import it into the keychain
  71. echo -n -e ${CFD_INSTALLER_KEY} | base64 -D > ${INSTALLER_PRIV}
  72. out2=$(security import ${INSTALLER_PRIV} -A -P "${CFD_INSTALLER_PASS}" 2>&1)
  73. exitcode2=$?
  74. if [ -n "$out2" ]; then
  75. if [ $exitcode2 -eq 0 ]; then
  76. echo "$out2"
  77. else
  78. if [ "$out2" != "${SEC_DUP_MSG}" ]; then
  79. echo "$out2" >&2
  80. exit $exitcode2
  81. fi
  82. fi
  83. fi
  84. rm ${INSTALLER_PRIV}
  85. fi
  86. fi
  87. # Add package signing certificate to the key chain
  88. if [[ ! -z "$CFD_INSTALLER_CERT" ]]; then
  89. # write certificate to disk and then import it keychain
  90. echo -n -e ${CFD_INSTALLER_CERT} | base64 -D > ${INSTALLER_CERT}
  91. out3=$(security import ${INSTALLER_CERT} -A 2>&1)
  92. exitcode3=$?
  93. if [ -n "$out3" ]; then
  94. if [ $exitcode3 -eq 0 ]; then
  95. echo "$out3"
  96. else
  97. if [ "$out3" != "${SEC_DUP_MSG}" ]; then
  98. echo "$out3" >&2
  99. exit $exitcode3
  100. else
  101. echo "already imported installer certificate"
  102. fi
  103. fi
  104. fi
  105. rm ${INSTALLER_CERT}
  106. fi
  107. # get the code signing certificate name
  108. if [[ ! -z "$CFD_CODE_SIGN_NAME" ]]; then
  109. CODE_SIGN_NAME="${CFD_CODE_SIGN_NAME}"
  110. else
  111. if [[ -n "$(security find-certificate -c "Developer ID Application" | cut -d'"' -f 4 -s | grep "Developer ID Application:" | head -1)" ]]; then
  112. CODE_SIGN_NAME=$(security find-certificate -c "Developer ID Application" | cut -d'"' -f 4 -s | grep "Developer ID Application:" | head -1)
  113. else
  114. CODE_SIGN_NAME=""
  115. fi
  116. fi
  117. # get the package signing certificate name
  118. if [[ ! -z "$CFD_INSTALLER_NAME" ]]; then
  119. PKG_SIGN_NAME="${CFD_INSTALLER_NAME}"
  120. else
  121. if [[ -n "$(security find-certificate -c "Developer ID Installer" | cut -d'"' -f 4 -s | grep "Developer ID Installer:" | head -1)" ]]; then
  122. PKG_SIGN_NAME=$(security find-certificate -c "Developer ID Installer" | cut -d'"' -f 4 -s | grep "Developer ID Installer:" | head -1)
  123. else
  124. PKG_SIGN_NAME=""
  125. fi
  126. fi
  127. # sign the cloudflared binary
  128. if [[ ! -z "$CODE_SIGN_NAME" ]]; then
  129. codesign -s "${CODE_SIGN_NAME}" -f -v --timestamp --options runtime ${BINARY_NAME}
  130. # notarize the binary
  131. if [[ ! -z "$CFD_NOTE_PASSWORD" ]]; then
  132. zip "${BINARY_NAME}.zip" ${BINARY_NAME}
  133. xcrun altool --notarize-app -f "${BINARY_NAME}.zip" -t osx -u ${CFD_NOTE_USERNAME} -p ${CFD_NOTE_PASSWORD} --primary-bundle-id ${BUNDLE_ID}
  134. fi
  135. fi
  136. # creating build directory
  137. rm -rf $TARGET_DIRECTORY
  138. mkdir "${TARGET_DIRECTORY}"
  139. mkdir "${TARGET_DIRECTORY}/contents"
  140. cp -r ".mac_resources/scripts" "${TARGET_DIRECTORY}/scripts"
  141. # copy cloudflared into the build directory
  142. cp ${BINARY_NAME} "${TARGET_DIRECTORY}/contents/${PRODUCT}"
  143. # compress cloudflared into a tar and gzipped file
  144. tar czf "$FILENAME" "${BINARY_NAME}"
  145. # build the installer package
  146. if [[ ! -z "$PKG_SIGN_NAME" ]]; then
  147. pkgbuild --identifier com.cloudflare.${PRODUCT} \
  148. --version ${VERSION} \
  149. --scripts ${TARGET_DIRECTORY}/scripts \
  150. --root ${TARGET_DIRECTORY}/contents \
  151. --install-location /usr/local/bin \
  152. --sign "${PKG_SIGN_NAME}" \
  153. ${PKGNAME}
  154. # notarize the package
  155. if [[ ! -z "$CFD_NOTE_PASSWORD" ]]; then
  156. xcrun altool --notarize-app -f ${PKGNAME} -t osx -u ${CFD_NOTE_USERNAME} -p ${CFD_NOTE_PASSWORD} --primary-bundle-id ${BUNDLE_ID}
  157. xcrun stapler staple ${PKGNAME}
  158. fi
  159. else
  160. pkgbuild --identifier com.cloudflare.${PRODUCT} \
  161. --version ${VERSION} \
  162. --scripts ${TARGET_DIRECTORY}/scripts \
  163. --root ${TARGET_DIRECTORY}/contents \
  164. --install-location /usr/local/bin \
  165. ${PKGNAME}
  166. fi
  167. # cleaning up the build directory
  168. rm -rf $TARGET_DIRECTORY