|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Deploy static site to the GitHub pages. |
| 4 | + |
| 5 | +set -eo pipefail |
| 6 | + |
| 7 | +git_repo_root=$(git rev-parse --show-toplevel) |
| 8 | +site_src="${git_repo_root}/_site" |
| 9 | +gh_pages_root="${git_repo_root}/docs" |
| 10 | + |
| 11 | +# Site built from the main branch will be available at 'https://<domain_name>/'. |
| 12 | +# Sites built from other branchs will be available at 'https://<domain_name>/branches/<branch_name>'. |
| 13 | +main_git_branch="hakyll" |
| 14 | + |
| 15 | +# replace "/", "#", etc with "-". |
| 16 | +slugify() { |
| 17 | + echo "$1" | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+/-/g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$/-/g' | tr A-Z a-z |
| 18 | +} |
| 19 | + |
| 20 | +deploy() { |
| 21 | + if [[ ! -z "$GITHUB_REF_NAME" ]]; then |
| 22 | + # The GITHUB_REF_NAME env variable is available in github actions. |
| 23 | + git_branch=$GITHUB_REF_NAME |
| 24 | + else |
| 25 | + git_branch=$(git branch --show-current) |
| 26 | + fi |
| 27 | + echo "Current git branch is '${git_branch}'." |
| 28 | + |
| 29 | + git config user.name github-actions |
| 30 | + git config user.email [email protected] |
| 31 | + |
| 32 | + git checkout gh-pages |
| 33 | + git pull origin gh-pages |
| 34 | + |
| 35 | + if [ "$git_branch" == "$main_git_branch" ]; then |
| 36 | + site_dest="${gh_pages_root}" |
| 37 | + |
| 38 | + # Create temporary backup for other branches content. |
| 39 | + mv "${gh_pages_root}/branches" . |
| 40 | + |
| 41 | + # Replace site files. |
| 42 | + rm -rf "${site_dest}" |
| 43 | + mkdir -p "${site_dest}" |
| 44 | + cp -a -v ${site_src}/* ${site_dest}/ |
| 45 | + |
| 46 | + # Restore temporary backup for other branches content. |
| 47 | + mv ./branches "${gh_pages_root}/" |
| 48 | + else |
| 49 | + site_dest="${gh_pages_root}/branches/$(slugify ${git_branch})" |
| 50 | + |
| 51 | + # Replace site files. |
| 52 | + rm -rf "${site_dest}" |
| 53 | + mkdir -p "${site_dest}" |
| 54 | + cp -a -v ${site_src}/* ${site_dest}/ |
| 55 | + fi |
| 56 | + |
| 57 | + echo "Updating gh-pages branch." |
| 58 | + git add --all |
| 59 | + git commit --allow-empty -m "[$(date '+%F %T %Z')] Updated site for the '${git_branch}' branch [ci skip]" |
| 60 | + git push --force origin gh-pages |
| 61 | + echo "Deployment finished." |
| 62 | +} |
| 63 | + |
| 64 | +update_deployments_list() { |
| 65 | + github_project_url=$(git remote get-url origin) |
| 66 | + if [[ $github_project_url == [email protected]: * ]] ; then |
| 67 | + github_repo= $(echo ${github_project_url#"[email protected]:"} | sed 's/\.git$//g') |
| 68 | + elif [[ $github_project_url == https://github.com/* ]]; then |
| 69 | + github_repo=$(echo ${github_project_url#"https://github.com/"} | sed 's/\.git$//g' | sed 's/^\/\///g') |
| 70 | + fi |
| 71 | + |
| 72 | + github_repo_owner=$(echo "${github_repo}" | sed 's/\/.*$//g') |
| 73 | + github_repo_name=$(echo "${github_repo}" | sed 's/^.*\///g') |
| 74 | + |
| 75 | + deployments_list="DEPLOYMENTS.md" |
| 76 | + echo "Updating ${deployments_list}" |
| 77 | + rm $deployments_list |
| 78 | + |
| 79 | + # Create a markdown table |
| 80 | + touch $deployments_list |
| 81 | + echo "# Deployments" >>$deployments_list |
| 82 | + echo "" >>$deployments_list |
| 83 | + echo "| Branch | Link |" >>$deployments_list |
| 84 | + echo "| --- | --- |" >>$deployments_list |
| 85 | + |
| 86 | + main_deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/" |
| 87 | + echo "| [main](https://github.com/${github_repo_owner}/${github_repo_name}/tree/${branch}) | [Open](${main_deployment_url}) |" >>$deployments_list |
| 88 | + |
| 89 | + remote_branches=$(git ls-remote --heads origin | sed 's?.*refs/heads/??') |
| 90 | + echo "$remote_branches" | while IFS= read -r branch; do |
| 91 | + safe_branch=$(slugify $branch) |
| 92 | + deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/branches/${safe_branch}" |
| 93 | + echo "| [${branch}](https://github.com/${github_repo_owner}/${github_repo_name}/tree/${branch}) | [Open](${deployment_url}) |" >>$deployments_list |
| 94 | + done |
| 95 | + |
| 96 | + # Update gh-pages branch |
| 97 | + git add --all |
| 98 | + git commit --allow-empty -m "Update ${deployments_list}" |
| 99 | + git push --force origin gh-pages |
| 100 | +} |
| 101 | + |
| 102 | +deploy |
| 103 | +update_deployments_list |
0 commit comments