deploy-site.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. # Publishes website/ to https://lynx-share.com (Hostinger static deploy).
  3. # Needs the Hostinger API token in ~/.config/hostinger/token (never commit it).
  4. # The AppImage's update address (update/, see appimage-update-lib.sh) is never taken from this tree: the archive carries
  5. # the target that is live right now, and the deploy is refused when that can't be read, and reported when it changed.
  6. # --dry-run: everything up to the upload, then lists what the archive has under update/.
  7. set -euo pipefail
  8. DRY=0; [[ "${1:-}" == "--dry-run" ]] && DRY=1
  9. SCRIPTS="$(cd "$(dirname "$0")" && pwd)"
  10. # shellcheck source=scripts/appimage-update-lib.sh
  11. . "$SCRIPTS/appimage-update-lib.sh"
  12. "$SCRIPTS/site-media.sh" || { echo "deploy-site: the tutorial videos (media branch, website/_src/learn/media.json) are not in place: not deploying" >&2; exit 1; }
  13. cd "$SCRIPTS/../website"
  14. TARGET="$(update_target)" || { echo "deploy-site: can't read the update address $UPDATE_ADDRESS: not deploying, try again" >&2; exit 1; }
  15. TARGET_ARM="$(update_target_aarch64)" || { echo "deploy-site: can't read the update address $UPDATE_ADDRESS_AARCH64: not deploying, try again" >&2; exit 1; }
  16. [[ -d update ]] && echo "deploy-site: leaving website/update out of the archive (the live target stays)"
  17. USER_=u258086853; DOMAIN=lynx-share.com
  18. ZIP=$(mktemp -d)/lynx-site.zip
  19. HT="$(mktemp)"; [[ -n "$TARGET" ]] && update_htaccess "$TARGET" "$TARGET_ARM" > "$HT" # (round 190qp: the arm64 redirect kept too)
  20. ZIP="$ZIP" HT="$HT" TARGET="$TARGET" python3 -c "
  21. import zipfile,os
  22. z=zipfile.ZipFile(os.environ['ZIP'],'w',zipfile.ZIP_DEFLATED)
  23. for r,d,f in os.walk('.'):
  24. if r=='.' and 'update' in d: d.remove('update')
  25. for x in f: z.write(os.path.join(r,x),os.path.relpath(os.path.join(r,x),'.'))
  26. if os.environ['TARGET']: z.write(os.environ['HT'],'update/.htaccess')
  27. z.close()"
  28. if [[ "$DRY" == 1 ]]; then
  29. echo "target: ${TARGET:-none}"
  30. python3 -c "import zipfile,sys;z=zipfile.ZipFile(sys.argv[1]);[print('archive:',n,'|',z.read(n).decode().splitlines()[1]) for n in z.namelist() if n.startswith('update/')];print('archive files:',len(z.namelist()))" "$ZIP"
  31. update_reminder "$TARGET"
  32. exit 0
  33. fi
  34. T=$(cat ~/.config/hostinger/token); B=https://developers.hostinger.com
  35. J=$(curl -sf -X POST -H "Authorization: Bearer $T" -H 'Content-Type: application/json' -d "{\"username\":\"$USER_\",\"domain\":\"$DOMAIN\"}" $B/api/hosting/v1/files/upload-urls)
  36. get() { python3 -c "import json,sys;print(json.load(sys.stdin)['$1'])" <<<"$J"; }
  37. URL=$(get url); AK=$(get auth_key); RK=$(get rest_auth_key); SIZE=$(stat -c%s "$ZIP")
  38. H=(-H "X-Auth: $AK" -H "X-Auth-Rest: $RK" -H 'Tus-Resumable: 1.0.0')
  39. curl -sf -o /dev/null -X POST "$URL/lynx-site.zip?override=true" "${H[@]}" -H "Upload-Length: $SIZE" -H 'Upload-Offset: 0'
  40. curl -sf -o /dev/null -X PATCH "$URL/lynx-site.zip?override=true" "${H[@]}" -H 'Upload-Offset: 0' -H 'Content-Type: application/offset+octet-stream' --data-binary @"$ZIP"
  41. curl -sf -X POST -H "Authorization: Bearer $T" -H 'Content-Type: application/json' -d '{"archive_path":"lynx-site.zip"}' $B/api/hosting/v1/accounts/$USER_/websites/$DOMAIN/deploy; echo
  42. # the CDN keeps assets for 30 days: purge it so the new files show up at once
  43. curl -sf -X DELETE -H "Authorization: Bearer $T" "$B/api/hosting/v1/accounts/$USER_/websites/$DOMAIN/cache/clear"; echo
  44. # (the deploy is accepted first and runs after: give it a moment)
  45. for _ in 1 2 3 4 5 6; do
  46. sleep 10
  47. AFTER="$(update_target || echo unreadable)"
  48. [[ "$AFTER" == "$TARGET" ]] && break
  49. done
  50. if [[ "$AFTER" != "$TARGET" ]]; then
  51. echo "deploy-site: THE UPDATE ADDRESS CHANGED: was '${TARGET:-none}', is now '${AFTER:-none}'. Run scripts/publish-appimage-update.sh for the newest release." >&2
  52. exit 1
  53. fi
  54. update_reminder "$TARGET"