Skip to content

Commit b7d0aa3

Browse files
committed
Fix deploy workflow to fetch and rebase
The workflow fails to push changes to gh-pages because it has commits that are not in the local branch.
1 parent 90f36a0 commit b7d0aa3

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

.github/workflows/build-and-deploy.yml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
steps:
1616
- name: Check out the repository
1717
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0 # Fetch full history to support rebasing
1820

1921
- name: Set up SSH for Git (deploy key)
2022
run: |
@@ -33,22 +35,42 @@ jobs:
3335
python3 -m pip install --upgrade pip
3436
pip install -r requirements.txt
3537
36-
- name: Fetch GitHub data
38+
- name: Generate Merged Data
3739
run: |
3840
python3 gh-data.py
39-
40-
- name: Merge data sources
41-
run: |
4241
python3 merge-data.py
4342
44-
- name: Commit and Push Changes to `gh-pages`
43+
- name: Switch to `gh-pages` and Preserve Changes
4544
run: |
4645
git config --global user.name "github-actions[bot]"
4746
git config --global user.email "github-actions[bot]@users.noreply.github.com"
48-
git checkout -b gh-pages
47+
48+
# Stash changes to preserve them
49+
git stash --include-untracked
50+
51+
# Fetch and switch to `gh-pages`
52+
git fetch origin gh-pages:gh-pages || git checkout --orphan gh-pages
53+
git checkout gh-pages
54+
55+
# Rebase `gh-pages` onto `main` to ensure a linear history
56+
git rebase main
57+
58+
# Apply stashed changes
59+
git stash pop || echo "No changes to apply."
60+
61+
- name: Commit and Push Updates
62+
run: |
63+
# Add changes
4964
git add -f merged-data.json
5065
git add .
51-
# If there are no changes, no commit gets created, and nothing is pushed
52-
git commit -m "Update GitHub Pages with latest data"
66+
67+
# Commit changes if there are any
68+
if git diff --cached --quiet; then
69+
echo "No changes to commit."
70+
else
71+
git commit -m "Update GitHub Pages with latest data"
72+
fi
73+
74+
# Push changes back to `gh-pages`
5375
git remote set-url origin [email protected]:${{ github.repository }}.git
5476
git push origin gh-pages

0 commit comments

Comments
 (0)