|
| 1 | +--- |
| 2 | +title: "Preview branches" |
| 3 | +description: "Create isolated environments for each branch of your code, allowing you to test changes before merging to production. You can create preview branches manually or automatically from your git branches." |
| 4 | +--- |
| 5 | + |
| 6 | +import UpgradeToV4Note from "/snippets/upgrade-to-v4-note.mdx"; |
| 7 | + |
| 8 | +<UpgradeToV4Note /> |
| 9 | + |
| 10 | +## How to use preview branches |
| 11 | + |
| 12 | +The preview environment is special – you create branches from it. The branches you create live under the preview environment and have all the features you're used to from other environments (like staging or production). That means you can trigger runs, have schedules, test them, use Realtime, etc. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +We recommend you automatically create a preview branch for each git branch when a Pull Request is opened and then archive it automatically when the PR is merged/closed. |
| 17 | + |
| 18 | +The process to use preview branches looks like this: |
| 19 | + |
| 20 | +1. Create a preview branch |
| 21 | +2. Deploy to the preview branch (1+ times) |
| 22 | +3. Trigger runs using your Preview API key (`TRIGGER_SECRET_KEY`) and the branch name (`TRIGGER_PREVIEW_BRANCH`). |
| 23 | +4. Archive the preview branch when the branch is done. |
| 24 | + |
| 25 | +There are two main ways to do this: |
| 26 | + |
| 27 | +1. Automatically: using GitHub Actions (recommended). |
| 28 | +2. Manually: in the dashboard and/or using the CLI. |
| 29 | + |
| 30 | +### Limits on active preview branches |
| 31 | + |
| 32 | +We restrict the number of active preview branches (per project). You can archive a preview branch at any time (automatically or manually) to unlock another slot – or you can upgrade your plan. |
| 33 | + |
| 34 | +Once archived you can still view the dashboard for the branch but you can't trigger or execute runs (or other write operations). |
| 35 | + |
| 36 | +This limit exists because each branch has an independent concurrency limit. For the Cloud product these are the limits: |
| 37 | + |
| 38 | +| Plan | Active preview branches | |
| 39 | +| ----- | ----------------------- | |
| 40 | +| Free | 0 | |
| 41 | +| Hobby | 5 | |
| 42 | +| Pro | 20 (then paid for more) | |
| 43 | + |
| 44 | +For full details see our [pricing page](https://trigger.dev/pricing). |
| 45 | + |
| 46 | +## Triggering runs and using the SDK |
| 47 | + |
| 48 | +Before we talk about how to deploy to preview branches, one important thing to understand is that you must set the `TRIGGER_PREVIEW_BRANCH` environment variable as well as the `TRIGGER_SECRET_KEY` environment variable. |
| 49 | + |
| 50 | +When deploying to somewhere that supports `process.env` (like Node.js runtimes) you can just set the environment variables: |
| 51 | + |
| 52 | +```bash |
| 53 | +TRIGGER_SECRET_KEY="tr_preview_1234567890" |
| 54 | +TRIGGER_PREVIEW_BRANCH="your-branch-name" |
| 55 | +``` |
| 56 | + |
| 57 | +If you're deploying somewhere that doesn't support `process.env` (like some edge runtimes) you can manually configure the SDK: |
| 58 | + |
| 59 | +```ts |
| 60 | +import { configure } from "@trigger.dev/sdk"; |
| 61 | +import { myTask } from "./trigger/myTasks"; |
| 62 | + |
| 63 | +configure({ |
| 64 | + secretKey: "tr_preview_1234567890", // WARNING: Never actually hardcode your secret key like this |
| 65 | + previewBranch: "your-branch-name", |
| 66 | +}); |
| 67 | + |
| 68 | +async function triggerTask() { |
| 69 | + await myTask.trigger({ userId: "1234" }); // Trigger a run in your-branch-name |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Preview branches with GitHub Actions (recommended) |
| 74 | + |
| 75 | +This GitHub Action will: |
| 76 | + |
| 77 | +1. Automatically create a preview branch for your Pull Request (if the branch doesn't already exist). |
| 78 | +2. Deploy the preview branch. |
| 79 | +3. Archive the preview branch when the Pull Request is merged/closed. |
| 80 | + |
| 81 | +```yml .github/workflows/trigger-preview-branches.yml |
| 82 | +name: Deploy to Trigger.dev (preview branches) |
| 83 | + |
| 84 | +on: |
| 85 | + pull_request: |
| 86 | + types: [opened, synchronize, reopened, closed] |
| 87 | + |
| 88 | +jobs: |
| 89 | + deploy-preview: |
| 90 | + runs-on: ubuntu-latest |
| 91 | + steps: |
| 92 | + - uses: actions/checkout@v4 |
| 93 | + |
| 94 | + - name: Use Node.js 20.x |
| 95 | + uses: actions/setup-node@v4 |
| 96 | + with: |
| 97 | + node-version: "20.x" |
| 98 | + |
| 99 | + - name: Install dependencies |
| 100 | + run: npm install |
| 101 | + |
| 102 | + - name: Deploy preview branch |
| 103 | + run: npx trigger.dev@v4-beta deploy --env preview |
| 104 | + env: |
| 105 | + TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_ACCESS_TOKEN }} |
| 106 | +``` |
| 107 | +
|
| 108 | +For this workflow to work, you need to set the following secrets in your GitHub repository: |
| 109 | +
|
| 110 | +- `TRIGGER_ACCESS_TOKEN`: A Trigger.dev personal access token (they start with `tr_pat_`). [Learn how to create one and set it in GitHub](/github-actions#creating-a-personal-access-token). |
| 111 | + |
| 112 | +Notice that the deploy command has `--env preview` at the end. We automatically detect the preview branch from the GitHub actions env var. |
| 113 | + |
| 114 | +You can manually specify the branch using `--branch <branch-name>` in the deploy command, but this isn't required. |
| 115 | + |
| 116 | +## Preview branches with the CLI (manual) |
| 117 | + |
| 118 | +### Deploying a preview branch |
| 119 | + |
| 120 | +Creating and deploying a preview branch manually is easy: |
| 121 | + |
| 122 | +```bash |
| 123 | +npx trigger.dev@v4-beta deploy --env preview |
| 124 | +``` |
| 125 | + |
| 126 | +This will create and deploy a preview branch, automatically detecting the git branch. If for some reason the auto-detection doesn't work it will let you know and tell you do this: |
| 127 | + |
| 128 | +```bash |
| 129 | +npx trigger.dev@v4-beta deploy --env preview --branch your-branch-name |
| 130 | +``` |
| 131 | + |
| 132 | +### Archiving a preview branch |
| 133 | + |
| 134 | +You can manually archive a preview branch with the CLI: |
| 135 | + |
| 136 | +```bash |
| 137 | +npx trigger.dev@v4-beta preview archive |
| 138 | +``` |
| 139 | + |
| 140 | +Again we will try auto-detect the current branch. But you can specify the branch name with `--branch <branch-name>`. |
| 141 | + |
| 142 | +## Creating and archiving preview branches from the dashboard |
| 143 | + |
| 144 | +From the "Preview branches" page you can create a branch: |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +You can also archive a branch: |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +## Environment variables |
| 154 | + |
| 155 | +You can set environment variables for "Preview" and they will get applied to all branches (existing and new). You can also set environment variables for a specific branch. If they are set for both then the branch-specific variables will take precedence. |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +These can be set manually in the dashboard, or automatically at deploy time using the [syncEnvVars()](/config/extensions/syncEnvVars) or [syncVercelEnvVars()](/config/extensions/syncEnvVars#syncvercelenvvars) build extensions. |
| 160 | + |
| 161 | +### Sync environment variables |
| 162 | + |
| 163 | +Full instructions are in the [syncEnvVars()](/config/extensions/syncEnvVars) documentation. |
| 164 | + |
| 165 | +```ts trigger.config.ts |
| 166 | +import { defineConfig } from "@trigger.dev/sdk"; |
| 167 | +// You will need to install the @trigger.dev/build package |
| 168 | +import { syncEnvVars } from "@trigger.dev/build/extensions/core"; |
| 169 | +
|
| 170 | +export default defineConfig({ |
| 171 | + //... other config |
| 172 | + build: { |
| 173 | + // This will automatically detect and sync environment variables |
| 174 | + extensions: [ |
| 175 | + syncEnvVars(async (ctx) => { |
| 176 | + // You can fetch env variables from a 3rd party service like Infisical, Hashicorp Vault, etc. |
| 177 | + // The ctx.branch will be set if it's a preview deployment. |
| 178 | + return await fetchEnvVars(ctx.environment, ctx.branch); |
| 179 | + }), |
| 180 | + ], |
| 181 | + }, |
| 182 | +}); |
| 183 | +``` |
| 184 | + |
| 185 | +### Sync Vercel environment variables |
| 186 | + |
| 187 | +You need to set the `VERCEL_ACCESS_TOKEN`, `VERCEL_PROJECT_ID` and `VERCEL_TEAM_ID` environment variables. You can find these in the Vercel dashboard. Full instructions are in the [syncVercelEnvVars()](/config/extensions/syncEnvVars#syncvercelenvvars) documentation. |
| 188 | + |
| 189 | +The extension will automatically detect a preview branch deploy from Vercel and sync the appropriate environment variables. |
| 190 | + |
| 191 | +```ts trigger.config.ts |
| 192 | +import { defineConfig } from "@trigger.dev/sdk"; |
| 193 | +// You will need to install the @trigger.dev/build package |
| 194 | +import { syncVercelEnvVars } from "@trigger.dev/build/extensions/core"; |
| 195 | +
|
| 196 | +export default defineConfig({ |
| 197 | + //... other config |
| 198 | + build: { |
| 199 | + // This will automatically detect and sync environment variables |
| 200 | + extensions: [syncVercelEnvVars()], |
| 201 | + }, |
| 202 | +}); |
| 203 | +``` |
0 commit comments