Skip to content

Commit 54e83cd

Browse files
committed
haskellfoundation#23 Make a site deployment per branch
1 parent ccad054 commit 54e83cd

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

.github/workflows/deploy.sh

+28-39
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ slugify() {
1717
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
1818
}
1919

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
20+
git_branch=$(git branch --show-current)
2521
echo "Current git branch is '${git_branch}'."
2622

2723
git config user.name github-actions
@@ -44,7 +40,7 @@ if [ "$git_branch" == "$main_git_branch" ]; then
4440
# Restore temporary backup for other branches content.
4541
mv ./branches "${gh_pages_root}/"
4642
else
47-
site_dest="${gh_pages_root}/branches/${git_branch}"
43+
site_dest="${gh_pages_root}/branches/$(slugify ${git_branch})"
4844

4945
# Replace site files.
5046
rm -rf "${site_dest}"
@@ -58,12 +54,6 @@ git commit --allow-empty -m "[$(date '+%F %T %Z')] Updated site for the '${git_b
5854
git push --force origin gh-pages
5955
echo "Deployment finished."
6056

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-
6757
github_project_url=$(git remote get-url origin)
6858
if [[ $github_project_url == [email protected]:* ]]; then
6959
github_repo=$(echo ${github_project_url#"[email protected]:"} | sed 's/\.git$//g')
@@ -73,33 +63,32 @@ fi
7363

7464
github_repo_owner=$(echo "${github_repo}" | sed 's/\/.*$//g')
7565
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
8366

84-
update_github_deployment_req_data() {
85-
cat <<EOF
86-
{
87-
"name": "deployment",
88-
"ref": "${git_branch}",
89-
"environment": "github-pages",
90-
"payload": "some payload https://xyz.abc",
91-
"description": "some description https://abc.xyz"
67+
update_deployments_list() {
68+
deployments_list="DEPLOYMENTS.md"
69+
echo "Updating ${deployments_list}"
70+
rm $deployments_list
71+
72+
touch $deployments_list
73+
echo "# Deployments" >>$deployments_list
74+
echo "" >>$deployments_list
75+
echo "| Branch | Link |" >>$deployments_list
76+
echo "| --- | --- |" >>$deployments_list
77+
78+
main_deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/"
79+
echo "| [main](https://github.com/${github_repo_owner}/${github_repo_name}/tree/${branch}) | [Open](${main_deployment_url}) |"
80+
81+
remote_branches=$(git ls-remote --heads origin | sed 's?.*refs/heads/??')
82+
echo "$remote_branches" | while IFS= read -r branch; do
83+
safe_branch=$(slugify $branch)
84+
deployment_url="https://${github_repo_owner}.github.io/${github_repo_name}/branches/${safe_branch}"
85+
echo "| [${branch}](https://github.com/${github_repo_owner}/${github_repo_name}/tree/${branch}) | [Open](${deployment_url}) |"
86+
done
87+
88+
# Update gh-pages branch
89+
git add --all
90+
git commit --allow-empty -m "Update ${deployments_list}"
91+
git push --force origin gh-pages
9292
}
93-
EOF
94-
}
95-
96-
curl -i \
97-
--header "Authorization: Bearer ${GITHUB_TOKEN}" \
98-
--header "Accept: application/vnd.github.v3+json" \
99-
--header "User-Agent: github-actions" \
100-
--header "Content-Type: application/json" \
101-
--data "$(update_github_deployment_req_data)" \
102-
--fail \
103-
https://api.github.com/repos/${github_repo_owner}/${github_repo_name}/deployments
10493

105-
echo "Deployment is available at: '${deployment_url}'"
94+
update_deployments_list

.github/workflows/main.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,4 @@ jobs:
5757
stack exec --system-ghc site build
5858
5959
- name: Deploy
60-
env:
61-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62-
run: |
63-
./.github/workflows/deploy.sh
60+
run: ./.github/workflows/deploy.sh

0 commit comments

Comments
 (0)