update_version.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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="${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. updateLatestVersion() {
  81. echo "Updating ${VERSION_PATH}/latest.json"
  82. # do not update the same version
  83. if [[ -f "${REPOSITORY_NAME}/${VERSION_PATH}/latest.json" ]]; then
  84. CURRENT_VERSION=$( jq -r '.name' "${REPOSITORY_NAME}/${VERSION_PATH}/latest.json" )
  85. echo "CURRENT_VERSION: ${CURRENT_VERSION}"
  86. if [[ "${CURRENT_VERSION}" == "${RELEASE_VERSION}" && "${FORCE_UPDATE}" != "true" ]]; then
  87. return 0
  88. fi
  89. fi
  90. echo "Generating ${VERSION_PATH}/latest.json"
  91. mkdir -p "${REPOSITORY_NAME}/${VERSION_PATH}"
  92. generateJson
  93. echo "${JSON_DATA}" > "${REPOSITORY_NAME}/${VERSION_PATH}/latest.json"
  94. echo "${JSON_DATA}"
  95. }
  96. # init versions repo for later commiting + pushing the json file to it
  97. # thank you https://www.vinaygopinath.me/blog/tech/commit-to-master-branch-on-github-using-travis-ci/
  98. git clone "https://${GH_HOST}/${VERSIONS_REPOSITORY}.git"
  99. cd "${REPOSITORY_NAME}" || { echo "'${REPOSITORY_NAME}' dir not found"; exit 1; }
  100. git config user.email "$( echo "${GITHUB_USERNAME}" | awk '{print tolower($0)}' )-ci@not-real.com"
  101. git config user.name "${GITHUB_USERNAME} CI"
  102. git remote rm origin
  103. git remote add origin "https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@${GH_HOST}/${VERSIONS_REPOSITORY}.git" &> /dev/null
  104. cd ..
  105. if [[ "${OS_NAME}" == "osx" ]]; then
  106. ASSET_NAME="${APP_NAME}-darwin-${VSCODE_ARCH}-${RELEASE_VERSION}.zip"
  107. VERSION_PATH="${VSCODE_QUALITY}/darwin/${VSCODE_ARCH}"
  108. updateLatestVersion
  109. elif [[ "${OS_NAME}" == "windows" ]]; then
  110. # system installer
  111. ASSET_NAME="${APP_NAME}Setup-${VSCODE_ARCH}-${RELEASE_VERSION}.exe"
  112. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/system"
  113. updateLatestVersion
  114. # user installer
  115. ASSET_NAME="${APP_NAME}UserSetup-${VSCODE_ARCH}-${RELEASE_VERSION}.exe"
  116. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/user"
  117. updateLatestVersion
  118. # windows archive
  119. ASSET_NAME="${APP_NAME}-win32-${VSCODE_ARCH}-${RELEASE_VERSION}.zip"
  120. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/archive"
  121. updateLatestVersion
  122. if [[ "${VSCODE_ARCH}" == "ia32" || "${VSCODE_ARCH}" == "x64" ]]; then
  123. # msi
  124. ASSET_NAME="${APP_NAME}-${VSCODE_ARCH}-${RELEASE_VERSION}.msi"
  125. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/msi"
  126. updateLatestVersion
  127. # updates-disabled msi
  128. ASSET_NAME="${APP_NAME}-${VSCODE_ARCH}-updates-disabled-${RELEASE_VERSION}.msi"
  129. VERSION_PATH="${VSCODE_QUALITY}/win32/${VSCODE_ARCH}/msi-updates-disabled"
  130. updateLatestVersion
  131. fi
  132. else # linux
  133. # update service links to tar.gz file
  134. # see https://update.code.visualstudio.com/api/update/linux-x64/stable/VERSION
  135. # as examples
  136. ASSET_NAME="${APP_NAME}-linux-${VSCODE_ARCH}-${RELEASE_VERSION}.tar.gz"
  137. VERSION_PATH="${VSCODE_QUALITY}/linux/${VSCODE_ARCH}"
  138. updateLatestVersion
  139. fi
  140. cd "${REPOSITORY_NAME}" || { echo "'${REPOSITORY_NAME}' dir not found"; exit 1; }
  141. git pull origin master # in case another build just pushed
  142. git add .
  143. CHANGES=$( git status --porcelain )
  144. if [[ -n "${CHANGES}" ]]; then
  145. echo "Some changes have been found, pushing them"
  146. dateAndMonth=$( date "+%D %T" )
  147. git commit -m "CI update: ${dateAndMonth} (Build ${GITHUB_RUN_NUMBER})"
  148. if ! git push origin master --quiet; then
  149. git pull origin master
  150. git push origin master --quiet
  151. fi
  152. else
  153. echo "No changes"
  154. fi
  155. cd ..