Each pull request gets its own VM. The workflow runs your app there and comments the preview address on the pull request.
Before you start
Section titled “Before you start”- 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_KEYin step 1. - Repository permission to push a branch and open a pull request.
1. Add the key as a secret
Section titled “1. Add the key as a secret”- Open the repository’s Settings in GitHub.
- Select Secrets and variables.
- Select Actions.
- Select New repository secret.
- Enter
SI_API_KEYas the name. - Paste your organization key as the value.
- Select Add secret.
2. Add the workflow
Section titled “2. Add the workflow”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.
3. Push a branch
Section titled “3. Push a branch”Push the workflow on a branch, then open a pull request from it.
git checkout -b preview-workflowgit add .github/workflows/preview.ymlgit commit -m "Add preview workflow"git push -u origin preview-workflow4. Read the comment
Section titled “4. Read the comment”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.
Check it works
Section titled “Check it works”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.
If it doesn’t work
Section titled “If it doesn’t work”| 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.
Clean up
Section titled “Clean up”Closing the pull request deletes the VM. To delete it by hand, run si rm pr-<number>.
Forks and public previews
Section titled “Forks and public previews”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.
- Open pull requests: push a branch from a VM and open the pull request from the agent.
- Services: the program that keeps your app running in the VM.
- Web addresses: the access states of a port.