deploy.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: MIT
  3. # This script is executed by GitHub Actions when a PR is merged (i.e. in the `deploy` step).
  4. set -ex
  5. function initialize {
  6. export TLDR_ARCHIVE="tldr.zip"
  7. if [[ ! -f $TLDR_ARCHIVE ]]; then
  8. echo "No changes to deploy."
  9. exit 0
  10. fi
  11. export SITE_HOME="$HOME/site"
  12. export LANG_ARCHIVES="$GITHUB_WORKSPACE/language_archives"
  13. export PDFS="$GITHUB_WORKSPACE/scripts/pdf"
  14. export INDEX="$GITHUB_WORKSPACE/index.json"
  15. RELEASE_TAG="$(git describe --tags --abbrev=0)"
  16. export RELEASE_TAG
  17. # Configure git.
  18. git config --global user.email "tldrbotgithub@gmail.com"
  19. git config --global user.name "tldr bot"
  20. git config --global push.default simple
  21. git config --global diff.zip.textconv "unzip -c -a"
  22. # Decrypt and add deploy key.
  23. eval "$(ssh-agent -s)"
  24. echo "$DEPLOY_KEY" > id_ed25519
  25. chmod 600 id_ed25519
  26. ssh-add id_ed25519
  27. }
  28. function upload_assets {
  29. git clone --quiet --depth 1 "git@github.com:tldr-pages/tldr-pages.github.io.git" "$SITE_HOME"
  30. cp -f "$TLDR_ARCHIVE" "$SITE_HOME/assets/"
  31. find "$LANG_ARCHIVES" -maxdepth 1 -name "*.zip" -exec cp -f {} "$SITE_HOME/assets/" \;
  32. cp -f "$INDEX" "$SITE_HOME/assets/"
  33. find "$PDFS" -maxdepth 1 -name "*.pdf" -exec cp -f {} "$SITE_HOME/assets/" \;
  34. cd "$SITE_HOME/assets"
  35. sha256sum -- index.json *.zip > tldr.sha256sums
  36. # Old way of distributing assets. This needs to be deleted later.
  37. git add -A
  38. git commit -m "[GitHub Actions] uploaded assets after commit tldr-pages/tldr@$GITHUB_SHA"
  39. git push -q
  40. echo "Assets (pages archive, index and checksums) deployed to the static site."
  41. # Suppress errors from unmatched patterns if some files don't exist.
  42. shopt -s nullglob
  43. gh release --repo tldr-pages/tldr upload --clobber "$RELEASE_TAG" -- \
  44. tldr.sha256sums \
  45. "$TLDR_ARCHIVE" \
  46. "$INDEX" \
  47. "$LANG_ARCHIVES/"*.zip \
  48. "$PDFS/"*.pdf
  49. shopt -u nullglob
  50. echo "Assets deployed to GitHub releases."
  51. }
  52. ###################################
  53. # MAIN
  54. ###################################
  55. initialize
  56. upload_assets