update-wiki.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/sh
  2. #
  3. # Copyright (c) Milan Jurkulák 2024.
  4. # Contact:
  5. # e: mimoccc@gmail.com
  6. # e: mj@mjdev.org
  7. # w: https://mjdev.org
  8. #
  9. set -eu
  10. #
  11. TEMP_REPO_DIR="$GITHUB_REPOSITORY$GITHUB_SHA"
  12. #
  13. if [ -z "$WIKI_DIR" ]; then
  14. echo "Wiki location is not specified, using default wiki/"
  15. WIKI_DIR='wiki'
  16. fi
  17. #
  18. if [ -z "$GH_TOKEN" ]; then
  19. echo "Token is not specified"
  20. exit 1
  21. fi
  22. # Clone wiki repo
  23. echo "Cloning wiki repo https://github.com/$GITHUB_REPOSITORY.wiki.git"
  24. #
  25. git clone "https://$GITHUB_ACTOR:$GH_TOKEN@github.com/$GITHUB_REPOSITORY.wiki.git" "$TEMP_REPO_DIR"
  26. # Copy wikis
  27. echo "Removing old wiki"
  28. old_dir=$(pwd)
  29. cd "$TEMP_REPO_DIR" || exit
  30. git rm -rf .
  31. git clean -fxd
  32. git update-ref -d refs/heads/master
  33. cd "$old_dir" || exit
  34. echo "Copying edited wiki"
  35. cp -a "$WIKI_DIR/." "$TEMP_REPO_DIR"
  36. # Get commit details
  37. author=$(git log -1 --format="%an")
  38. email=$(git log -1 --format="%ae")
  39. message=$(git log -1 --format="%s")
  40. # Changes check
  41. echo "Checking if wiki has changes"
  42. cd "$TEMP_REPO_DIR" || exit
  43. git config --local user.email "$email"
  44. git config --local user.name "$author"
  45. git add .
  46. if git diff-index --quiet HEAD; then
  47. echo "Nothing changed"
  48. exit 0
  49. fi
  50. # Push
  51. echo "Pushing changes to wiki"
  52. git commit -m "$message" && git push "https://$GITHUB_ACTOR:$GH_TOKEN@github.com/$GITHUB_REPOSITORY.wiki.git"
  53. # Done