update_version.sh 6.1 KB

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