Skip to content

Commit 99500c2

Browse files
authored
Merge pull request #4 from edwardshturman/main
Add error handling for Vercel Deployment Protection
2 parents 46d33ad + 6f17837 commit 99500c2

File tree

14 files changed

+325
-195
lines changed

14 files changed

+325
-195
lines changed

.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ yarn-error.log*
3838
# typescript
3939
*.tsbuildinfo
4040
next-env.d.ts
41+
42+
# env
43+
.env*

README.md

Lines changed: 75 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,111 @@
11
# Next.js & Checkly Starter Template
22

3-
This repo showcases a simple Next.js app, hosted on Vercel, that uses Checkly to run checks before and after deployment in CI.
4-
It includes the following features:
3+
This repo showcases a simple Next.js app, hosted on Vercel, that uses Checkly to run checks before and after deployment in CI. It includes the following features:
4+
55
1. A Next.js app that fetches data from the `/api/greetings` endpoint and displays it on the landing page.
66
2. Checkly checks in the `__checks__` directory verify if the page loads and if the API responds correctly.
77
3. The necessary Checkly CLI and GitHub Actions configuration to run these checks in CI.
88

99
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fcheckly%2Fnextjs-checkly-starter-template)
1010

11-
## Quickstart
11+
## Getting started
12+
13+
There are a couple ways to use this template:
14+
15+
- Forking the repo / clicking "Use this template" in the top right corner
16+
- Deploying the official [Vercel template](https://vercel.com/templates/Next.js/nextjs-checkly)
17+
18+
Either way, get started by cloning your repo and installing dependencies:
19+
20+
```bash
21+
git clone https://github.com/checkly/nextjs-checkly-starter-template.git # replace with your repo link
22+
cd nextjs-checkly-starter-template
23+
npm i
24+
```
25+
26+
### Vercel Deployment Protection
27+
28+
Before running our checks, let's make sure Vercel Deployment Protection is configured to work with Checkly. If you chose to fork or use the template on GitHub, go ahead and deploy your repo to Vercel [in the dashboard](https://vercel.com/new) or with the CLI:
29+
30+
```bash
31+
vercel
32+
```
33+
34+
By default, Vercel Preview Deployments are protected and only accessible by logged-in members of your Vercel team. We want to be able to access these from our Checkly checks as well. We can bypass Vercel Deployment Protection by either:
35+
36+
- Enabling [Protection Bypass for Automation](https://vercel.com/docs/deployment-protection/methods-to-bypass-deployment-protection/protection-bypass-automation)
37+
- Disabling Vercel Deployment Protection altogether for this example repo
38+
39+
![Disable Vercel Deployment Protection](assets/vercel_deployment_protection.png)
40+
41+
If you choose to keep Deployment Protection on, ensure:
1242

13-
1. Fork this repo or click the "Use this template" button in the top right corner.
14-
2. Clone the repo and run `npm install`.
43+
1. The **Automatically expose [System Environment Variables](https://vercel.com/docs/environment-variables/system-environment-variables)** setting (in **Project Settings > Environment Variables**) is checked; and
44+
2. You create a `VERCEL_AUTOMATION_BYPASS_SECRET` in **Project Settings > Deployment Protection**. Our app will automatically use this for fetching from the `/api/greetings` endpoint.
1545

16-
```bash
17-
git clone https://github.com/checkly/nextjs-checkly-starter-template.git
18-
cd nextjs-checkly-starter-template
19-
npm i
20-
```
46+
Redeploy after applying any changes to your project settings in Vercel. Each deployment will alert you of anything it's missing on the landing page.
2147

22-
3. Deploy the app to Vercel. You should now have a stable, production Vercel URL for your app similar to `https://nextjs-checkly-starter-template-checkly.vercel.app/`
23-
4. Copy and paste that URL into the `.env` file as the `PRODUCTION_URL` value, e.g.
48+
## Running checks
2449

25-
```
26-
PRODUCTION_URL="YOUR VERCEL PRODUCTION URL"
27-
```
28-
Now we know where to aim our production checks.
29-
5. Make sure you have a Checkly account. Just run this command and follow the instructions:
30-
31-
```bash
32-
npx checkly login
33-
```
34-
6. Run your checks in the Checkly cloud with the following commands:
35-
36-
```bash
37-
npx checkly test --env-file "./.env" --record
38-
```
39-
This will run the checks in the `__checks__` directory and record them in your Checkly account as [test session](https://www.checklyhq.com/docs/testing/#test-sessions). You can now see them in the [Checkly test sessions dashboard](https://app.checklyhq.com/test-sessions).
50+
### Configuring testing & monitoring for production
4051

52+
Use the Checkly CLI to login, or [create an account](https://app.checklyhq.com/signup) if you don't already have one.
4153

42-
7. To deploy your checks as monitors, run the following commands:
54+
```bash
55+
npx checkly login
56+
```
4357

44-
```bash
45-
npx checkly env add "PRODUCTION_URL" "<YOUR VERCEL PRODUCTION URL>"
46-
```
47-
This command persists the `PRODUCTION_URL` to the Checkly cloud.
58+
We have a Vercel Production URL for our app, similar to `https://nextjs-checkly-starter-template-checkly.vercel.app`. Let's add this to our Checkly environment variables:
4859

49-
```bash
50-
npx checkly deploy
51-
```
52-
This command deploys the checks in the `__checks__` directory as monitors in your Checkly account. You can now see them in the [Checkly home dashboard](https://app.checklyhq.com/).
60+
```bash
61+
npx checkly env add "PRODUCTION_URL" "<your Vercel Production URL>"
62+
```
5363

64+
Now would be a good time to also add your Vercel Deployment Protection bypass secret, if you created one earlier:
5465

55-
## 2. Running Checks before & after Deployment in CI
66+
```bash
67+
npx checkly env add --secret "VERCEL_AUTOMATION_BYPASS_SECRET" "<secret>"
68+
```
5669

57-
You can run your Checkly checks right after any **Vercel Preview Deployment** and then deploy your checks as
58-
monitors on Checkly. This is a powerful strategy to make sure your never ship critical breaking errors
59-
to **Production**, while at the same time surfacing any outages in your **Production Deployments**.
70+
We're ready to go! Let's run our checks in the `__checks__` directory in the Checkly cloud, and record them as a [test session](https://checklyhq.com/docs/testing/#test-sessions):
6071

61-
### 2.1 Bypassing Vercel Preview Deployment Protection
72+
```bash
73+
npx checkly test --record
74+
```
6275

63-
By default, Vercel Preview Deployments are protected and only accessible by logged-in users. However, we want to access
64-
any Preview Deployment with our Playwright-powered Checkly checks. To do this, we need to bypass the protection.
76+
Check it out in the [Checkly test sessions dashboard](https://app.checklyhq.com/test-sessions).
6577

66-
For the sake of this example repo, you can just disable this protection in the **Settings / Deployment Protection** section
67-
in your Vercel project.
78+
You can also deploy the checks as monitors:
6879

69-
![disable deployment protection](assets/vercel_deployment_protection.png)
80+
```bash
81+
npx checkly deploy
82+
```
7083

71-
In a real-world scenario, you would create a [Vercel protection bypass token](https://www.checklyhq.com/docs/cicd/vercel-deployment-protection/) and use that in your Checkly scripts to
72-
authenticate against the Preview Deployment.
84+
Great! We're all set up with testing & monitoring our Production Deployments, which you can check out in the [Checkly home dashboard](https://app.checklyhq.com). Let's do the same for our Vercel Preview environment.
7385

74-
### 2.2 Setting up GithHub Actions
86+
### Running checks in CI
7587

76-
This example uses GitHub Actions. Check out the workflow in `.github/workflows/checkly.yml` but you can any other CI platform.
77-
We have [example configs for Jenkins and GitLab CI in our docs](https://www.checklyhq.com/docs/cicd/).
88+
You can run your Checkly checks right after any Vercel Preview Deployment, and then deploy your checks as monitors on Checkly. This is a powerful strategy to make sure you never ship breaking errors to Production, while at the same time surfacing any outages in your Production Deployments.
7889

90+
This example uses GitHub Actions — check out the workflow in [`.github/workflows/checkly.yml`](/.github/workflows/checkly.yml) — but you can use any other CI platform. We have [example configs for Jenkins and GitLab CI in our docs](https://checklyhq.com/docs/cicd).
7991

80-
1. Create an API key [in the API keys section of your Checkly account](https://app.checklyhq.com/settings/user/api-keys)
81-
2. Take a note of your [Checkly Account ID in the General section of your Checkly account](https://app.checklyhq.com/settings/account/general)
82-
3. Save your API key and Account ID as `CHECKLY_API_KEY` and `CHECKLY_ACCOUNT_ID` as **secrets** in your GitHub Actions configuration.
92+
1. Create an API key in [**User Settings > API keys**](https://app.checklyhq.com/settings/user/api-keys).
93+
2. Take note of your Checkly Account ID in [**Account Settings > General**](https://app.checklyhq.com/settings/account/general).
94+
3. Save your API key and Account ID as secrets named `CHECKLY_API_KEY` and `CHECKLY_ACCOUNT_ID` in your GitHub Actions configuration.
8395

8496
![GitHub Actions Secret Page](assets/gh_actions_secrets.png)
8597

8698
Now, on every deployment webhook that GitHub receives from Vercel, the GitHub Actions workflow will run the checks in the `__checks__` directory.
8799

88-
- A markdown formatted report will be posted as a comment in the GitHub Actions summary.
89-
- Preview Deployments are tested against the generated preview deployment URL and recorded as test sessions in Checkly.
90-
- Production Deployments are tested against the production URL and deployed as monitors in Checkly if all checks pass.
100+
- A Markdown-formatted report will be posted as a comment in the GitHub Actions summary.
101+
- Preview Deployments are tested against the generated Preview Deployment URL, and recorded as test sessions in Checkly.
102+
- Production Deployments are tested against the Production URL, and deployed as monitors in Checkly if all checks pass.
91103

92-
Links:
93-
- [Checkly docs on Test Sessions](https://www.checklyhq.com/docs/testing/#test-sessions)
94-
- [Checkly docs on GitHub Actions integration](https://www.checklyhq.com/docs/cicd/github-actions/)
95-
- [Vercel docs on running tests](https://vercel.com/guides/how-can-i-run-end-to-end-tests-after-my-vercel-preview-deployment)
104+
That's it for this example! If you have questions or feedback, please drop us a note in our [Slack community](https://checklyhq.com/slack).
96105

97-
## Notes
106+
## Further reading
98107

99-
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app)
100-
and edited where needed.
108+
- Official [Checkly integration from the Vercel Marketplace](https://vercel.com/integrations/checkly)
109+
- Checkly docs on [integrating with Vercel](https://checklyhq.com/docs/cicd/vercel)
110+
- Checkly docs on [integrating with GitHub Actions](https://checklyhq.com/docs/cicd/github-actions)
111+
- Vercel docs on [running tests](https://vercel.com/guides/how-can-i-run-end-to-end-tests-after-my-vercel-preview-deployment)

__checks__/api.check.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import { ApiCheck, Frequency, AssertionBuilder, RetryStrategyBuilder } from 'checkly/constructs'
1+
import { ApiCheck, Frequency, AssertionBuilder, RetryStrategyBuilder } from "checkly/constructs"
22

3-
new ApiCheck('api-check-1', {
4-
name: 'Greetings API',
3+
new ApiCheck("api-check-1", {
4+
name: "Greetings API",
55
frequency: Frequency.EVERY_1H,
6-
tags: ['api'],
7-
locations: ['us-east-1', 'us-west-1'],
6+
tags: ["api"],
7+
locations: ["us-east-1", "us-west-1"],
88
runParallel: true,
99
setupScript: {
10-
content: "process.env.BASE_URL = process.env.ENVIRONMENT_URL || process.env.PRODUCTION_URL",
10+
content: "process.env.BASE_URL = process.env.ENVIRONMENT_URL || process.env.PRODUCTION_URL"
1111
},
1212
request: {
13-
url: '{{BASE_URL}}/api/greetings',
14-
method: 'GET',
13+
url: "{{BASE_URL}}/api/greetings",
14+
method: "GET",
15+
headers: [
16+
{ key: "x-vercel-protection-bypass", value: "{{VERCEL_AUTOMATION_BYPASS_SECRET}}" }
17+
],
1518
assertions: [
1619
AssertionBuilder.statusCode().equals(200),
17-
AssertionBuilder.jsonBody('$[0].text').notEmpty()
20+
AssertionBuilder.jsonBody("$[0].text").notEmpty()
1821
]
1922
},
2023
retryStrategy: RetryStrategyBuilder.exponentialStrategy({

__checks__/landing-page.check.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import {BrowserCheck, Frequency, RetryStrategyBuilder} from "checkly/constructs";
1+
import {BrowserCheck, Frequency, RetryStrategyBuilder} from "checkly/constructs"
22

3-
new BrowserCheck('landing-page-check-1', {
4-
name: 'Landing Page Check',
3+
new BrowserCheck("landing-page-check-1", {
4+
name: "Landing Page Check",
55
frequency: Frequency.EVERY_1H,
6-
tags: ['web'],
7-
locations: ['us-east-1', 'us-west-1'],
6+
tags: ["web"],
7+
locations: ["us-east-1", "us-west-1"],
88
runParallel: true,
99
code: {
10-
entrypoint: './landing-page.spec.ts'
10+
entrypoint: "./landing-page.spec.ts"
1111
},
1212
retryStrategy: RetryStrategyBuilder.exponentialStrategy({
1313
maxRetries: 3,

__checks__/landing-page.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { test, expect } from '@playwright/test'
1+
import { test, expect } from "@playwright/test"
22

3-
test('Landing page', async ({ page }) => {
3+
test.use({
4+
extraHTTPHeaders: {
5+
"x-vercel-protection-bypass": process.env.VERCEL_AUTOMATION_BYPASS_SECRET || ""
6+
}
7+
})
8+
9+
test("Landing page", async ({ page }) => {
410
const baseURL = process.env.ENVIRONMENT_URL! || process.env.PRODUCTION_URL!
511
const response = await page.goto(baseURL)
612
expect(response?.status()).toBeLessThan(400)
7-
await page.screenshot({ path: 'test-results/screenshot/landing-page.jpg' })
8-
await expect(page.getByText('Go to the GitHub repo')).toBeVisible()
13+
await page.screenshot({ path: "test-results/screenshot/landing-page.jpg" })
14+
await expect(page.getByText("Go to the GitHub repo")).toBeVisible()
915
})

checkly.config.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
1-
import { defineConfig } from 'checkly'
1+
import { defineConfig } from "checkly"
22

33
/**
44
* See https://www.checklyhq.com/docs/cli/project-structure/
55
*/
66
const config = defineConfig({
77
/* A human friendly name for your project */
8-
projectName: 'Next.js starter template',
8+
projectName: "Next.js starter template",
99
/** A logical ID that needs to be unique across your Checkly account,
1010
* See https://www.checklyhq.com/docs/cli/constructs/ to learn more about logical IDs.
1111
*/
12-
logicalId: 'nextjs-template-project',
12+
logicalId: "nextjs-template-project",
1313
/* An optional URL to your Git repo */
14-
repoUrl: 'https://github.com/checkly/nextjs-starter-template',
14+
repoUrl: "https://github.com/checkly/nextjs-starter-template",
1515
/* Sets default values for Checks */
1616
checks: {
1717
/* A default for how often your Check should run in minutes */
1818
frequency: 10,
1919
/* Checkly data centers to run your Checks as monitors */
20-
locations: ['us-east-1', 'us-west-1'],
20+
locations: ["us-east-1", "us-west-1"],
2121
/* An optional array of tags to organize your Checks */
22-
tags: ['nextjs'],
22+
tags: ["nextjs"],
2323
/** The Checkly Runtime identifier, determining npm packages and the Node.js version available at runtime.
2424
* See https://www.checklyhq.com/docs/cli/npm-packages/
2525
*/
26-
runtimeId: '2024.09',
26+
runtimeId: "2024.09",
2727
/* A glob pattern that matches the Checks inside your repo, see https://www.checklyhq.com/docs/cli/using-check-test-match/ */
28-
checkMatch: '**/__checks__/**/*.check.ts',
28+
checkMatch: "**/__checks__/**/*.check.ts",
2929
/* Global configuration option for Playwright-powered checks. See https://www.checklyhq.com/docs/browser-checks/playwright-test/#global-configuration */
3030
playwrightConfig: {
3131
use: {
3232
viewport: {
3333
width: 1920,
34-
height: 1080,
34+
height: 1080
3535
}
3636
},
3737
expect: {
38-
timeout: 10000,
38+
timeout: 10000
3939
}
4040
},
4141
browserChecks: {
4242
/* A glob pattern matches any Playwright .spec.ts files and automagically creates a Browser Check. This way, you
4343
* can just write native Playwright code. See https://www.checklyhq.com/docs/cli/using-check-test-match/
4444
* */
45-
testMatch: '**/__checks__/**/*.spec.ts',
46-
},
45+
testMatch: "**/__checks__/**/*.spec.ts"
46+
}
4747
},
4848
cli: {
4949
/* The default datacenter location to use when running npx checkly test */
50-
runLocation: 'us-east-1',
50+
runLocation: "us-east-1",
5151
/* An array of default reporters to use when a reporter is not specified with the "--reporter" flag */
52-
reporters: ['list'],
52+
reporters: ["list"],
5353
/* How many times to retry a failing test run when running `npx checkly test` or `npx checkly trigger` (max. 3) */
54-
retries: 0,
55-
},
54+
retries: 0
55+
}
5656
})
5757

5858
export default config

next.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { NextConfig } from "next";
1+
import type { NextConfig } from "next"
22

33
const nextConfig: NextConfig = {
44
/* config options here */
5-
};
5+
}
66

7-
export default nextConfig;
7+
export default nextConfig

postcss.config.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/** @type {import('postcss-load-config').Config} */
1+
/** @type {import("postcss-load-config").Config} */
22
const config = {
33
plugins: {
4-
tailwindcss: {},
5-
},
6-
};
4+
tailwindcss: {}
5+
}
6+
}
77

8-
export default config;
8+
export default config

src/app/api/greetings/route.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)