Skip to content

Commit

Permalink
Add automatic pages update workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitmishra786 committed Nov 2, 2024
1 parent d8e352a commit 70d6f66
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/update-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# .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)

0 comments on commit 70d6f66

Please sign in to comment.