updater.sh 5.0 KB

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