-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (142 loc) · 5.96 KB
/
github-pages-deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
name: Deploy in Github Pages
on:
push:
paths:
- 'published/**'
- 'index.css'
- 'common/**/*.md'
permissions:
contents: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Get modified slide directories
run: |
# Get the list of modified files
modified_files=$(git show ${{ github.sha }} --name-only)
echo "Modified files: $modified_files"
# Check if index.css is modified
if echo "$modified_files" | grep -q "^index.css$"; then
echo "index_css_modified=true" >> $GITHUB_ENV
else
echo "index_css_modified=false" >> $GITHUB_ENV
fi
# Check if any markdown files in the common directory are modified
if echo "$modified_files" | grep -q "^common/.*\.md$"; then
echo "common_md_modified=true" >> $GITHUB_ENV
else
echo "common_md_modified=false" >> $GITHUB_ENV
fi
# Get modified published directories
published_dirs=$(echo "$modified_files" | grep "^published/.*/.*\.\(md\|vue\|ts\|tsx\|js\|jsx\|html\|css\)$" | xargs -I {} dirname {} | sort | uniq)
echo "Modified slide directories: $published_dirs"
# Temporary file to store directories
temp_file=$(mktemp)
# Loop through modified files and find the closest parent directory with a markdown file
for dir in $published_dirs; do
while [ "$dir" != "." ]; do
if ls "$dir"/*.md >/dev/null 2>&1; then
echo "$dir" >> "$temp_file"
break
fi
dir=$(dirname "$dir")
done
done
echo "Directories with markdown files: $(cat $temp_file)"
# Remove duplicates and store the result in modified_dirs
modified_dirs=$(sort "$temp_file" | uniq | tr '\n' ' ')
# Clean up temporary file
rm "$temp_file"
echo "Unique modified directories: $modified_dirs"
# Output the modified directories to be used as an environment variable
echo "modified_dirs=${modified_dirs}" >> $GITHUB_ENV
- name: Create build directory
run: |
mkdir -p build
if [ "${{ env.common_md_modified }}" = "true" ]; then
# Create folders for all slide projects if common markdown modified
find published -type f -name "slides.md" | while read -r file; do
dir=$(dirname "$file")
mkdir -p build/$(echo "$dir" | awk -F'published/' '{print $2}')
done
else
# Only create folders for modified directories
for dir in ${{ env.modified_dirs }}; do
mkdir -p build/$(echo "$dir" | awk -F'published/' '{print $2}')
done
fi
- name: Build modified slides
if: env.modified_dirs != '' || env.common_md_modified == 'true'
run: |
if [ "${{ env.common_md_modified }}" = "true" ]; then
# Rebuild all slides if common markdown modified
find published -type f -name "slides.md" | while read -r file; do
dir=$(dirname "$file")
npm run build -- --base /lectures/$(echo "$dir" | awk -F'published/' '{print $2}')/ $file
mv $dir/dist/* build/$(echo "$dir" | awk -F'published/' '{print $2}') -v
done
else
# Only build modified slides
for dir in ${{ env.modified_dirs }}; do
npm run build -- --base /lectures/$(echo "$dir" | awk -F'published/' '{print $2}')/ $dir/slides.md
mv $dir/dist/* build/$(echo "$dir" | awk -F'published/' '{print $2}') -v
done
fi
- name: Build index.html
if: env.index_css_modified == 'true' || env.modified_dirs != '' || env.common_md_modified == 'true'
run: |
if [[ -f index.css ]]; then
mv index.css build/index.css
fi
# Temporary file to store links
temp_links_file=$(mktemp)
# Find all directories containing a slides.md file and extract their relative paths
find published -type f -name "slides.md" | while read -r file; do
dir=$(dirname "$file")
slide_path=$(echo "$dir" | sed 's|published/||')
echo "$slide_path" >> "$temp_links_file"
done
# Sort the links lexicographically and remove duplicates
sorted_links=$(sort "$temp_links_file" | uniq)
echo "Sorted links: $sorted_links"
# Begin writing the new index.html in the lectures directory
{
echo '<!DOCTYPE html>'
echo '<html lang="en">'
echo '<head>'
echo ' <meta charset="UTF-8">'
echo ' <meta name="viewport" content="width=device-width, initial-scale=1.0">'
echo ' <link rel="stylesheet" href="./index.css">'
echo ' <title>Lectures</title>'
echo '</head>'
echo '<body class="container">'
echo ' <h1>Lecture Presentations</h1>'
echo ' <ul>'
# Add sorted links to index.html
for link in $sorted_links; do
echo " <li><a href=\"$link/\">$link</a></li>"
done
echo ' </ul>'
echo '</body>'
echo '</html>'
} > build/index.html
# Clean up temporary files
rm "$temp_links_file"
- name: Deploy slides 🚀
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages # The branch the action should deploy to.
folder: build # The folder the action should deploy.
target-folder: ''
clean: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}