Skip to content

Deploy PR-Preview

Deploy PR-Preview #90

Workflow file for this run

name: Deploy PR-Preview
on:
workflow_run:
workflows: ["Build PR-Preview"]
types:
- completed
permissions:
contents: write
pages: write
jobs:
deploy:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: user
path: .
github-token: ${{github.token}}
repository: ${{github.repository}}
run-id: ${{github.event.workflow_run.id}}
- name: Get user
id: get_user
run: |
USER=$(cat user.txt)
echo "USER=$USER" >> $GITHUB_ENV
- name: Get Pull Request Number
id: get_pr_number
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls?head=${{env.USER}}:${{ github.event.workflow_run.head_branch }}" \
| jq -r '.[0].number')
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
- name: Check PR Approval Status
id: approval_check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=${{ env.PR_NUMBER }}
RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews")
if ! echo "$RESPONSE" | jq . > /dev/null 2>&1; then
echo "Error: Invalid JSON response from GitHub API."
exit 1
fi
LATEST_REVIEW=$(echo "$RESPONSE" | jq 'sort_by(.submitted_at) | last')
STATE=$(echo "$LATEST_REVIEW" | jq -r '.state')
AUTHOR_ASSOCIATION=$(echo "$LATEST_REVIEW" | jq -r '.author_association')
echo "Latest review state: $STATE"
echo "Author association: $AUTHOR_ASSOCIATION"
if [ "$STATE" != "APPROVED" ] || { [ "$AUTHOR_ASSOCIATION" != "COLLABORATOR" ] && [ "$AUTHOR_ASSOCIATION" != "OWNER" ]; }; then
echo "The latest review is not an approved review from a collaborator or owner. Exiting."
exit 1
fi
- uses: actions/download-artifact@v4
if: success()
with:
name: github-pages
path: build/
github-token: ${{github.token}}
repository: ${{github.repository}}
run-id: ${{github.event.workflow_run.id}}
- name: Deploy to GitHub Pages
if: success()
uses: crazy-max/ghaction-github-pages@v2
with:
target_branch: gh-deploy
build_dir: build/
commit_message: "Deploy to Github Pages"
jekyll: false
keep_history: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}