Gatsby
his document provides instructions for working with Gatsby and the CommandK CLI. It explains how to run your Gatsby application and manage secrets effectively using the CommandK CLI.
Prerequisites
Before you begin the integration process, make sure you have the following:
- API Access Token: To authenticate your Gatsby application with CommandK, you'll need an API access token. If you haven't obtained one yet, refer to this link for instructions on how to acquire it.
Integration Steps
Follow these steps to integrate CommandK with your Gatsby application:
Step 1: Create a Gatsby Application
If you haven't already created a Gatsby application, you can set up a new project using the Gatsby CLI or any other preferred method. Here, we'll use the Gatsby CLI for demonstration:
-
Install the Gatsby CLI globally if you haven't already:
npm install -g gatsby-cli
-
Create a new Gatsby project using the following command:
gatsby new my-gatsby-app
Replace
my-gatsby-app
with your desired project name. -
Follow the prompts to configure your Gatsby project. You can choose options such as the starter template, package manager, and more based on your project requirements.
Step 2: Access Secrets in Your Gatsby Application
Inside your Gatsby application, you can access secrets as environment variables. Open the appropriate file (e.g., gatsby-config.js
) and add the following code snippet to access an environment variable:
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
});
module.exports = {
// ...
plugins: [
// ...
],
};
This code uses the dotenv
library to load environment variables from an .env
file based on the current NODE_ENV
.
Running Your Gatsby Application
To run your Gatsby application with the CommandK CLI, you can use the following command:
$ cmdk run <application-name> --environment development -- npm run develop
Replace <application-name>
with the actual name of your Gatsby application. This command will execute your Gatsby application in the development environment.
Running with dotenv
If your Gatsby application uses dotenv to manage environment variables, you can utilize the CommandK CLI to write these variables to a .env.development
file and then run your service. Follow these steps:
- Execute the following command:
$ cmdk run <application-name> --environment development \
--run-type file-store \
--file-name .env.development.local \
--file-format env \
-- gatsby develop
Replace <application-name>
with the actual name of your Gatsby application. This command will write your environment variables to the .env.development.local
file in the specified env format and then run your Gatsby service.
Adjust the file name and format to match your project's configuration if necessary.
By following these instructions, you can seamlessly integrate the CommandK CLI with your Gatsby application, making it easy to manage secrets and run your application securely.
Updated 11 months ago