Update Pages List #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .github/workflows/update-pages.yml | |
name: Update Pages List | |
on: | |
push: | |
branches: [ main ] | |
paths: | |
- '**.md' | |
workflow_dispatch: | |
permissions: | |
contents: write | |
pages: write | |
jobs: | |
update-pages: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
ref: main | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Update index.html | |
run: | | |
python3 - <<EOF | |
import os | |
import re | |
# Get all markdown files | |
md_files = [f for f in os.listdir('.') if f.endswith('.md')] | |
# Sort files alphabetically, but ensure README.md is first | |
md_files.sort() | |
if 'README.md' in md_files: | |
md_files.remove('README.md') | |
md_files.insert(0, 'README.md') | |
# Read the current index.html | |
with open('index.html', 'r') as f: | |
content = f.read() | |
# Create the new pages array string | |
pages_array = "const pages = [\n '" + "',\n '".join(md_files) + "'\n ];" | |
# Replace the existing pages array in index.html | |
new_content = re.sub( | |
r'const pages = \[[\s\S]*?\];', | |
pages_array, | |
content | |
) | |
# Write the updated content back to index.html | |
with open('index.html', 'w') as f: | |
f.write(new_content) | |
EOF | |
- name: Commit and push if changed | |
run: | | |
git config --global user.name 'GitHub Action' | |
git config --global user.email '[email protected]' | |
git add index.html | |
git diff --quiet && git diff --staged --quiet || (git commit -m "Update pages list" && git push) | |
- name: Checkout gh-pages | |
uses: actions/checkout@v3 | |
with: | |
ref: gh-pages | |
clean: false | |
- name: Copy updated index.html to gh-pages | |
run: | | |
git checkout main -- index.html | |
git config --global user.name 'GitHub Action' | |
git config --global user.email '[email protected]' | |
git add index.html | |
git diff --quiet && git diff --staged --quiet || (git commit -m "Update pages list" && git push) |