create pr environment workflow #2
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: Deploy to GitHub Pages | |
on: | |
push: | |
branches: ['*'] | |
pull_request: | |
branches: ['*'] | |
permissions: | |
contents: write | |
pages: write | |
id-token: write | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Install Dependencies | |
run: npm ci | |
- name: Build | |
run: npm run build | |
- name: Prepare preview directory | |
run: | | |
# Get branch name or PR number | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
DEPLOY_PATH="pr-${{ github.event.number }}" | |
else | |
# Replace slashes with dashes in branch name | |
DEPLOY_PATH="${GITHUB_REF_NAME//\//-}" | |
fi | |
# Create preview directory structure | |
mkdir -p "preview-out/${DEPLOY_PATH}" | |
cp -r dist/* "preview-out/${DEPLOY_PATH}/" | |
- name: Deploy to gh-pages | |
uses: peaceiris/actions-gh-pages@v3 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
publish_dir: preview-out | |
keep_files: true | |
publish_branch: gh-pages | |
- name: Comment PR | |
if: github.event_name == 'pull_request' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const prNumber = context.payload.pull_request.number; | |
const repoName = context.repo.repo; | |
const owner = context.repo.owner; | |
const previewUrl = `https://${owner}.github.io/${repoName}/pr-${prNumber}/`; | |
const comment = `🚀 Preview deployment is ready!\n\nBuilt with commit ${context.sha}\n\n[Preview Link](${previewUrl})`; | |
github.rest.issues.createComment({ | |
owner: owner, | |
repo: repoName, | |
issue_number: prNumber, | |
body: comment | |
}); |