update_version.sh 6.2 KB

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