deploy_wiki.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. function debug() {
  3. echo "::debug file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
  4. }
  5. function warning() {
  6. echo "::warning file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
  7. }
  8. function error() {
  9. echo "::error file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::$1"
  10. }
  11. function add_mask() {
  12. echo "::add-mask::$1"
  13. }
  14. if [ -z "$GITHUB_ACTOR" ]; then
  15. error "GITHUB_ACTOR environment variable is not set"
  16. exit 1
  17. fi
  18. if [ -z "$GITHUB_REPOSITORY" ]; then
  19. error "GITHUB_REPOSITORY environment variable is not set"
  20. exit 1
  21. fi
  22. if [ -z "$GH_PERSONAL_ACCESS_TOKEN" ]; then
  23. error "GH_PERSONAL_ACCESS_TOKEN environment variable is not set"
  24. exit 1
  25. fi
  26. if [ -z "$WIKI_PATH" ]; then
  27. echo "WIKI_PATH environment variable is not set"
  28. exit 1
  29. fi
  30. add_mask "${GH_PERSONAL_ACCESS_TOKEN}"
  31. if [ -z "${WIKI_COMMIT_MESSAGE:-}" ]; then
  32. debug "WIKI_COMMIT_MESSAGE not set, using default"
  33. WIKI_COMMIT_MESSAGE='Automatically publish wiki'
  34. fi
  35. GIT_REPOSITORY_URL="https://${GH_PERSONAL_ACCESS_TOKEN}@github.com/$GITHUB_REPOSITORY.wiki.git"
  36. debug "Checking out wiki repository"
  37. tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
  38. (
  39. cd "$tmp_dir" || exit 1
  40. git init
  41. git config user.name "$GITHUB_ACTOR"
  42. git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
  43. git pull "$GIT_REPOSITORY_URL"
  44. )
  45. debug "Rsync contents of $WIKI_PATH"
  46. rsync -q -a --delete --exclude=.git "$GITHUB_WORKSPACE/$WIKI_PATH/" "$tmp_dir"
  47. if [ ! -r "$tmp_dir/Home.md" ]; then
  48. debug "Copy README.md to wiki/Home.md"
  49. rsync -q -a "$GITHUB_WORKSPACE/README.md" "$tmp_dir/Home.md"
  50. fi
  51. debug "Rewriting images path to absolute"
  52. (
  53. cd "$tmp_dir" || exit 1
  54. find . -type f -exec sed -Ei 's@([ (])([^( ]+)(\/wiki_static\/.+?\.(png|jpe?g|svg)[ \)])@\1https://github.com/Flipper-Zero/flipperzero-firmware-community/raw/master\3@' {} \;
  55. )
  56. debug "Committing and pushing changes"
  57. (
  58. cd "$tmp_dir" || exit 1
  59. git add .
  60. git commit -m "$WIKI_COMMIT_MESSAGE"
  61. git push --set-upstream "$GIT_REPOSITORY_URL" master
  62. )
  63. rm -rf "$tmp_dir"
  64. exit 0