|
| 1 | +# Getting started with Gatsby Cloud and Kontent |
| 2 | + |
| 3 | +Learn how to connect Gatsby Cloud with Kentico Kontent based Gatsby site. |
| 4 | + |
| 5 | +In this tutorial you will discover how to easily integrate site sourced by Kentico Kontent with Gatsby Cloud. |
| 6 | + |
| 7 | +You will |
| 8 | + |
| 9 | +* Create a website using data from Kentico Kontent. |
| 10 | +* Store its source Code on GitHub. |
| 11 | +* Register this Github repository in Gatsby Cloud. |
| 12 | +* Configure Kentico Kontent webhooks to notify Gatsby Cloud about the content changes on preview environment as well as on production. |
| 13 | +* Configure preview URLs in Kentico Kontent. |
| 14 | + |
| 15 | +## What are Gatsby Cloud and Kontent, and why use them together |
| 16 | + |
| 17 | +[Kontent](https://kontent.ai) is a headless CMS that content editors can use to edit and publish content. It is a Content-as-a-Service solution that gives enterprises control over their entire content lifecycle in a single unified environment. Gatsby Cloud allows you to integrate your site with Kontent in order to run performant builds and preview content changes made in the CMS before publishing. |
| 18 | + |
| 19 | +## Setting up a Kontent and Gatsby site |
| 20 | + |
| 21 | +First, you’ll need a Gatsby site with a [gatsby-source-kontent](https://www.gatsbyjs.org/packages/@kentico/gatsby-source-kontent) source plugin to pull data from Kontent. If you haven’t set that up yet, you can quickly create a new project by using the [gatsby-starter-kontent-lumen](https://github.com/Kentico/gatsby-starter-kontent-lumen) repository. ~~To achieve it, see the [Getting started](https://github.com/kentico/gatsby-starter-kontent-lumen#getting-started) tutorial.~~ |
| 22 | + |
| 23 | +> Note that source plugin version supporting Gatsby Cloud is in beta phase (internally called [`vNext`](https://github.com/Kentico/gatsby-source-kontent/tree/vNext/packages/gatsby-source-kontent#readme) version), so use the link to the development branch to [Getting started](https://github.com/Simply007/gatsby-starter-kontent-lumen/tree/vNext#getting-started) section. to be able to spin up project supporting Gatsby Cloud. |
| 24 | +
|
| 25 | +## Signing in to Gatsby Cloud |
| 26 | + |
| 27 | +[Access Gatsby Cloud](https://gatsbyjs.com/dashboard/sites/create) and select **Sign in with GitHub**. You’ll be asked to authorize the Gatsby Cloud app with your GitHub account. If you need to request access to one or more repositories, you can click *request access* now or later when creating an instance. |
| 28 | + |
| 29 | +Once signed in, configuring Gatsby Cloud with Kontent requires several steps that are walked through below. |
| 30 | + |
| 31 | +### Creating an instance |
| 32 | + |
| 33 | +Start by [registering you GitHub repository](https://gatsbyjs.com/dashboard/sites/create). |
| 34 | + |
| 35 | +Use the **I already have a Gatsby site** flow to manually integrate your site. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +### Select an organization & repository |
| 40 | + |
| 41 | +Pick your Gatsby site from the list of GitHub repositories. |
| 42 | + |
| 43 | +If you don’t see your site, it might be because it belongs to a GitHub organization rather than your personal account. You can connect a new GitHub Organization. |
| 44 | + |
| 45 | +*Note: Repositories must contain one Gatsby project configured at their root to be enabled. Gatsby Cloud works best with Gatsby version 2.20.36 and higher.* |
| 46 | + |
| 47 | +You’ll need to select a branch and then indicate the publish directory where the `gatsby-config.js` lives. If you leave the field blank, it defaults to the root of the site. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +Once the branch and publish directory are correct, select **Next**. |
| 52 | + |
| 53 | +### Create the instance |
| 54 | + |
| 55 | +Click **Skip this step** to configure Kontent manually. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +*Gatsby Cloud will automatically try and detect environment variables necessary in your `gatsby-config.js`. However — consider adding any additional variables that the automatic detection may have missed. See [Setting up Environment Variables](#setting-up-environment-variables) for more info. You'll be able to add, delete, or update these later on in your Site Settings.* |
| 60 | + |
| 61 | +### Setting up Environment Variables |
| 62 | + |
| 63 | +An environment variable references a value that can affect how running processes will behave in a specific environment, such as staging and production environments. You must save your environment variables in Gatsby Cloud to authorize your instance to pull source data from Kontent. |
| 64 | + |
| 65 | +First, open your Kontent project and go to **Project settings** > **API Keys**. |
| 66 | +On the API keys screen, copy the values for **Project ID** and **Preview API key** (Primary or Secondary). |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +Then go to **Project settings** > **Localization** to get the codenames of your project languages. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +The language codenames used in the [`gatsby-starter-kontent-lumen`](https://github.com/Kentico/gatsby-starter-kontent-lumen) sample app are `en-US,cs-CZ`. |
| 75 | + |
| 76 | +Value | Environment Variable Name |
| 77 | +------------ | ------------- |
| 78 | +<YOUR_PROJECT_ID> | KONTENT_PROJECT_ID |
| 79 | +<YOUR_PREVIEW_API_KEY> | KONTENT_PREVIEW_KEY |
| 80 | +<YOUR_LANGUAGE1,YOUR_LANGUAGE2> | KONTENT_LANGUAGE_CODENAMES |
| 81 | + |
| 82 | +You will want to set `KONTENT_PREVIEW_ENABLED` to `true` for Preview and `false` for Builds. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +Once you’ve entered your variables, click **Save** and then **Create site**. |
| 87 | + |
| 88 | +Go to the *Preview* tab and wait for the preview instance to be created. |
| 89 | + |
| 90 | +## Webhooks: Configuring your Gatsby site to be updated with content changes |
| 91 | + |
| 92 | +To connect Kontent with your Gatsby Cloud instance, you’ll need to [configure a webhook](https://docs.kontent.ai/tutorials/develop-apps/integrate/webhooks#a-creating-a-webhook) in Kontent so that content changes can be pushed to Gatsby Cloud. |
| 93 | + |
| 94 | +You’ll add two webhooks in Kontent: one for Gatsby Preview and another for Gatsby Builds. |
| 95 | + |
| 96 | +### Adding a Preview Webhook |
| 97 | + |
| 98 | +Navigate to your Gatsby Cloud instance and click **Site Settings**. Copy the *Preview Webhook* on this page. |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +In your Kentico Kontent project, go to **Project settings** > **Webhooks** and click **Create new webhook**. |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +Name the webhook and paste the *Preview webhook* into the URL address field. |
| 107 | + |
| 108 | +> Following webhook triggers are currently available via [API](https://docs.kontent.ai/reference/management-api-v2#operation/add-a-webhook) (use `preview_delivery_api_content_changes` as trigers section and `uspert` + `archive` as operations). The UI and documentation is about to be released in the next iteration. (The UI is currently available on QA environment) |
| 109 | +
|
| 110 | +In the webhook configuration, select the *Create or Update* and *Delete* triggers under DELIVERY PREVIEW API TRIGGERS. |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +Click **Save**. |
| 115 | + |
| 116 | +Your Preview webhook is now ready! When you change your content in Kontent, your Gatsby Preview will update! |
| 117 | + |
| 118 | +### Adding a Build Webhook |
| 119 | + |
| 120 | +Navigate to your Gatsby Cloud instance and click **Site Settings**. Copy the Build Webhook on this page. |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +In Kentico Kontent, go to **Project settings** > **Webhooks** and click **Create new webhook**. |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +Name the webhook and paste the *Build webhook* in the URL address field. |
| 129 | + |
| 130 | +In the webhook configuration, select the *Publish* and *Unpublish* triggers under DELIVERY API TRIGGERS. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +Click **Save**. |
| 135 | + |
| 136 | +Your Build webhook is now ready! When you publish/unpublish content in Kontent, your Gatsby Build will update! |
| 137 | + |
| 138 | +## Setting the Gatsby Preview Domain for Kontent |
| 139 | + |
| 140 | +To open the Gatsby Cloud preview right from Kentico Kontent, you need to [configure preview URLs](https://docs.kontent.ai/tutorials/develop-apps/build-strong-foundation/set-up-preview#a-set-up-content-preview-in-your-project) for your content. The following instructions assume you're using the [`gatsby-starter-kontent-lumen`](https://github.com/Kentico/gatsby-starter-kontent-lumen) sample app. |
| 141 | + |
| 142 | +Navigate to your Gatsby Cloud instance and click **Preview**. Copy the preview URL. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +In Kentico Kontent, go to **Project settings** > **Preview URLs** and provide the URLs according to your routes configuration. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +Now you can open the content preview right from Kentico Kontent UI! |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +## Wrapping Up |
| 155 | + |
| 156 | +At this point, you have a Kontent instance configured to best support Gatsby Cloud. Edit content, click the Preview button, and watch it appear live in Gatsby Cloud! |
0 commit comments