entrypoint.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. set -e
  3. zulip_organization_url=$1
  4. zulip_bot_email=$2
  5. zulip_bot_api_key=$3
  6. github_personal_access_token=$4
  7. delete_history=$5
  8. archive_branch=$6
  9. # This is a temporary workaround.
  10. # See https://github.com/actions/checkout/issues/766
  11. git config --global --add safe.directory "$GITHUB_WORKSPACE"
  12. checked_out_repo_path="$(pwd)"
  13. html_dir_path=$checked_out_repo_path
  14. json_dir_path="${checked_out_repo_path}/zulip_json"
  15. img_dir_path="${checked_out_repo_path}/assets/img"
  16. streams_config_file_path="${checked_out_repo_path}/streams.yaml"
  17. initial_sha="$(git rev-parse HEAD)"
  18. if [ ! -f $streams_config_file_path ]; then
  19. echo "Missing streams.yaml file."
  20. exit 1
  21. fi
  22. cd "/zulip-archive-action"
  23. curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
  24. python3 get-pip.py
  25. pip install virtualenv
  26. virtualenv -p python3 .
  27. source bin/activate
  28. pip3 install install -r requirements.txt
  29. # crudini is not available as an Alpine pkg, so we install via pip.
  30. pip3 install crudini
  31. # Uses GitHub pages API
  32. # https://docs.github.com/en/rest/pages
  33. auth_header="Authorization: Bearer ${github_personal_access_token}"
  34. accept_header="Accept: application/vnd.github.switcheroo-preview+json"
  35. page_api_url="https://api.github.com/repos/${GITHUB_REPOSITORY}/pages"
  36. # Enable GitHub pages
  37. curl -H "$auth_header" -H "$accept_header" --data "{\"source\":{\"branch\":\"${archive_branch}\"}}" "$page_api_url"
  38. print_site_url_code="import sys, json; print(json.load(sys.stdin)['html_url'])"
  39. github_pages_url_with_trailing_slash=$(curl -H "${auth_header}" $page_api_url | python3 -c "${print_site_url_code}")
  40. github_pages_url=${github_pages_url_with_trailing_slash%/}
  41. cp default_settings.py settings.py
  42. cp $streams_config_file_path .
  43. crudini --set zuliprc api site $zulip_organization_url
  44. crudini --set zuliprc api key $zulip_bot_api_key
  45. crudini --set zuliprc api email $zulip_bot_email
  46. export PROD_ARCHIVE=true
  47. export SITE_URL=$github_pages_url
  48. export HTML_DIRECTORY=$html_dir_path
  49. export JSON_DIRECTORY=$json_dir_path
  50. export HTML_ROOT=""
  51. export ZULIP_ICON_URL="${github_pages_url}/assets/img/zulip.svg"
  52. if [ ! -d $json_dir_path ]; then
  53. mkdir -p $json_dir_path
  54. mkdir -p $img_dir_path
  55. cp assets/img/* $img_dir_path
  56. python3 archive.py -t
  57. else
  58. python3 archive.py -i
  59. fi
  60. python3 archive.py -b
  61. cd ${checked_out_repo_path}
  62. git checkout $archive_branch
  63. git fetch origin
  64. current_sha="$(git rev-parse origin/${archive_branch})"
  65. if [[ "$current_sha" != "$initial_sha" ]]
  66. then
  67. echo "Archive update failed, commits have been added while processing"
  68. exit 1
  69. fi
  70. echo "delete history: $delete_history"
  71. if [[ "$delete_history" == "true" ]]
  72. then
  73. echo "resetting"
  74. rm -rf .git
  75. git config --global init.defaultBranch "$archive_branch"
  76. git init
  77. fi
  78. git config --global user.email "zulip-archive-bot@users.noreply.github.com"
  79. git config --global user.name "Archive Bot"
  80. git add -A
  81. git commit -m "Update archive."
  82. git remote add origin2 https://${GITHUB_ACTOR}:${github_personal_access_token}@github.com/${GITHUB_REPOSITORY}
  83. git push origin2 HEAD:$archive_branch -f
  84. echo "pushed"
  85. echo "Zulip Archive published/updated in ${github_pages_url}"