Add deploy preview workflow #1
This file contains hidden or 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 Preview | ||
| on: | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| types: [opened, synchronize, reopened] | ||
| concurrency: | ||
| group: deploy-preview-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
| permissions: | ||
| contents: read | ||
| pages: write | ||
| id-token: write | ||
| pull-requests: write | ||
| deployments: write | ||
| jobs: | ||
| build-preview: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
| - name: Install Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: '20' | ||
| - name: Install Antora | ||
| run: npm install antora | ||
| - name: Generate Site | ||
| run: npx antora antora-playbook.yml | ||
| - name: Post-process paths for VC context/schema docs | ||
| run: | | ||
| if [ -d "build/site/identity/1.1/_attachments/ica" ]; then | ||
| mv build/site/identity/1.1/_attachments/ica build/site/identity/1.1/ica | ||
| fi | ||
| - name: Disable Jekyll | ||
| run: touch build/site/.nojekyll | ||
| - name: Upload Preview Artifact | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: preview-site-${{ github.event.pull_request.number }} | ||
| path: ./build/site | ||
| retention-days: 30 | ||
| deploy-preview: | ||
| needs: build-preview | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: preview-pr-${{ github.event.pull_request.number }} | ||
| url: ${{ steps.deploy.outputs.page_url }} | ||
| steps: | ||
| - name: Download Preview Artifact | ||
| uses: actions/download-artifact@v4 | ||
| with: | ||
| name: preview-site-${{ github.event.pull_request.number }} | ||
| path: ./site | ||
| - name: Setup Pages | ||
| uses: actions/configure-pages@v4 | ||
| - name: Upload Pages Artifact | ||
| uses: actions/upload-pages-artifact@v3 | ||
| with: | ||
| path: ./site | ||
| - name: Deploy to GitHub Pages | ||
| id: deploy | ||
| uses: actions/deploy-pages@v4 | ||
| - name: Comment on PR | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const deploymentUrl = '${{ steps.deploy.outputs.page_url }}'; | ||
| const prNumber = context.issue.number; | ||
| // Find existing preview comment | ||
| const comments = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| }); | ||
| const existingComment = comments.data.find(comment => | ||
| comment.body.includes('🚀 Deploy Preview') | ||
| ); | ||
| const commentBody = `🚀 **Deploy Preview** for PR #${prNumber} is ready! | ||
| **Preview URL:** ${deploymentUrl} | ||
| This preview will be updated automatically when you push new changes to this PR. | ||
| --- | ||
| *Deployed from commit: ${context.sha}*`; | ||
| if (existingComment) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existingComment.id, | ||
| body: commentBody | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| body: commentBody | ||
| }); | ||
| } | ||
| cleanup-on-close: | ||
| runs-on: ubuntu-latest | ||
| if: github.event.action == 'closed' | ||
| steps: | ||
| - name: Delete deployment environment | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const prNumber = context.issue.number; | ||
| const environmentName = `preview-pr-${prNumber}`; | ||
| try { | ||
| // Get the environment | ||
| const environment = await github.rest.repos.getEnvironment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| environment_name: environmentName | ||
| }); | ||
| // Delete the environment | ||
| await github.rest.repos.deleteAnEnvironment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| environment_name: environmentName | ||
| }); | ||
| console.log(`Environment ${environmentName} deleted successfully`); | ||
| } catch (error) { | ||
| console.log(`Environment ${environmentName} not found or already deleted`); | ||
| } | ||