|
1 | 1 | # Next.js & Checkly Starter Template |
2 | 2 |
|
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 | + |
5 | 5 | 1. A Next.js app that fetches data from the `/api/greetings` endpoint and displays it on the landing page. |
6 | 6 | 2. Checkly checks in the `__checks__` directory verify if the page loads and if the API responds correctly. |
7 | 7 | 3. The necessary Checkly CLI and GitHub Actions configuration to run these checks in CI. |
8 | 8 |
|
9 | 9 | [](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fcheckly%2Fnextjs-checkly-starter-template) |
10 | 10 |
|
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 | + |
| 40 | + |
| 41 | +If you choose to keep Deployment Protection on, ensure: |
12 | 42 |
|
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. |
15 | 45 |
|
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. |
21 | 47 |
|
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 |
24 | 49 |
|
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 |
40 | 51 |
|
| 52 | +Use the Checkly CLI to login, or [create an account](https://app.checklyhq.com/signup) if you don't already have one. |
41 | 53 |
|
42 | | -7. To deploy your checks as monitors, run the following commands: |
| 54 | +```bash |
| 55 | +npx checkly login |
| 56 | +``` |
43 | 57 |
|
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: |
48 | 59 |
|
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 | +``` |
53 | 63 |
|
| 64 | +Now would be a good time to also add your Vercel Deployment Protection bypass secret, if you created one earlier: |
54 | 65 |
|
55 | | -## 2. Running Checks before & after Deployment in CI |
| 66 | +```bash |
| 67 | +npx checkly env add --secret "VERCEL_AUTOMATION_BYPASS_SECRET" "<secret>" |
| 68 | +``` |
56 | 69 |
|
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): |
60 | 71 |
|
61 | | -### 2.1 Bypassing Vercel Preview Deployment Protection |
| 72 | +```bash |
| 73 | +npx checkly test --record |
| 74 | +``` |
62 | 75 |
|
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). |
65 | 77 |
|
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: |
68 | 79 |
|
69 | | - |
| 80 | +```bash |
| 81 | +npx checkly deploy |
| 82 | +``` |
70 | 83 |
|
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. |
73 | 85 |
|
74 | | -### 2.2 Setting up GithHub Actions |
| 86 | +### Running checks in CI |
75 | 87 |
|
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. |
78 | 89 |
|
| 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). |
79 | 91 |
|
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. |
83 | 95 |
|
84 | 96 |  |
85 | 97 |
|
86 | 98 | Now, on every deployment webhook that GitHub receives from Vercel, the GitHub Actions workflow will run the checks in the `__checks__` directory. |
87 | 99 |
|
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. |
91 | 103 |
|
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). |
96 | 105 |
|
97 | | -## Notes |
| 106 | +## Further reading |
98 | 107 |
|
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) |
0 commit comments