| 1 | # .github/workflows/preview.yml |
| 2 | name: PR Preview |
| 3 | |
| 4 | on: |
| 5 | pull_request: |
| 6 | types: [opened, synchronize] |
| 7 | |
| 8 | jobs: |
| 9 | provision-preview: |
| 10 | runs-on: ubuntu-latest |
| 11 | steps: |
| 12 | - name: Provision sandbox |
| 13 | run: | |
| 14 | RESPONSE=$(curl -s -X POST https://api.modbox.run/sandboxes/provision \ |
| 15 | -H "Authorization: Bearer ${{ secrets.MODBOX_API_TOKEN }}" \ |
| 16 | -H "Content-Type: application/json" \ |
| 17 | -d '{ |
| 18 | "task_id": "pr-${{ github.event.pull_request.number }}", |
| 19 | "image_id": "${{ vars.DEV_IMAGE_ID }}", |
| 20 | "ttl_seconds": 86400, |
| 21 | "env_vars": { |
| 22 | "GIT_BRANCH": "${{ github.head_ref }}", |
| 23 | "PR_NUMBER": "${{ github.event.pull_request.number }}" |
| 24 | } |
| 25 | }') |
| 26 | SANDBOX_ID=$(echo $RESPONSE | jq -r '.sandbox_id') |
| 27 | echo "SANDBOX_ID=$SANDBOX_ID" >> $GITHUB_ENV |
| 28 | |
| 29 | - name: Wait for ready |
| 30 | run: | |
| 31 | curl -s -X POST https://api.modbox.run/sandboxes/wait \ |
| 32 | -H "Authorization: Bearer ${{ secrets.MODBOX_API_TOKEN }}" \ |
| 33 | -H "Content-Type: application/json" \ |
| 34 | -d '{"task_id": "pr-${{ github.event.pull_request.number }}", "timeout": 120}' |
| 35 | |
| 36 | - name: Get sandbox URL |
| 37 | run: | |
| 38 | SANDBOX=$(curl -s https://api.modbox.run/sandboxes/detail/$SANDBOX_ID \ |
| 39 | -H "Authorization: Bearer ${{ secrets.MODBOX_API_TOKEN }}") |
| 40 | URL=$(echo $SANDBOX | jq -r '.sandbox_url') |
| 41 | echo "Preview ready: $URL" >> $GITHUB_STEP_SUMMARY |
| 42 | |
| 43 | - name: Comment on PR |
| 44 | uses: actions/github-script@v7 |
| 45 | with: |
| 46 | script: | |
| 47 | github.rest.issues.createComment({ |
| 48 | issue_number: context.issue.number, |
| 49 | owner: context.repo.owner, |
| 50 | repo: context.repo.repo, |
| 51 | body: '**Preview environment ready!**\n\nYour sandbox is live and accessible. It will auto-destroy after 24 hours.' |
| 52 | }) |