|
| 1 | +name: Deploy PR Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + |
| 7 | +# Cancel previous runs when new commits are pushed to the PR |
| 8 | +concurrency: |
| 9 | + group: pr-preview-${{ github.event.pull_request.number }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +jobs: |
| 13 | + deploy-preview: |
| 14 | + name: Deploy PR Preview |
| 15 | + runs-on: ubuntu-latest |
| 16 | + if: github.event.action != 'closed' |
| 17 | + permissions: |
| 18 | + pull-requests: write |
| 19 | + deployments: write |
| 20 | + contents: read |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout PR code |
| 24 | + uses: actions/checkout@v6 |
| 25 | + with: |
| 26 | + ref: ${{ github.event.pull_request.head.sha }} |
| 27 | + |
| 28 | + - name: Setup Node.js |
| 29 | + uses: actions/setup-node@v6 |
| 30 | + with: |
| 31 | + node-version: '22' |
| 32 | + cache: 'npm' |
| 33 | + cache-dependency-path: 'package-lock.json' |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: | |
| 37 | + echo "Installing dependencies..." |
| 38 | + npm ci |
| 39 | +
|
| 40 | + - name: Build Docusaurus site |
| 41 | + env: |
| 42 | + DOCUSAURUS_BASE_URL: /pr-${{ github.event.pull_request.number }} |
| 43 | + run: | |
| 44 | + echo "Building site with base URL: $DOCUSAURUS_BASE_URL" |
| 45 | + npm run build |
| 46 | +
|
| 47 | + - name: Upload build artifact for local testing |
| 48 | + uses: actions/upload-artifact@v4 |
| 49 | + with: |
| 50 | + name: pr-${{ github.event.pull_request.number }}-build |
| 51 | + path: build/ |
| 52 | + retention-days: 30 |
| 53 | + |
| 54 | + - name: Prepare deployment directory |
| 55 | + run: | |
| 56 | + PR_DIR="pr-${{ github.event.pull_request.number }}" |
| 57 | + echo "Creating directory: $PR_DIR" |
| 58 | + mkdir -p "$PR_DIR" |
| 59 | +
|
| 60 | + # Create config.yaml |
| 61 | + cat > "$PR_DIR/config.yaml" << 'EOF' |
| 62 | + static: |
| 63 | + files: 'build/**' |
| 64 | + urlPath: 'pr-${{ github.event.pull_request.number }}' |
| 65 | + extensions: ['html'] |
| 66 | + index: true |
| 67 | + EOF |
| 68 | +
|
| 69 | + # Copy build directory |
| 70 | + cp -r build "$PR_DIR/" |
| 71 | +
|
| 72 | + echo "Contents of $PR_DIR:" |
| 73 | + ls -la "$PR_DIR" |
| 74 | +
|
| 75 | + - name: Install HarperDB CLI |
| 76 | + run: | |
| 77 | + npm install -g harperdb |
| 78 | +
|
| 79 | + - name: Deploy to HarperDB |
| 80 | + env: |
| 81 | + HARPER_PREVIEW_TARGET: ${{ secrets.HARPER_PREVIEW_TARGET }} |
| 82 | + HARPER_PREVIEW_USERNAME: ${{ secrets.HARPER_PREVIEW_USERNAME }} |
| 83 | + HARPER_PREVIEW_PASSWORD: ${{ secrets.HARPER_PREVIEW_PASSWORD }} |
| 84 | + run: | |
| 85 | + cd "pr-${{ github.event.pull_request.number }}" |
| 86 | + # Add your additional arguments here |
| 87 | + harper deploy \ |
| 88 | + target=${HARPER_PREVIEW_TARGET}:9925 \ |
| 89 | + username=$HARPER_PREVIEW_USERNAME \ |
| 90 | + password=$HARPER_PREVIEW_PASSWORD \ |
| 91 | + project=pr-${{ github.event.pull_request.number }} \ |
| 92 | + restart=true \ |
| 93 | + replicated=true |
| 94 | +
|
| 95 | + - name: Create deployment |
| 96 | + env: |
| 97 | + HARPER_PREVIEW_TARGET: ${{ secrets.HARPER_PREVIEW_TARGET }} |
| 98 | + uses: actions/github-script@v8 |
| 99 | + with: |
| 100 | + script: | |
| 101 | + const prNumber = context.payload.pull_request.number; |
| 102 | + const target = process.env.HARPER_PREVIEW_TARGET; |
| 103 | + const deploymentUrl = `${target}/pr-${prNumber}`; |
| 104 | +
|
| 105 | + // Create deployment |
| 106 | + const deployment = await github.rest.repos.createDeployment({ |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + ref: context.payload.pull_request.head.sha, |
| 110 | + environment: `pr-${prNumber}`, |
| 111 | + description: `Preview deployment for PR #${prNumber}`, |
| 112 | + auto_merge: false, |
| 113 | + required_contexts: [] |
| 114 | + }); |
| 115 | +
|
| 116 | + // Create deployment status |
| 117 | + await github.rest.repos.createDeploymentStatus({ |
| 118 | + owner: context.repo.owner, |
| 119 | + repo: context.repo.repo, |
| 120 | + deployment_id: deployment.data.id, |
| 121 | + state: 'success', |
| 122 | + environment_url: deploymentUrl, |
| 123 | + description: 'Preview deployment successful' |
| 124 | + }); |
| 125 | +
|
| 126 | + // Also add a comment to the PR |
| 127 | + await github.rest.issues.createComment({ |
| 128 | + owner: context.repo.owner, |
| 129 | + repo: context.repo.repo, |
| 130 | + issue_number: prNumber, |
| 131 | + body: `## 🚀 Preview Deployment\n\nYour preview deployment is ready!\n\n🔗 **Preview URL:** ${deploymentUrl}\n\nThis preview will update automatically when you push new commits.` |
| 132 | + }); |
| 133 | +
|
| 134 | + cleanup-preview: |
| 135 | + name: Cleanup PR Preview |
| 136 | + runs-on: ubuntu-latest |
| 137 | + if: github.event.action == 'closed' |
| 138 | + permissions: |
| 139 | + pull-requests: write |
| 140 | + deployments: write |
| 141 | + contents: read |
| 142 | + |
| 143 | + steps: |
| 144 | + - name: Checkout code |
| 145 | + uses: actions/checkout@v6 |
| 146 | + |
| 147 | + - name: Setup Node.js |
| 148 | + uses: actions/setup-node@v6 |
| 149 | + with: |
| 150 | + node-version: '22' |
| 151 | + cache: 'npm' |
| 152 | + cache-dependency-path: 'package-lock.json' |
| 153 | + |
| 154 | + - name: Install HarperDB CLI |
| 155 | + run: | |
| 156 | + npm install -g harperdb |
| 157 | +
|
| 158 | + - name: Remove preview deployment |
| 159 | + env: |
| 160 | + HARPER_PREVIEW_TARGET: ${{ secrets.HARPER_PREVIEW_TARGET }} |
| 161 | + HARPER_PREVIEW_USERNAME: ${{ secrets.HARPER_PREVIEW_USERNAME }} |
| 162 | + HARPER_PREVIEW_PASSWORD: ${{ secrets.HARPER_PREVIEW_PASSWORD }} |
| 163 | + run: | |
| 164 | + # Add your cleanup command here (e.g., harper undeploy or similar) |
| 165 | + harper drop_component \ |
| 166 | + target=${HARPER_PREVIEW_TARGET}:9925 \ |
| 167 | + username=$HARPER_PREVIEW_USERNAME \ |
| 168 | + password=$HARPER_PREVIEW_PASSWORD \ |
| 169 | + project=pr-${{ github.event.pull_request.number }} \ |
| 170 | + replicated=true \ |
| 171 | + restart=true |
| 172 | + echo "Cleaning up preview for PR #${{ github.event.pull_request.number }}" |
| 173 | +
|
| 174 | + - name: Update deployment status |
| 175 | + uses: actions/github-script@v7 |
| 176 | + with: |
| 177 | + script: | |
| 178 | + const prNumber = context.payload.pull_request.number; |
| 179 | +
|
| 180 | + // Mark deployment as inactive |
| 181 | + const deployments = await github.rest.repos.listDeployments({ |
| 182 | + owner: context.repo.owner, |
| 183 | + repo: context.repo.repo, |
| 184 | + environment: `pr-${prNumber}` |
| 185 | + }); |
| 186 | +
|
| 187 | + for (const deployment of deployments.data) { |
| 188 | + await github.rest.repos.createDeploymentStatus({ |
| 189 | + owner: context.repo.owner, |
| 190 | + repo: context.repo.repo, |
| 191 | + deployment_id: deployment.id, |
| 192 | + state: 'inactive', |
| 193 | + description: 'Preview deployment removed' |
| 194 | + }); |
| 195 | + } |
| 196 | +
|
| 197 | + // Add cleanup comment to PR |
| 198 | + await github.rest.issues.createComment({ |
| 199 | + owner: context.repo.owner, |
| 200 | + repo: context.repo.repo, |
| 201 | + issue_number: prNumber, |
| 202 | + body: `## 🧹 Preview Cleanup\n\nThe preview deployment for this PR has been removed.` |
| 203 | + }); |
0 commit comments