Skip to content

Commit ebaebfc

Browse files
committed
Handle branch deletion
1 parent e685013 commit ebaebfc

File tree

5 files changed

+59
-31
lines changed

5 files changed

+59
-31
lines changed

.github/workflows/branch-deleted.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Branch Deleted
2+
3+
on: delete
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
with:
12+
fetch-depth: 0
13+
ref: gh-pages
14+
15+
- name: Remove stale deployments
16+
run: ./.github/workflows/deployment/delete.sh $GITHUB_REF_NAME

.github/workflows/main.yml renamed to .github/workflows/build.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
# This is a basic workflow to help you get started with Actions
1+
name: Build
22

3-
name: CI
4-
5-
# Controls when the workflow will run
63
on:
7-
# Triggers the workflow on push or pull request events but only for the hakyll branch
84
push:
9-
# branches:
105
branches-ignore:
116
- gh-pages
127
pull_request:
@@ -18,17 +13,14 @@ on:
1813

1914
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
2015
jobs:
21-
# This workflow contains a single job called "build"
2216
build:
23-
# The type of runner that the job will run on
2417
runs-on: ubuntu-latest
2518

2619
strategy:
2720
matrix:
2821
stack: ["2.7.3"]
2922
ghc: ["8.10.7"]
3023

31-
# Steps represent a sequence of tasks that will be executed as part of the job
3224
steps:
3325
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
3426
- uses: actions/checkout@v2
@@ -56,4 +48,4 @@ jobs:
5648
stack exec --system-ghc site rebuild
5749
5850
- name: Deploy site
59-
run: ./.github/workflows/deploy.sh
51+
run: ./.github/workflows/deployment/deploy.sh
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
export repo_root_dir=$(git rev-parse --show-toplevel)
4+
export site_src="${repo_root_dir}/_site"
5+
export gh_pages_dir="${repo_root_dir}/docs"
6+
export deployments_dir="${gh_pages_dir}/branches"
7+
8+
# Site built from the main branch will be available at 'https://<domain_name>/'.
9+
# Sites built from other branchs will be available at 'https://<domain_name>/branches/<branch_name>'.
10+
export main_git_branch="hakyll"
11+
12+
# replace "/", "#", etc with "-".
13+
slugify() {
14+
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
15+
}
16+
export -f slugify
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Remove deployment by specified git branch name.
4+
# Script assumes that you are on gh-pages branch.
5+
6+
set -eo pipefail
7+
8+
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
9+
source $script_dir/commons.sh
10+
11+
branch_slug=$($script_dir/slugify.sh $1)
12+
13+
rm -rf "$deployments_dir/$branch_slug"

.github/workflows/deploy.sh renamed to .github/workflows/deployment/deploy.sh

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@
44

55
set -eo pipefail
66

7-
git_repo_root=$(git rev-parse --show-toplevel)
8-
site_src="${git_repo_root}/_site"
9-
gh_pages_root="${git_repo_root}/docs"
7+
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
8+
source $script_dir/commons.sh
109

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-
}
10+
git config user.name github-actions
11+
git config user.email [email protected]
1912

2013
deploy() {
2114
if [[ ! -z "$GITHUB_REF_NAME" ]]; then
@@ -26,27 +19,25 @@ deploy() {
2619
fi
2720
echo "Current git branch is '${git_branch}'."
2821

29-
git config user.name github-actions
30-
git config user.email [email protected]
31-
3222
git checkout gh-pages
3323
git pull origin gh-pages
3424

3525
if [ "$git_branch" == "$main_git_branch" ]; then
36-
site_dest="${gh_pages_root}"
26+
site_dest="${gh_pages_dir}"
3727

3828
# Create temporary backup for other branches content.
39-
mv "${gh_pages_root}/branches" .
29+
mv "${deployments_dir}" .
4030

4131
# Replace site files.
4232
rm -rf "${site_dest}"
4333
mkdir -p "${site_dest}"
4434
cp -a -v ${site_src}/* ${site_dest}/
4535

4636
# Restore temporary backup for other branches content.
47-
mv ./branches "${gh_pages_root}/"
37+
mv ./branches "${gh_pages_dir}/"
4838
else
49-
site_dest="${gh_pages_root}/branches/$(slugify ${git_branch})"
39+
branch_slug=$(slugify $git_branch)
40+
site_dest="${gh_pages_dir}/branches/${branch_slug}"
5041

5142
# Replace site files.
5243
rm -rf "${site_dest}"
@@ -86,10 +77,10 @@ update_deployments_list() {
8677
main_deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/"
8778
echo "| [${main_git_branch}](https://github.com/${github_repo_owner}/${github_repo_name}/tree/${branch}) | [Open](${main_deployment_url}) |" >>$deployments_list
8879

89-
remote_branches=$( git ls-remote --heads origin | sed 's?.*refs/heads/??' | grep -v "gh-pages" | grep -v "${main_git_branch}")
80+
remote_branches=$(git ls-remote --heads origin | sed 's?.*refs/heads/??' | grep -v "gh-pages" | grep -v "${main_git_branch}")
9081
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}"
82+
branch_slug=$(slugify $branch)
83+
deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/branches/${branch_slug}"
9384
echo "| [${branch}](https://github.com/${github_repo_owner}/${github_repo_name}/tree/${branch}) | [Open](${deployment_url}) |" >>$deployments_list
9485
done
9586

0 commit comments

Comments
 (0)