update_version.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC1091
  3. set -e
  4. if [[ "${SHOULD_BUILD}" != "yes" && "${FORCE_UPDATE}" != "true" ]]; then
  5. echo "Will not update version JSON because we did not build"
  6. exit 0
  7. fi
  8. if [[ -z "${GH_TOKEN}" ]] && [[ -z "${GITHUB_TOKEN}" ]] && [[ -z "${GH_ENTERPRISE_TOKEN}" ]] && [[ -z "${GITHUB_ENTERPRISE_TOKEN}" ]]; then
  9. echo "Will not update version JSON because no GITHUB_TOKEN defined"
  10. exit 0
  11. else
  12. GITHUB_TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-${GH_ENTERPRISE_TOKEN:-${GITHUB_ENTERPRISE_TOKEN}}}}"
  13. fi
  14. # Support for GitHub Enterprise
  15. GH_HOST="${GH_HOST:-github.com}"
  16. if [[ "${FORCE_UPDATE}" == "true" ]]; then
  17. . version.sh
  18. fi
  19. if [[ -z "${BUILD_SOURCEVERSION}" ]]; then
  20. echo "Will not update version JSON because no BUILD_SOURCEVERSION defined"
  21. exit 0
  22. fi
  23. # if [[ "${VSCODE_ARCH}" == "ppc64le" ]] || [[ "${VSCODE_ARCH}" == "riscv64" ]] ; then
  24. # echo "Skip PPC64LE since only reh is published"
  25. # exit 0
  26. # fi
  27. # {
  28. # "url": "https://az764295.vo.msecnd.net/stable/51b0b28134d51361cf996d2f0a1c698247aeabd8/VSCode-darwin-stable.zip",
  29. # "name": "1.33.1",
  30. # "version": "51b0b28134d51361cf996d2f0a1c698247aeabd8",
  31. # "productVersion": "1.33.1",
  32. # "hash": "cb4109f196d23b9d1e8646ce43145c5bb62f55a8",
  33. # "timestamp": 1554971059007,
  34. # "sha256hash": "ac2a1c8772501732cd5ff539a04bb4dc566b58b8528609d2b34bbf970d08cf01"
  35. # }
  36. # `url` is URL_BASE + filename of asset e.g.
  37. # darwin: https://github.com/${ASSETS_REPOSITORY}/releases/download/${RELEASE_VERSION}/${APP_NAME}-darwin-${RELEASE_VERSION}.zip
  38. # `name` is $RELEASE_VERSION
  39. # `version` is $BUILD_SOURCEVERSION
  40. # `productVersion` is $RELEASE_VERSION
  41. # `hash` in <filename>.sha1
  42. # `timestamp` is $(node -e 'console.log(Date.now())')
  43. # `sha256hash` in <filename>.sha256
  44. REPOSITORY_NAME="${VERSIONS_REPOSITORY/*\//}"
  45. URL_BASE="https://${GH_HOST}/${ASSETS_REPOSITORY}/releases/download/${RELEASE_VERSION}"
  46. generateJson() {
  47. local url name version productVersion sha1hash sha256hash timestamp
  48. JSON_DATA="{}"
  49. # generate parts
  50. url="${URL_BASE}/${ASSET_NAME}"
  51. name="${RELEASE_VERSION}"
  52. version="${BUILD_SOURCEVERSION}"
  53. productVersion="$( transformVersion "${RELEASE_VERSION}" )"
  54. timestamp=$( node -e 'console.log(Date.now())' )
  55. if [[ ! -f "assets/${ASSET_NAME}" ]]; then
  56. echo "Downloading asset '${ASSET_NAME}'"
  57. gh release download --repo "${ASSETS_REPOSITORY}" "${RELEASE_VERSION}" --dir "assets" --pattern "${ASSET_NAME}*"
  58. fi
  59. sha1hash=$( awk '{ print $1 }' "assets/${ASSET_NAME}.sha1" )
  60. sha256hash=$( awk '{ print $1 }' "assets/${ASSET_NAME}.sha256" )
  61. # check that nothing is blank (blank indicates something awry with build)
  62. for key in url name version productVersion sha1hash timestamp sha256hash; do
  63. if [[ -z "${key}" ]]; then
  64. echo "Variable '${key}' is empty; exiting..."
  65. exit 1
  66. fi
  67. done
  68. # generate json
  69. JSON_DATA=$( jq \
  70. --arg url "${url}" \
  71. --arg name "${name}" \
  72. --arg version "${version}" \
  73. --arg productVersion "${productVersion}" \
  74. --arg hash "${sha1hash}" \
  75. --arg timestamp "${timestamp}" \
  76. --arg sha256hash "${sha256hash}" \
  77. '. | .url=$url | .name=$name | .version=$version | .productVersion=$productVersion | .hash=$hash | .timestamp=$timestamp | .sha256hash=$sha256hash' \
  78. <<<'{}' )
  79. }
  80. transformVersion() {
  81. local version parts
  82. version="${1%-insider}"
  83. IFS='.' read -r -a parts <<< "${version}"
  84. # Remove leading zeros from third part
  85. parts[2]="$((10#${parts[2]}))"
  86. version="${parts[0]}.${parts[1]}.${parts[2]}.0"
  87. if [[ "${1}" == *-insider ]]; then
  88. version="${version}-insider"
  89. fi
  90. echo "${version}"
  91. }
  92. updateLatestVersion() {
  93. echo "Updating ${VERSION_PATH}/latest.json"
  94. # do not update the same version
  95. if [[ -f "${REPOSITORY_NAME}/${VERSION_PATH}/latest.json" ]]; then
  96. CURRENT_VERSION=$( jq -r '.name' "${REPOSITORY_NAME}/${VERSION_PATH}/latest.json" )
  97. echo "CURRENT_VERSION: ${CURRENT_VERSION}"
  98. if [[ "${CURRENT_VERSION}" == "${RELEASE_VERSION}" && "${FORCE_UPDATE}" != "true" ]]; then
  99. return 0
  100. fi
  101. fi
  102. echo "Generating ${VERSION_PATH}/latest.json"
  103. mkdir -p "${REPOSITORY_NAME}/${VERSION_PATH}"
  104. generateJson
  105. echo "${JSON_DATA}" > "${REPOSITORY_NAME}/${VERSION_PATH}/latest.json"
  106. echo "${JSON_DATA}"
  107. }
  108. # init versions repo for later commiting + pushing the json file to it
  109. # thank you https://www.vinaygopinath.me/blog/tech/commit-to-master-branch-on-github-using-travis-ci/
  110. git clone "https://${GH_HOST}/${VERSIONS_REPOSITORY}.git"
  111. cd "${REPOSITORY_NAME}" || { echo "'${REPOSITORY_NAME}' dir not found"; exit 1; }
  112. git config user.email "$( echo "${GITHUB_USERNAME}" | awk '{print tolower($0)}' )-ci@not-real.com"
  113. git config user.name "${GITHUB_USERNAME} CI"
  114. git remote rm origin
  115. git remote add origin "https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@${GH_HOST}/${VERSIONS_REPOSITORY}.git" &> /dev/null
  116. cd ..
  117. if [[ "${OS_NAME}" == "osx" ]]; then
  118. ASSET_NAME="${APP_NAME}-darwin-${VSCODE_ARCH}-${RELEASE_VERSION}.zip"
  119. VERSION_PATH="${VSCODE_QUALITY}/darwin/${VSCODE_ARCH}"
  120. updateLatestVersion
  121. elif [[ "${OS_NAME}" == "windows" ]]; then
  122. # system installer
  123. ASSET_NAME="${APP_NAME}Setup-${VSCODE_ARCH}-${RELEASE_VERSION}.exe"
  124. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/system"
  125. updateLatestVersion
  126. # user installer
  127. ASSET_NAME="${APP_NAME}UserSetup-${VSCODE_ARCH}-${RELEASE_VERSION}.exe"
  128. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/user"
  129. updateLatestVersion
  130. # windows archive
  131. ASSET_NAME="${APP_NAME}-win32-${VSCODE_ARCH}-${RELEASE_VERSION}.zip"
  132. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/archive"
  133. updateLatestVersion
  134. if [[ "${VSCODE_ARCH}" == "ia32" || "${VSCODE_ARCH}" == "x64" ]]; then
  135. # msi
  136. ASSET_NAME="${APP_NAME}-${VSCODE_ARCH}-${RELEASE_VERSION}.msi"
  137. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/msi"
  138. updateLatestVersion
  139. # updates-disabled msi
  140. ASSET_NAME="${APP_NAME}-${VSCODE_ARCH}-updates-disabled-${RELEASE_VERSION}.msi"
  141. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/msi-updates-disabled"
  142. updateLatestVersion
  143. fi
  144. else # linux
  145. # update service links to tar.gz file
  146. # see https://update.code.visualstudio.com/api/update/linux-x64/stable/VERSION
  147. # as examples
  148. ASSET_NAME="${APP_NAME}-linux-${VSCODE_ARCH}-${RELEASE_VERSION}.tar.gz"
  149. VERSION_PATH="${VSCODE_QUALITY}/linux/${VSCODE_ARCH}"
  150. updateLatestVersion
  151. fi
  152. cd "${REPOSITORY_NAME}" || { echo "'${REPOSITORY_NAME}' dir not found"; exit 1; }
  153. git pull origin master # in case another build just pushed
  154. git add .
  155. CHANGES=$( git status --porcelain )
  156. if [[ -n "${CHANGES}" ]]; then
  157. echo "Some changes have been found, pushing them"
  158. dateAndMonth=$( date "+%D %T" )
  159. git commit -m "CI update: ${dateAndMonth} (Build ${GITHUB_RUN_NUMBER})"
  160. if ! git push origin master --quiet; then
  161. git pull origin master
  162. git push origin master --quiet
  163. fi
  164. else
  165. echo "No changes"
  166. fi
  167. cd ..