|
| 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 | +if [[ ! -z "$GITHUB_ACTIONS" ]]; then |
| 21 | + git_branch=$(slugify "$GITHUB_REF_NAME") |
| 22 | +else |
| 23 | + git_branch=$(slugify $(git branch --show-current)) |
| 24 | +fi |
| 25 | +echo "Current git branch is '${git_branch}'." |
| 26 | + |
| 27 | +git config user.name github-actions |
| 28 | +git config user.email [email protected] |
| 29 | + |
| 30 | +git checkout gh-pages |
| 31 | +git pull origin gh-pages |
| 32 | + |
| 33 | +if [ "$git_branch" == "$main_git_branch" ]; then |
| 34 | + site_dest="${gh_pages_root}" |
| 35 | + |
| 36 | + # Create temporary backup for other branches content. |
| 37 | + mv "${gh_pages_root}/branches" . |
| 38 | + |
| 39 | + # Replace site files. |
| 40 | + rm -rf "${site_dest}" |
| 41 | + mkdir -p "${site_dest}" |
| 42 | + cp -a -v ${site_src}/* ${site_dest}/ |
| 43 | + |
| 44 | + # Restore temporary backup for other branches content. |
| 45 | + mv ./branches "${gh_pages_root}/" |
| 46 | +else |
| 47 | + site_dest="${gh_pages_root}/branches/${git_branch}" |
| 48 | + |
| 49 | + # Replace site files. |
| 50 | + rm -rf "${site_dest}" |
| 51 | + mkdir -p "${site_dest}" |
| 52 | + cp -a -v ${site_src}/* ${site_dest}/ |
| 53 | +fi |
| 54 | + |
| 55 | +echo "Updating gh-pages branch." |
| 56 | +git add --all |
| 57 | +git commit --allow-empty -m "[$(date '+%F %T %Z')] Updated site for the '${git_branch}' branch [ci skip]" |
| 58 | +git push --force origin gh-pages |
| 59 | +echo "Deployment finished." |
| 60 | + |
| 61 | +echo "Publishing an URL to the deployment. It will be visible as a link at GitHub in the red/green commit checks, near CI jobs result." |
| 62 | +if [[ -z GITHUB_TOKEN ]]; then |
| 63 | + echo "GITHUB_TOKEN env variable must be provided." |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +github_project_url=$(git remote get-url origin) |
| 68 | +if [[ $github_project_url == [email protected]: * ]] ; then |
| 69 | + github_repo= $(echo ${github_project_url#"[email protected]:"} | sed 's/\.git$//g') |
| 70 | +elif [[ $github_project_url == https://github.com/* ]]; then |
| 71 | + github_repo=$(echo ${github_project_url#"https://github.com/"} | sed 's/\.git$//g' | sed 's/^\/\///g') |
| 72 | +fi |
| 73 | + |
| 74 | +github_repo_owner=$(echo "${github_repo}" | sed 's/\/.*$//g') |
| 75 | +github_repo_name=$(echo "${github_repo}" | sed 's/^.*\///g') |
| 76 | +git_head_sha=$(git rev-parse --verify HEAD) |
| 77 | + |
| 78 | +if [ "$git_branch" == "$main_git_branch" ]; then |
| 79 | + deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/" |
| 80 | +else |
| 81 | + deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/branches/${git_branch}" |
| 82 | +fi |
| 83 | + |
| 84 | +update_github_checks_json() |
| 85 | +{ |
| 86 | + cat <<EOF |
| 87 | +{ |
| 88 | + "name": "deployment", |
| 89 | + "head_sha": "${git_head_sha}", |
| 90 | + "status": "completed", |
| 91 | + "conclusion": "success", |
| 92 | + "details_url": "${deployment_url}" |
| 93 | +} |
| 94 | +EOF |
| 95 | +} |
| 96 | + |
| 97 | +curl \ |
| 98 | + --header "Authorization: Bearer ${GITHUB_TOKEN}" \ |
| 99 | + --header "Accept: application/vnd.github.v3+json" \ |
| 100 | + --header "User-Agent: github-actions" \ |
| 101 | + --header "Content-Type: application/json" \ |
| 102 | + --data "$(update_github_checks_json)" \ |
| 103 | + --fail \ |
| 104 | + https://api.github.com/repos/${github_repo_owner}/${github_repo_name}/check-runs |
| 105 | + |
| 106 | + |
| 107 | +echo "Deployment is available at: '${deployment_url}'" |
0 commit comments