Skip to content

Each pull request gets its own VM. The workflow runs your app there and comments the preview address on the pull request.

  • A GitHub repository with an app that starts with one command. A public repository needs nothing else. For a private one, connect GitHub first, as in Open pull requests.
  • An organization key, saved as the repository secret SI_API_KEY in step 1.
  • Repository permission to push a branch and open a pull request.
  1. Open the repository’s Settings in GitHub.
  2. Select Secrets and variables.
  3. Select Actions.
  4. Select New repository secret.
  5. Enter SI_API_KEY as the name.
  6. Paste your organization key as the value.
  7. Select Add secret.

Save this as .github/workflows/preview.yml. Replace npm ci and npm start with your app’s install and start commands.

name: preview
on:
pull_request:
types: [opened, synchronize, reopened, closed]
permissions:
contents: read
pull-requests: write
jobs:
preview:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
env:
SI_API_KEY: ${{ secrets.SI_API_KEY }}
PR: ${{ github.event.pull_request.number }}
steps:
- name: Install si
run: |
curl -fsSL https://slashinfra.com/install | sh
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Create or reuse the VM
run: |
si create --name "pr-$PR" --ttl 72h --if-absent --json > vm.json
echo "VM_ID=$(jq -r .id vm.json)" >> "$GITHUB_ENV"
- name: Fetch the pull request head
run: |
if ! si exec "$VM_ID" -- test -d /home/user/app/.git; then
si exec "$VM_ID" -- git clone "https://github.com/${GITHUB_REPOSITORY}.git" /home/user/app
fi
si exec "$VM_ID" -- git -C /home/user/app fetch origin "pull/${PR}/head:pr-${PR}"
si exec "$VM_ID" -- git -C /home/user/app checkout -f "pr-${PR}"
si exec "$VM_ID" -- sh -lc 'cd /home/user/app && npm ci'
- name: Start the app
run: si svm svc "$VM_ID" add web --port 3000 --dir /home/user/app -- npm start || si svm svc "$VM_ID" restart web
- name: Open the port
run: |
si port open "$VM_ID" 3000 --public
echo "PREVIEW=https://3000-${VM_ID}.slashinfra.dev" >> "$GITHUB_ENV"
- name: Comment the preview address
env:
GH_TOKEN: ${{ github.token }}
run: gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body "Preview: $PREVIEW"
cleanup:
if: github.event.action == 'closed'
runs-on: ubuntu-latest
env:
SI_API_KEY: ${{ secrets.SI_API_KEY }}
steps:
- name: Install si
run: |
curl -fsSL https://slashinfra.com/install | sh
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Delete the VM
run: si rm "pr-${{ github.event.pull_request.number }}"

--if-absent makes a new push reuse the VM of the pull request instead of failing. A reused VM keeps its original expiry, so the next push after 72 hours creates a new one. The --json reply gives the VM id that the later steps use.

Push the workflow on a branch, then open a pull request from it.

Terminal window
git checkout -b preview-workflow
git add .github/workflows/preview.yml
git commit -m "Add preview workflow"
git push -u origin preview-workflow

GitHub runs the preview job for the pull request. When it finishes, the pull request has a comment with the address, Preview: https://3000-<id>.slashinfra.dev. Open it in your browser.

The address shows your app. The first load can take a moment, because the first request wakes the VM. Closing the pull request runs the cleanup job, which deletes the VM.

Symptom Cause Fix
Repository not found at git clone The repository is private, and the VM has no GitHub credential. Connect GitHub as in Open pull requests.
503 with nothing listens on port 3000 in this computer The app did not start. Run si svm svc "$VM_ID" logs web from a terminal with the VM id, and fix the start command in the workflow.

Scroll horizontally to see all columns.

Closing the pull request deletes the VM. To delete it by hand, run si rm pr-<number>.

A pull request from a fork does not receive the repository secret, so it does not create a VM. Keep pull_request as the trigger. Do not use pull_request_target with this secret.

--public lets anyone with the address reach the app. For an app that only reviewers should open, drop --public so the port is authenticated, and comment a link instead: si port url "$VM_ID" 3000 --ttl 24h prints an address that expires.