Deploy to GitHub Pages #29937
This file contains hidden or 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
| name: Deploy to GitHub Pages | |
| on: | |
| schedule: | |
| - cron: "*/5 * * * *" | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| fetch-depth: 0 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.x" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Run dict.py | |
| run: python dict.py | |
| - name: Run main.py | |
| run: python main.py | |
| - name: Check if gh-pages branch exists | |
| id: check-branch | |
| run: | | |
| if git ls-remote --heads origin gh-pages | grep -q gh-pages; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Set up Git identity | |
| run: | | |
| git config --global user.email "actions@github.com" | |
| git config --global user.name "GitHub Actions" | |
| - name: Create gh-pages branch if it doesn't exist | |
| if: steps.check-branch.outputs.exists == 'false' | |
| run: | | |
| git checkout --orphan gh-pages | |
| git rm -rf . | |
| git commit --allow-empty -m "Initial commit for gh-pages - $(date '+%Y-%m-%d %H:%M:%S')" | |
| git push origin gh-pages | |
| git checkout main | |
| - name: Checkout gh-pages branch | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| path: gh-pages | |
| fetch-depth: 0 | |
| - name: Backup pr-preview directory | |
| run: | | |
| if [ -d "gh-pages/pr-preview" ]; then | |
| mv gh-pages/pr-preview /tmp/pr-preview-backup | |
| fi | |
| - name: Copy files to gh-pages branch | |
| run: | | |
| # Only remove files that are in web-page directory | |
| for file in $(ls web-page/); do | |
| if [ -e "gh-pages/$file" ]; then | |
| rm -rf "gh-pages/$file" | |
| fi | |
| done | |
| cp -r web-page/* gh-pages/ | |
| - name: Restore pr-preview directory | |
| run: | | |
| if [ -d "/tmp/pr-preview-backup" ]; then | |
| mkdir -p gh-pages/pr-preview | |
| mv /tmp/pr-preview-backup/* gh-pages/pr-preview/ | |
| fi | |
| - name: Commit and push changes | |
| run: | | |
| cd gh-pages | |
| git add . | |
| if git diff --cached --quiet; then | |
| echo "No changes to commit" | |
| else | |
| git commit -m "Update website content" | |
| git push origin gh-pages | |
| fi | |
| git remote set-url origin https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git | |
| git push |