Skip to content

Commit b9df94c

Browse files
committed
haskellfoundation#23 Deploy site for each git branch
1 parent f1134af commit b9df94c

File tree

3 files changed

+134
-17
lines changed

3 files changed

+134
-17
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs.
2+
# More: https://editorconfig.org/
3+
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
charset = utf-8
12+
end_of_line = lf
13+
14+
[Makefile]
15+
indent_style = tab

.github/workflows/deploy.sh

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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}'"

.github/workflows/main.yml

+12-17
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ name: CI
66
on:
77
# Triggers the workflow on push or pull request events but only for the hakyll branch
88
push:
9-
branches: [ hakyll ]
9+
# branches:
10+
branches-ignore:
11+
- gh-pages
1012
pull_request:
11-
branches: [ hakyll ]
13+
branches-ignore:
14+
- gh-pages
1215

1316
# Allows you to run this workflow manually from the Actions tab
1417
workflow_dispatch:
@@ -48,21 +51,13 @@ jobs:
4851
- name: Build dependencies
4952
run: stack build --system-ghc --only-dependencies
5053

51-
- name: Build site executable
52-
run: stack build --system-ghc
54+
- name: Build
55+
run: |
56+
stack build --system-ghc
57+
stack exec --system-ghc site build
5358
54-
# Runs a set of commands using the runners shell
5559
- name: Deploy
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5662
run: |
57-
git config user.name github-actions
58-
git config user.email [email protected]
59-
stack exec --system-ghc site rebuild
60-
git checkout main
61-
git pull --rebase
62-
# Overwrite existing files with new files
63-
cp -a -v _site/. .
64-
# Commit
65-
git add --all
66-
git commit -m "[`date '+%F %T %Z'`] New release [ci skip]"
67-
# Push
68-
git push origin main:main
63+
./.github/workflows/deploy.sh

0 commit comments

Comments
 (0)