updater.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. #=================================================
  3. # PACKAGE UPDATING HELPER
  4. #=================================================
  5. # This script is meant to be run by GitHub Actions
  6. # The YunoHost-Apps organisation offers a template Action to run this script periodically
  7. # Since each app is different, maintainers can adapt its contents so as to perform
  8. # automatic actions when a new upstream release is detected.
  9. # Remove this exit command when you are ready to run this Action
  10. #exit 1
  11. #=================================================
  12. # FETCHING LATEST RELEASE AND ITS ASSETS
  13. #=================================================
  14. # Fetching information
  15. current_version=$(cat manifest.json | jq -j '.version|split("~")[0]')
  16. repo=$(cat manifest.json | jq -j '.upstream.code|split("https://github.com/")[1]')
  17. # Some jq magic is needed, because the latest upstream release is not always the latest version (e.g. security patches for older versions)
  18. version=$(curl --silent "https://api.github.com/repos/$repo/releases" | jq -r '.[] | select( .prerelease != true ) | .tag_name' | sort -V | tail -1)
  19. assets=($(curl --silent "https://api.github.com/repos/$repo/releases" | jq -r '[ .[] | select(.tag_name=="'$version'").assets[].browser_download_url ] | join(" ") | @sh' | tr -d "'"))
  20. # Later down the script, we assume the version has only digits and dots
  21. # Sometimes the release name starts with a "v", so let's filter it out.
  22. # You may need more tweaks here if the upstream repository has different naming conventions.
  23. if [[ ${version:0:1} == "v" || ${version:0:1} == "V" ]]; then
  24. version=${version:1}
  25. fi
  26. # Setting up the environment variables
  27. echo "Current version: $current_version"
  28. echo "Latest release from upstream: $version"
  29. echo "VERSION=$version" >> $GITHUB_ENV
  30. # For the time being, let's assume the script will fail
  31. echo "PROCEED=false" >> $GITHUB_ENV
  32. # Proceed only if the retrieved version is greater than the current one
  33. if ! dpkg --compare-versions "$current_version" "lt" "$version" ; then
  34. echo "::warning ::No new version available"
  35. exit 0
  36. # Proceed only if a PR for this new version does not already exist
  37. elif git ls-remote -q --exit-code --heads https://github.com/$GITHUB_REPOSITORY.git ci-auto-update-v$version ; then
  38. echo "::warning ::A branch already exists for this update"
  39. exit 0
  40. fi
  41. # Each release can hold multiple assets (e.g. binaries for different architectures, source code, etc.)
  42. echo "${#assets[@]} available asset(s)"
  43. #=================================================
  44. # UPDATE SOURCE FILES
  45. #=================================================
  46. # Here we use the $assets variable to get the resources published in the upstream release.
  47. # Here is an example for Grav, it has to be adapted in accordance with how the upstream releases look like.
  48. # Let's loop over the array of assets URLs
  49. for asset_url in ${assets[@]}; do
  50. echo "Handling asset at $asset_url"
  51. # Assign the asset to a source file in conf/ directory
  52. # Here we base the source file name upon a unique keyword in the assets url (admin vs. update)
  53. # Leave $src empty to ignore the asset
  54. case $asset_url in
  55. *"admin"*)
  56. src="app"
  57. ;;
  58. *"update"*)
  59. src="app-upgrade"
  60. ;;
  61. *)
  62. src=""
  63. ;;
  64. esac
  65. # If $src is not empty, let's process the asset
  66. if [ ! -z "$src" ]; then
  67. # Create the temporary directory
  68. tempdir="$(mktemp -d)"
  69. # Download sources and calculate checksum
  70. filename=${asset_url##*/}
  71. curl --silent -4 -L $asset_url -o "$tempdir/$filename"
  72. checksum=$(sha256sum "$tempdir/$filename" | head -c 64)
  73. # Delete temporary directory
  74. rm -rf $tempdir
  75. # Get extension
  76. if [[ $filename == *.tar.gz ]]; then
  77. extension=tar.gz
  78. else
  79. extension=${filename##*.}
  80. fi
  81. # Rewrite source file
  82. cat <<EOT > conf/$src.src
  83. SOURCE_URL=$asset_url
  84. SOURCE_SUM=$checksum
  85. SOURCE_SUM_PRG=sha256sum
  86. SOURCE_FORMAT=$extension
  87. SOURCE_IN_SUBDIR=true
  88. SOURCE_FILENAME=
  89. EOT
  90. echo "... conf/$src.src updated"
  91. else
  92. echo "... asset ignored"
  93. fi
  94. done
  95. #=================================================
  96. # SPECIFIC UPDATE STEPS
  97. #=================================================
  98. # Any action on the app's source code can be done.
  99. # The GitHub Action workflow takes care of committing all changes after this script ends.
  100. #=================================================
  101. # GENERIC FINALIZATION
  102. #=================================================
  103. # Replace new version in manifest
  104. echo "$(jq -s --indent 4 ".[] | .version = \"$version~ynh1\"" manifest.json)" > manifest.json
  105. # No need to update the README, yunohost-bot takes care of it
  106. # The Action will proceed only if the PROCEED environment variable is set to true
  107. echo "PROCEED=true" >> $GITHUB_ENV
  108. exit 0