Build and Deploy #14
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
name: Build and Deploy | |
on: | |
schedule: | |
- cron: '0 */6 * * *' # Runs every 6 hours | |
push: | |
branches: | |
- main | |
workflow_dispatch: # Allows manual trigger | |
jobs: | |
build_and_deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # Fetch full history to support rebasing | |
- name: Set up SSH for Git (deploy key) | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519 | |
chmod 600 ~/.ssh/id_ed25519 | |
ssh-keyscan github.com >> ~/.ssh/known_hosts | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Install Dependencies | |
run: | | |
python3 -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Generate Merged Data | |
run: | | |
python3 gh-data.py | |
python3 merge-data.py | |
- name: Switch to `gh-pages` and Preserve Changes | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
# Stash changes to preserve them | |
git stash --include-untracked | |
# Fetch and switch to `gh-pages` | |
git fetch origin gh-pages:gh-pages || git checkout --orphan gh-pages | |
git checkout gh-pages | |
# Rebase `gh-pages` onto `main` to ensure a linear history | |
git rebase main | |
# Apply stashed changes | |
git stash pop || echo "No changes to apply." | |
- name: Commit and Push Updates | |
run: | | |
# Add changes | |
git add -f merged-data.json | |
git add . | |
# Commit changes if there are any | |
if git diff --cached --quiet; then | |
echo "No changes to commit." | |
else | |
git commit -m "Update GitHub Pages with latest data" | |
fi | |
# Push changes back to `gh-pages` | |
git remote set-url origin [email protected]:${{ github.repository }}.git | |
git push origin gh-pages |