Skip to content

Commit d6dd2c9

Browse files
authored
Preview branches docs (#2130)
* Draft preview branches docs * More wip on docs * Explain what the GH action does * Added manual deploy/archive instructions * Added dashboard section to the docs * More wip on the preview branch docs * Added preview/branch to the CLI deploy docs * Add preview branch note to triggering docs * Added preview branch notes to the API keys docs * Added preview stuff to the Context docs * CLI preview archive command * Add a better explanation and details about env vars * Fix for weird reference to the dashboard
1 parent 01e7ef3 commit d6dd2c9

13 files changed

+330
-9
lines changed

docs/apikeys.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ Each environment has its own secret key. You can find the value on the API keys
1111

1212
![How to find your secret key](/images/api-keys.png)
1313

14+
<Note>
15+
For preview branches, you need to also set the `TRIGGER_PREVIEW_BRANCH` environment variable as
16+
well. You can find the value on the API keys page when you're on the preview branch.
17+
</Note>
18+
1419
### Automatically Configuring the SDK
1520

1621
To automatically configure the SDK with your secret key, you can set the `TRIGGER_SECRET_KEY` environment variable. The SDK will automatically use this value when calling API methods (like `trigger`).
1722

1823
```bash .env
1924
TRIGGER_SECRET_KEY="tr_dev_…"
25+
TRIGGER_PREVIEW_BRANCH="my-branch" # Only needed for preview branches
2026
```
2127

2228
You can do the same if you are self-hosting and need to change the default URL by using `TRIGGER_API_URL`.
2329

2430
```bash .env
2531
TRIGGER_API_URL="https://trigger.example.com"
32+
TRIGGER_PREVIEW_BRANCH="my-branch" # Only needed for preview branches
2633
```
2734

2835
The default URL is `https://api.trigger.dev`.
@@ -37,6 +44,7 @@ import { myTask } from "./trigger/myTasks";
3744

3845
configure({
3946
secretKey: "tr_dev_1234", // WARNING: Never actually hardcode your secret key like this
47+
previewBranch: "my-branch", // Only needed for preview branches
4048
baseURL: "https://mytrigger.example.com", // Optional
4149
});
4250

docs/cli-preview-archive.mdx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: "CLI preview archive command"
3+
sidebarTitle: "preview archive"
4+
description: "The `trigger.dev preview archive` command can be used to archive a preview branch."
5+
---
6+
7+
import UpgradeToV4Note from "/snippets/upgrade-to-v4-note.mdx";
8+
import ProjectPathArg from "/snippets/cli-args-project-path.mdx";
9+
import CommonOptions from "/snippets/cli-options-common.mdx";
10+
import ProjectRefOption from "/snippets/cli-options-project-ref.mdx";
11+
import EnvFileOption from "/snippets/cli-options-env-file.mdx";
12+
import ConfigFileOption from "/snippets/cli-options-config-file.mdx";
13+
import SkipUpdateCheckOption from "/snippets/cli-options-skip-update-check.mdx";
14+
import BranchOption from "/snippets/cli-options-branch.mdx";
15+
16+
<UpgradeToV4Note />
17+
18+
Run the command like this:
19+
20+
<CodeGroup>
21+
22+
```bash npm
23+
npx trigger.dev@v4-beta preview archive
24+
```
25+
26+
```bash pnpm
27+
pnpm dlx trigger.dev@v4-beta preview archive
28+
```
29+
30+
```bash yarn
31+
yarn dlx trigger.dev@v4-beta preview archive
32+
```
33+
34+
</CodeGroup>
35+
36+
It will archive the preview branch, automatically detecting the branch name from git. You can manually specify the branch using the `--branch` option.
37+
38+
## Arguments
39+
40+
```
41+
npx trigger.dev@v4-beta preview archive [path]
42+
```
43+
44+
<ProjectPathArg />
45+
46+
## Options
47+
48+
<BranchOption />
49+
50+
<ConfigFileOption />
51+
52+
<ProjectRefOption />
53+
54+
<EnvFileOption />
55+
56+
<SkipUpdateCheckOption />
57+
58+
### Common options
59+
60+
These options are available on most commands.
61+
62+
<CommonOptions />

docs/context.mdx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ title: "Context"
33
description: "Get the context of a task run."
44
---
55

6-
Context (`ctx`) is a way to get information about a run.
6+
Context (`ctx`) is a way to get information about a run.
77

88
<Note>
9-
The context object does not change whilst your code is executing. This means values like `ctx.run.durationMs` will be fixed at the moment the `run()` function is called.
9+
The context object does not change whilst your code is executing. This means values like
10+
`ctx.run.durationMs` will be fixed at the moment the `run()` function is called.
1011
</Note>
1112

1213
<RequestExample>
@@ -17,7 +18,6 @@ import { task } from "@trigger.dev/sdk/v3";
1718
export const parentTask = task({
1819
id: "parent-task",
1920
run: async (payload: { message: string }, { ctx }) => {
20-
2121
if (ctx.environment.type === "DEVELOPMENT") {
2222
return;
2323
}
@@ -32,7 +32,8 @@ export const parentTask = task({
3232
<ResponseField name="task" type="object">
3333
<Expandable title="properties" defaultOpen={true}>
3434
<ResponseField name="exportName" type="string">
35-
The exported function name of the task e.g. `myTask` if you defined it like this: `export const myTask = task(...)`.
35+
The exported function name of the task e.g. `myTask` if you defined it like this: `export
36+
const myTask = task(...)`.
3637
</ResponseField>
3738
<ResponseField name="id" type="string">
3839
The ID of the task.
@@ -93,13 +94,16 @@ export const parentTask = task({
9394
The [maximum number of attempts](/triggering#maxattempts) allowed for this task run.
9495
</ResponseField>
9596
<ResponseField name="durationMs" type="number">
96-
The duration of the task run in milliseconds when the `run()` function is called. For live values use the [usage SDK functions](/run-usage).
97+
The duration of the task run in milliseconds when the `run()` function is called. For live
98+
values use the [usage SDK functions](/run-usage).
9799
</ResponseField>
98100
<ResponseField name="costInCents" type="number">
99-
The cost of the task run in cents when the `run()` function is called. For live values use the [usage SDK functions](/run-usage).
101+
The cost of the task run in cents when the `run()` function is called. For live values use the
102+
[usage SDK functions](/run-usage).
100103
</ResponseField>
101104
<ResponseField name="baseCostInCents" type="number">
102-
The base cost of the task run in cents when the `run()` function is called. For live values use the [usage SDK functions](/run-usage).
105+
The base cost of the task run in cents when the `run()` function is called. For live values
106+
use the [usage SDK functions](/run-usage).
103107
</ResponseField>
104108
<ResponseField name="version" type="string" optional>
105109
The [version](/versioning) of the task run.
@@ -132,6 +136,40 @@ export const parentTask = task({
132136
<ResponseField name="type" type="string">
133137
The type of the environment (PRODUCTION, STAGING, DEVELOPMENT, or PREVIEW).
134138
</ResponseField>
139+
<ResponseField name="branchName" type="string" optional>
140+
If the environment is `PREVIEW` then this will be the branch name.
141+
</ResponseField>
142+
<ResponseField name="git" type="object">
143+
<Expandable title="properties">
144+
<ResponseField name="commitAuthorName" type="string" optional>
145+
The name of the commit author.
146+
</ResponseField>
147+
<ResponseField name="commitMessage" type="string" optional>
148+
The message of the commit.
149+
</ResponseField>
150+
<ResponseField name="commitRef" type="string" optional>
151+
The ref of the commit.
152+
</ResponseField>
153+
<ResponseField name="commitSha" type="string" optional>
154+
The SHA of the commit.
155+
</ResponseField>
156+
<ResponseField name="dirty" type="boolean" optional>
157+
Whether the commit is dirty, i.e. there are uncommitted changes.
158+
</ResponseField>
159+
<ResponseField name="remoteUrl" type="string" optional>
160+
The remote URL of the repository.
161+
</ResponseField>
162+
<ResponseField name="pullRequestNumber" type="number" optional>
163+
The number of the pull request.
164+
</ResponseField>
165+
<ResponseField name="pullRequestTitle" type="string" optional>
166+
The title of the pull request.
167+
</ResponseField>
168+
<ResponseField name="pullRequestState" type="string" optional>
169+
The state of the pull request (open, closed, or merged).
170+
</ResponseField>
171+
</Expandable>
172+
</ResponseField>
135173
</Expandable>
136174
</ResponseField>
137175

259 KB
Loading
53 KB
Loading

docs/deployment/preview-branches.mdx

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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+
![Preview environment and branches](/deployment/preview-environment-branches.png)
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+
![Preview branches page](/deployment/preview-branches.png)
147+
![Create preview branch](/deployment/preview-branches-new.png)
148+
149+
You can also archive a branch:
150+
151+
![Archive preview branch](/deployment/preview-branches-archive.png)
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+
![Environment variables](/deployment/preview-environment-variables.png)
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+
```

docs/deployment/preview-branches.png

252 KB
Loading
18.8 KB
Loading
Loading

0 commit comments

Comments
 (0)