upload-appimage.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #! /bin/bash
  2. # Env
  3. export BUILD=${DRONE_BUILD_NUMBER}
  4. export PR=${DRONE_PULL_REQUEST}
  5. export GIT_USERNAME=${CI_UPLOAD_GIT_USERNAME}
  6. export GIT_TOKEN=${CI_UPLOAD_GIT_TOKEN}
  7. # Defaults
  8. export GIT_REPO=ci-builds
  9. export API_BASE_URL=https://api.github.com/repos/$GIT_USERNAME/$GIT_REPO
  10. export DESKTOP_API_BASE_URL=https://api.github.com/repos/nextcloud/desktop
  11. # PR / master
  12. export TAG_NAME=${PR:=master}
  13. export RELEASE_BODY=https://github.com/nextcloud/desktop
  14. if [ $TAG_NAME != "master" ]; then
  15. TAG_NAME="PR-$TAG_NAME"
  16. RELEASE_BODY="nextcloud/desktop#$PR"
  17. fi
  18. cd /build
  19. # AppImage
  20. export APPIMAGE=$(readlink -f ./Nextcloud*.AppImage)
  21. export UPDATE=$(readlink -f ./Nextcloud*.AppImage.zsync)
  22. export BASENAME=$(basename ${APPIMAGE})
  23. if ! test -e $APPIMAGE ; then
  24. exit 1
  25. fi
  26. echo "Found AppImage: $BASENAME"
  27. if [ $TAG_NAME != "master" ]; then
  28. # Delete all old comments in desktop PR, starting with "AppImage file:"
  29. oldComments=$(curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X GET $DESKTOP_API_BASE_URL/issues/$PR/comments | jq '.[] | (.id |tostring) + "|" + (.user.login | test("'${GIT_USERNAME}'") | tostring) + "|" + (.body | test("AppImage file:.*") | tostring)' | grep "true|true" | tr -d "\"" | cut -f1 -d"|")
  30. if [[ "$oldComments" != "" ]]; then
  31. echo $oldComments | while read comment ; do
  32. curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X DELETE $DESKTOP_API_BASE_URL/issues/comments/$comment
  33. done
  34. fi
  35. fi
  36. # Helper functions
  37. urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
  38. create_release()
  39. {
  40. name=$TAG_NAME
  41. body=$RELEASE_BODY
  42. tagName=$TAG_NAME
  43. echo $(curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X POST $API_BASE_URL/releases -d "{ \"tag_name\": \"$tagName\", \"target_commitish\": \"master\", \"name\": \"$name\", \"body\": \"$body\", \"draft\": false, \"prerelease\": true }")
  44. }
  45. get_release()
  46. {
  47. tagName=$TAG_NAME
  48. echo $(curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X GET $API_BASE_URL/releases/tags/$tagName)
  49. }
  50. get_release_assets()
  51. {
  52. releaseId=$1
  53. echo $(curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X GET $API_BASE_URL/releases/$releaseId/assets)
  54. }
  55. upload_release_asset()
  56. {
  57. uploadUrl=$1
  58. echo $(curl --max-time 900 -u $GIT_USERNAME:$GIT_TOKEN -X POST $uploadUrl --header "Content-Type: application/octet-stream" --upload-file $APPIMAGE)
  59. echo $(curl --max-time 900 -u $GIT_USERNAME:$GIT_TOKEN -X POST $uploadUrl --header "Content-Type: application/octet-stream" --upload-file $UPDATE)
  60. }
  61. delete_release_asset()
  62. {
  63. assetId=$1
  64. curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X DELETE $API_BASE_URL/releases/assets/$assetId
  65. }
  66. # Try to get an already existing release
  67. json=$(get_release)
  68. releaseId=$(echo $json | jq -r '.id')
  69. uploadUrl=$(echo $json | jq -r '.upload_url')
  70. if [[ "$uploadUrl" == "null" ]]; then
  71. # Try to create a release
  72. json=$(create_release)
  73. releaseId=$(echo $json | jq -r '.id')
  74. uploadUrl=$(echo $json | jq -r '.upload_url')
  75. if [[ "$uploadUrl" == "null" ]]; then
  76. echo "create_release failed: $json"
  77. exit 2
  78. fi
  79. fi
  80. # Prepare upload url
  81. uploadUrl=$(echo "${uploadUrl/'{?name,label}'/?name=$BASENAME}")
  82. # Try to delete existing AppImage assets for this PR
  83. assets=$(get_release_assets $releaseId)
  84. for data in $(echo $assets | jq -r '.[] | @uri'); do
  85. json=$(urldecode "$data")
  86. assetId=$(echo $json | jq -r '.id')
  87. name=$(echo $json | jq -r '.name')
  88. if [[ "$name" == *.AppImage ]]; then
  89. echo "Deleting old asset: $name"
  90. $(delete_release_asset $assetId)
  91. fi
  92. done
  93. # Upload release asset
  94. echo "Uploading new asset: $BASENAME"
  95. json=$(upload_release_asset "$uploadUrl")
  96. browserDownloadUrl=$(echo $json | jq -r '.browser_download_url')
  97. if [[ "$browserDownloadUrl" == "null" ]]; then
  98. echo "upload_release_asset failed: $json"
  99. exit 3
  100. fi
  101. if [ $TAG_NAME != "master" ]; then
  102. # Create comment in desktop PR
  103. curl 2>/dev/null -u $GIT_USERNAME:$GIT_TOKEN -X POST $DESKTOP_API_BASE_URL/issues/$PR/comments -d "{ \"body\" : \"AppImage file: [$BASENAME]($browserDownloadUrl) <br/><br/>To test this change/fix you can simply download above AppImage file and test it. <br/><br/>Please make sure to quit your existing Nextcloud app and backup your data. \" }"
  104. fi
  105. echo
  106. echo "AppImage link: $browserDownloadUrl"