|
| 1 | +name: Deploy PR Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - synchronize |
| 9 | + - closed |
| 10 | + |
| 11 | +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + pages: write |
| 15 | + id-token: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +# Allow only one concurrent deployment per PR |
| 19 | +concurrency: |
| 20 | + group: pr-preview-${{ github.event.pull_request.number }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + # Build job |
| 25 | + build: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + # Don't run on closed PRs |
| 28 | + if: github.event.action != 'closed' |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Setup Node.js |
| 35 | + uses: actions/setup-node@v3 |
| 36 | + with: |
| 37 | + node-version: '22.9.0' |
| 38 | + |
| 39 | + - name: Setup Yarn Corepack |
| 40 | + run: corepack enable |
| 41 | + |
| 42 | + - name: Install dependencies |
| 43 | + run: yarn install |
| 44 | + |
| 45 | + - name: Build Storybook |
| 46 | + run: yarn build-storybook |
| 47 | + |
| 48 | + # Verify the build output |
| 49 | + - name: Verify build output |
| 50 | + run: | |
| 51 | + echo "Checking build output directory..." |
| 52 | + ls -la apps/docs/storybook-static |
| 53 | + echo "Checking for index.html..." |
| 54 | + if [ -f apps/docs/storybook-static/index.html ]; then |
| 55 | + echo "index.html exists" |
| 56 | + else |
| 57 | + echo "index.html does not exist" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +
|
| 61 | + # Add a comment to the PR with the preview URL |
| 62 | + - name: Comment PR |
| 63 | + uses: actions/github-script@v6 |
| 64 | + with: |
| 65 | + script: | |
| 66 | + const previewUrl = `https://lambda-curry.github.io/medusa-forms/pr-${context.issue.number}/`; |
| 67 | + const commentBody = `📝 **Storybook Preview**: [View Storybook](${previewUrl}) |
| 68 | +
|
| 69 | + This preview will be updated automatically when you push new changes to this PR. |
| 70 | + |
| 71 | + > Note: The preview will be available after the workflow completes and the PR is approved for deployment.`; |
| 72 | + |
| 73 | + // Get existing comments |
| 74 | + const comments = await github.rest.issues.listComments({ |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + issue_number: context.issue.number, |
| 78 | + }); |
| 79 | + |
| 80 | + // Check if we already have a comment |
| 81 | + const botComment = comments.data.find(comment => |
| 82 | + comment.user.login === 'github-actions[bot]' && |
| 83 | + comment.body.includes('Storybook Preview') |
| 84 | + ); |
| 85 | + |
| 86 | + if (botComment) { |
| 87 | + // Update existing comment |
| 88 | + await github.rest.issues.updateComment({ |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: context.repo.repo, |
| 91 | + comment_id: botComment.id, |
| 92 | + body: commentBody |
| 93 | + }); |
| 94 | + } else { |
| 95 | + // Create new comment |
| 96 | + await github.rest.issues.createComment({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + issue_number: context.issue.number, |
| 100 | + body: commentBody |
| 101 | + }); |
| 102 | + } |
| 103 | +
|
| 104 | + # Create PR-specific directory structure |
| 105 | + - name: Create PR-specific directory |
| 106 | + run: | |
| 107 | + mkdir -p pr-preview/pr-${{ github.event.pull_request.number }} |
| 108 | + cp -r apps/docs/storybook-static/* pr-preview/pr-${{ github.event.pull_request.number }}/ |
| 109 | +
|
| 110 | + # Upload the artifact for the deployment job |
| 111 | + - name: Upload artifact |
| 112 | + uses: actions/upload-pages-artifact@v3 |
| 113 | + with: |
| 114 | + path: pr-preview |
| 115 | + retention-days: 30 |
| 116 | + |
| 117 | + # Deploy job |
| 118 | + deploy: |
| 119 | + needs: build |
| 120 | + runs-on: ubuntu-latest |
| 121 | + if: github.event.action != 'closed' |
| 122 | + |
| 123 | + # Use a specific environment with protection rules |
| 124 | + # This ensures only approved PRs can deploy |
| 125 | + environment: |
| 126 | + name: pr-preview |
| 127 | + url: ${{ steps.deployment.outputs.page_url }} |
| 128 | + |
| 129 | + steps: |
| 130 | + - name: Setup Pages |
| 131 | + uses: actions/configure-pages@v5 |
| 132 | + |
| 133 | + - name: Deploy to GitHub Pages |
| 134 | + id: deployment |
| 135 | + uses: actions/deploy-pages@v4 |
| 136 | + |
| 137 | + # Clean up when PR is closed |
| 138 | + cleanup: |
| 139 | + runs-on: ubuntu-latest |
| 140 | + if: github.event.action == 'closed' |
| 141 | + permissions: |
| 142 | + contents: write |
| 143 | + |
| 144 | + steps: |
| 145 | + - name: Checkout |
| 146 | + uses: actions/checkout@v4 |
| 147 | + with: |
| 148 | + ref: gh-pages |
| 149 | + |
| 150 | + - name: Delete PR Preview |
| 151 | + run: | |
| 152 | + PR_NUMBER="${{ github.event.pull_request.number }}" |
| 153 | + PR_PREVIEW_PATH="pr-preview/pr-$PR_NUMBER" |
| 154 | + |
| 155 | + if [ -d "$PR_PREVIEW_PATH" ]; then |
| 156 | + echo "Removing PR preview at $PR_PREVIEW_PATH" |
| 157 | + rm -rf "$PR_PREVIEW_PATH" |
| 158 | + |
| 159 | + # Commit and push the changes |
| 160 | + git config user.name "GitHub Actions" |
| 161 | + git config user.email "actions@github.com" |
| 162 | + git add -A |
| 163 | + git commit -m "Remove PR preview for PR #$PR_NUMBER" || echo "No changes to commit" |
| 164 | + git push |
| 165 | + else |
| 166 | + echo "PR preview directory not found" |
| 167 | + fi |
| 168 | +
|
0 commit comments