Skip to content

Commit f02456b

Browse files
committed
Fix preview deployment permissions by using peaceiris/actions-gh-pages
1 parent 852d754 commit f02456b

1 file changed

Lines changed: 25 additions & 50 deletions

File tree

.github/workflows/preview-deploy.yml

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,13 @@ jobs:
5151
env:
5252
JEKYLL_ENV: production
5353

54-
- name: Deploy to preview branch
55-
run: |
56-
# Configure git
57-
git config --global user.name "github-actions[bot]"
58-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
59-
60-
# Clone the repository to a temporary directory
61-
git clone https://github.com/${{ github.repository }}.git temp-repo
62-
cd temp-repo
63-
64-
# Check if gh-pages branch exists, create if not
65-
if git ls-remote --exit-code --heads origin gh-pages; then
66-
git checkout gh-pages
67-
else
68-
git checkout --orphan gh-pages
69-
git rm -rf .
70-
fi
71-
72-
# Create preview directory structure
73-
mkdir -p preview/${{ steps.branch-info.outputs.branch-name }}
74-
75-
# Copy built site to preview directory
76-
cp -r ../_site/* preview/${{ steps.branch-info.outputs.branch-name }}/
77-
78-
# Add and commit
79-
git add .
80-
git commit -m "Deploy preview for ${{ steps.branch-info.outputs.branch-name }}" || echo "No changes to commit"
81-
82-
# Push to gh-pages
83-
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git gh-pages
54+
- name: Deploy to GitHub Pages preview
55+
uses: peaceiris/actions-gh-pages@v3
56+
with:
57+
github_token: ${{ secrets.GITHUB_TOKEN }}
58+
publish_dir: ./_site
59+
destination_dir: preview/${{ steps.branch-info.outputs.branch-name }}
60+
keep_files: true
8461

8562
- name: Comment on PR with preview URL
8663
if: github.event_name == 'pull_request'
@@ -106,25 +83,23 @@ jobs:
10683
runs-on: ubuntu-latest
10784
if: github.event_name == 'pull_request' && github.event.action == 'closed'
10885
steps:
109-
- name: Cleanup preview on PR close
110-
run: |
111-
# Configure git
112-
git config --global user.name "github-actions[bot]"
113-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
114-
115-
# Clone the repository
116-
git clone https://github.com/${{ github.repository }}.git temp-repo
117-
cd temp-repo
86+
- name: Checkout
87+
uses: actions/checkout@v4
88+
with:
89+
ref: gh-pages
11890

119-
# Switch to gh-pages branch
120-
if git ls-remote --exit-code --heads origin gh-pages; then
121-
git checkout gh-pages
122-
123-
# Remove the preview directory
124-
if [ -d "preview/pr-${{ github.event.number }}" ]; then
125-
rm -rf preview/pr-${{ github.event.number }}
126-
git add .
127-
git commit -m "Cleanup preview for closed PR #${{ github.event.number }}" || echo "No changes to commit"
128-
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git gh-pages
129-
fi
91+
- name: Remove preview directory
92+
run: |
93+
if [ -d "preview/pr-${{ github.event.number }}" ]; then
94+
rm -rf preview/pr-${{ github.event.number }}
95+
echo "Removed preview directory for PR #${{ github.event.number }}"
96+
else
97+
echo "Preview directory for PR #${{ github.event.number }} not found"
13098
fi
99+
100+
- name: Deploy cleanup
101+
uses: peaceiris/actions-gh-pages@v3
102+
with:
103+
github_token: ${{ secrets.GITHUB_TOKEN }}
104+
publish_dir: .
105+
keep_files: true

0 commit comments

Comments
 (0)