Skip to content

Commit 270a275

Browse files
authored
Add pages action (#3311)
* Add script to handle updates to the docs folder * Create gh-pages.yml * Update on-pages.sh
1 parent 01d9345 commit 270a275

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

.github/scripts/on-pages.sh

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#/bin/bash
2+
set -e
3+
4+
function get_file_size(){
5+
local file="$1"
6+
if [[ "$OSTYPE" == "darwin"* ]]; then
7+
eval `stat -s "$file"`
8+
local res="$?"
9+
echo "$st_size"
10+
return $res
11+
else
12+
stat --printf="%s" "$file"
13+
return $?
14+
fi
15+
}
16+
17+
#git_remove_from_pages <file>
18+
function git_remove_from_pages(){
19+
local path=$1
20+
local info=`curl -s -k -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.object+json" -X GET "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path?ref=gh-pages"`
21+
local type=`echo "$info" | jq -r '.type'`
22+
if [ ! $type == "file" ]; then
23+
if [ ! $type == "null" ]; then
24+
echo "Wrong type '$type'"
25+
else
26+
echo "File is not on Pages"
27+
fi
28+
return 0
29+
fi
30+
local sha=`echo "$info" | jq -r '.sha'`
31+
local message="Deleting "$(basename $path)
32+
local json="{\"branch\":\"gh-pages\",\"message\":\"$message\",\"sha\":\"$sha\"}"
33+
echo "$json" | curl -s -k -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.raw+json" -X DELETE --data @- "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path"
34+
}
35+
36+
function git_upload_to_pages(){
37+
local path=$1
38+
local src=$2
39+
40+
if [ ! -f "$src" ]; then
41+
>&2 echo "Input is not a file! Aborting..."
42+
return 1
43+
fi
44+
45+
local info=`curl -s -k -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.object+json" -X GET "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path?ref=gh-pages"`
46+
local type=`echo "$info" | jq -r '.type'`
47+
local message=$(basename $path)
48+
local sha=""
49+
local content=""
50+
51+
if [ $type == "file" ]; then
52+
sha=`echo "$info" | jq -r '.sha'`
53+
sha=",\"sha\":\"$sha\""
54+
message="Updating $message"
55+
elif [ ! $type == "null" ]; then
56+
>&2 echo "Wrong type '$type'"
57+
return 1
58+
else
59+
message="Creating $message"
60+
fi
61+
62+
content=`base64 -i "$src"`
63+
data="{\"branch\":\"gh-pages\",\"message\":\"$message\",\"content\":\"$content\"$sha}"
64+
65+
echo "$data" | curl -s -k -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3.raw+json" -X PUT --data @- "https://api.github.com/repos/$GITHUB_REPOSITORY/contents/$path"
66+
}
67+
68+
function git_safe_upload_to_pages(){
69+
local path=$1
70+
local file="$2"
71+
local name=$(basename "$file")
72+
local size=`get_file_size "$file"`
73+
local upload_res=`git_upload_to_pages "$path" "$file"`
74+
if [ $? -ne 0 ]; then
75+
>&2 echo "ERROR: Failed to upload '$name' ($?)"
76+
return 1
77+
fi
78+
up_size=`echo "$upload_res" | jq -r '.content.size'`
79+
if [ $up_size -ne $size ]; then
80+
>&2 echo "ERROR: Uploaded size does not match! $up_size != $size"
81+
#git_delete_asset
82+
return 1
83+
fi
84+
echo "$upload_res" | jq -r '.content.download_url'
85+
return $?
86+
}
87+
88+
EVENT_JSON=`cat $GITHUB_EVENT_PATH`
89+
90+
pages_added=`echo "$EVENT_JSON" | jq -r '.commits[].added[]'`
91+
pages_modified=`echo "$EVENT_JSON" | jq -r '.commits[].modified[]'`
92+
pages_removed=`echo "$EVENT_JSON" | jq -r '.commits[].removed[]'`
93+
94+
for page in $pages_added; do
95+
if [[ $page != "README.md" && $page != "docs/"* ]]; then
96+
continue
97+
fi
98+
echo "Adding '$page' to pages ..."
99+
if [[ $page == "README.md" ]]; then
100+
git_safe_upload_to_pages "index.md" "README.md"
101+
else
102+
git_safe_upload_to_pages "$page" "$page"
103+
fi
104+
done
105+
106+
for page in $pages_modified; do
107+
if [[ $page != "README.md" && $page != "docs/"* ]]; then
108+
continue
109+
fi
110+
echo "Modifying '$page' ..."
111+
if [[ $page == "README.md" ]]; then
112+
git_safe_upload_to_pages "index.md" "README.md"
113+
else
114+
git_safe_upload_to_pages "$page" "$page"
115+
fi
116+
done
117+
118+
for page in $pages_removed; do
119+
if [[ $page != "README.md" && $page != "docs/"* ]]; then
120+
continue
121+
fi
122+
echo "Removing '$page' from pages ..."
123+
if [[ $page == "README.md" ]]; then
124+
git_remove_from_pages "README.md" > /dev/null
125+
else
126+
git_remove_from_pages "$page" > /dev/null
127+
fi
128+
done
129+
130+
echo
131+
echo "DONE!"

.github/workflows/gh-pages.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: GitHub Pages CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- 'README.md'
9+
- 'docs/**'
10+
11+
jobs:
12+
13+
build-pages:
14+
name: Build GitHub Pages
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v1
18+
- name: Copy Files
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
run: bash ./.github/scripts/on-pages.sh

0 commit comments

Comments
 (0)