updater.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #=================================================
  10. # FETCHING LATEST RELEASE AND ITS ASSETS
  11. #=================================================
  12. # Fetching information
  13. current_version=$(cat manifest.json | jq -j '.version|split("~")[0]')
  14. repo=$(cat manifest.json | jq -j '.upstream.code|split("https://github.com/")[1]')
  15. # Some jq magic is needed, because the latest upstream release is not always the latest version (e.g. security patches for older versions)
  16. version=$(curl --silent "https://api.github.com/repos/$repo/releases" | jq -r '.[] | select( .prerelease != true ) | .tag_name' | sort -V | tail -1)
  17. version_dashboard=$(curl --silent "https://api.github.com/repos/pi-hole/AdminLTE/releases" | jq -r '.[] | select( .prerelease != true ) | .tag_name' | sort -V | tail -1)
  18. assets[0]="https://github.com/pi-hole/pi-hole/archive/$version.tar.gz"
  19. assets[1]="https://github.com/pi-hole/AdminLTE/archive/$version_dashboard.tar.gz"
  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. echo "REPO=$repo" >> $GITHUB_ENV
  31. # For the time being, let's assume the script will fail
  32. echo "PROCEED=false" >> $GITHUB_ENV
  33. # Proceed only if the retrieved version is greater than the current one
  34. if ! dpkg --compare-versions "$current_version" "lt" "$version" ; then
  35. echo "::warning ::No new version available"
  36. exit 0
  37. # Proceed only if a PR for this new version does not already exist
  38. elif git ls-remote -q --exit-code --heads https://github.com/$GITHUB_REPOSITORY.git ci-auto-update-v$version ; then
  39. echo "::warning ::A branch already exists for this update"
  40. exit 0
  41. fi
  42. # Each release can hold multiple assets (e.g. binaries for different architectures, source code, etc.)
  43. echo "${#assets[@]} available asset(s)"
  44. #=================================================
  45. # UPDATE SOURCE FILES
  46. #=================================================
  47. # Here we use the $assets variable to get the resources published in the upstream release.
  48. # Here is an example for Grav, it has to be adapted in accordance with how the upstream releases look like.
  49. # Let's loop over the array of assets URLs
  50. for asset_url in ${assets[@]}; do
  51. echo "Handling asset at $asset_url"
  52. # Assign the asset to a source file in conf/ directory
  53. # Here we base the source file name upon a unique keyword in the assets url (admin vs. update)
  54. # Leave $src empty to ignore the asset
  55. case $asset_url in
  56. *"AdminLTE"*)
  57. src="admin_dashboard"
  58. ;;
  59. *"pi-hole"*)
  60. src="app"
  61. ;;
  62. *)
  63. src=""
  64. ;;
  65. esac
  66. # If $src is not empty, let's process the asset
  67. if [ ! -z "$src" ]; then
  68. # Create the temporary directory
  69. tempdir="$(mktemp -d)"
  70. # Download sources and calculate checksum
  71. filename=${asset_url##*/}
  72. curl --silent -4 -L $asset_url -o "$tempdir/$filename"
  73. checksum=$(sha256sum "$tempdir/$filename" | head -c 64)
  74. # Delete temporary directory
  75. rm -rf $tempdir
  76. # Get extension
  77. if [[ $filename == *.tar.gz ]]; then
  78. extension=tar.gz
  79. else
  80. extension=${filename##*.}
  81. fi
  82. # Rewrite source file
  83. cat <<EOT > conf/$src.src
  84. SOURCE_URL=$asset_url
  85. SOURCE_SUM=$checksum
  86. SOURCE_SUM_PRG=sha256sum
  87. SOURCE_FORMAT=$extension
  88. SOURCE_IN_SUBDIR=true
  89. SOURCE_FILENAME=
  90. SOURCE_EXTRACT=true
  91. EOT
  92. echo "... conf/$src.src updated"
  93. else
  94. echo "... asset ignored"
  95. fi
  96. done
  97. #=================================================
  98. # SPECIFIC UPDATE STEPS
  99. #=================================================
  100. # Any action on the app's source code can be done.
  101. # The GitHub Action workflow takes care of committing all changes after this script ends.
  102. #=================================================
  103. # GENERIC FINALIZATION
  104. #=================================================
  105. # Replace new version in manifest
  106. echo "$(jq -s --indent 4 ".[] | .version = \"$version~ynh1\"" manifest.json)" > manifest.json
  107. # No need to update the README, yunohost-bot takes care of it
  108. # The Action will proceed only if the PROCEED environment variable is set to true
  109. echo "PROCEED=true" >> $GITHUB_ENV
  110. exit 0