updater.sh 5.4 KB

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