Import Content using the Seed Command
The seed command in Contentstack CLI allows users to import content to your stack, from GitHub repositories. It's an effective command that can help you to migrate content to your stack with minimal steps.
To import content to your stack, you can choose from the following two sources:
- Contentstack’s GitHub Organization: In this organization, we have provided sample content, which you can import directly to your stack using the seed command. Learn more on how to import from Contentstack’s GitHub organization .
- Non-Contentstack's GitHub Repository: You can import stack content available on GitHub’s repository belonging to any organization or individual. Learn more on how to import from Non-Contentstack's GitHub repository.
Prerequisites
- Contentstack account
- Node.js version 20 or above
- CLI installed on your machine: npm install -g @contentstack/cli
Now let’s import content to your stack by performing the following steps:
Log in to the CLI Session or use the Management Token
Note: By default, this CLI session will work for the app hosted in the North America region. If you want to switch to Europe, Azure North America, Azure Europe, or Google North America region, follow the steps mentioned in the Set Region command section.
Run the Seed command by logging in to the CLI
To use the seed command, you will first have to generate the authtoken. To generate the authtoken, open your terminal, and run the following command to log in to your Contentstack account:
csdx auth:login
It will ask you to provide your email address and password of your Contentstack account. Once you log in successfully, an authtoken will be generated and saved to the CLI session until you log out from this session.
Run the Seed command using the Management Token
You can use the management token alias to run the seed command without the need of logging in to CLI.
To run the seed command using the management token alias, open your terminal and run the following command:
csdx cm:stacks:seed --alias <management_token_alias>
Additional Resource: To learn more about the login command, refer to the Login command section.
Use the “seed” command
To use the seed command, you can choose from the following options to import the content to your stack, as mentioned above.
Note: If you want to upload your stack’s content onto your GitHub repository for other users to import it, refer to the steps mentioned in the upload stack's content on the GitHub section.
Let's discuss the above two options in detail.
Import from Contentstack’s GitHub Organization
In this option, you can import content that Contentstack provides from its GitHub organization. To use this option, run the following command in your terminal:
csdx cm:stacks:seed
This command prompts the following options:
- Stack to Import: You will get a list of stacks from which you must choose the stack which you want to import.
- Organization name: You will get a list of organizations to which you have access. Select the one from the list where your source stack is located or where you want to create a new stack.
Note: Choose an organization where you are an owner or admin.
- Stack preference: Next, you will get an option to create a new stack or use an existing stack.
- If you select New, you must enter the stack name, and the stack creation process will start.
- If you select Existing, you will get a list of stacks to which you have access in the organization. Choose the destination stack where you want to import the content. If the existing stack has some content, it will ask for a confirmation to continue with the same stack.
Additional resource: Refer to the Stack Roles documentation to learn more about permissions.
Tip: To avoid the chances of any error, we recommend that you select the option of creating a new destination stack. If you import content to an existing stack, ensure that the stack is empty.
Finally, you’ll get the content imported to the destination stack.
Import from Non-Contentstack's GitHub Repository
In this option, you can import content from your GitHub repository to your stack. However, first, ensure the content is available on your GitHub repository. To upload content on GitHub, perform the steps specified in the upload stack’s content on GitHub section.
Note: The GitHub repository needs to be public.
To import content from the GitHub repository, run the seed command in the following format:
csdx cm:stacks:seed --repo “<account/repository>”
After running this command, you'll be prompted to select an organization and a stack, as discussed in the Import from Contentstack’s GitHub Organization section.
The alternate way of using the plugin is by providing the required parameters after the command in a single line.
Options:
- --repo=repo: GitHub organization name or GitHub user name/ repository name.
- -k,--stack-api-key=stack-api-key: Provide stack API key to seed content to.
- -l,--fetch-limit: Limit for number of organizations or stacks to be fetched.
- --org=org: Provide the organization UID to create a new stack.
- -n,--stack-name=stack-name: Provide the name of the new stack that needs to be created.
Examples:
- To import stack content from the “blog” repository belonging to the “Stackcontent” organization, the seed command should follow this format:
csdx cm:stacks:seed --repo “Stackcontent/blog"
Likewise, you can use the above format if the stack content resides in an individual’s GitHub repository by replacing the organization’s name with the individual’s username.
To import content to a specific stack use the following command:
csdx cm:stacks:seed --repo "<account/repository> -k "<stack-api-key>"
Use the following command to create a new stack in an organization and import your content:
csdx cm:stacks:seed --repo "<account/repository>" --org "<org-uid>" -n "<stack-name>"
The above command creates a new stack in your organization and imports the content in that stack.
Upload Stack’s Content on GitHub
To upload your stack’s content to your recently created GitHub repository, first, you need to export the stack’s content. To do so, follow these steps:
Note: We assume that you are familiar with GitHub and Git. If not, go through GitHub’s documentation to create and upload files to a GitHub repository.
Before uploading stack content on your GitHub repository, you need to create a public repository in this format: stack-<name>. For example, stack-exportdata, stack-gatsby-website, etc.
- Create a folder named “stack” in your machine.
- Then, open your terminal and run this command to log in to the CLI session:This command will ask you to provide your Contentstack account credentials.
csdx auth:login
- After successful login, run this command to export your stack’s content to the “stack” folder:For example:
csdx cm:stacks:export -k <stack_ApiKey> -d “<path_to_store_content>”
csdx cm:stacks:export -k blt********** -d “C:\Users\Name\Desktop\content\stack”
Note: If the management token is not specified, by default export command uses Auth token.
This command will export your stack’s content and save it to the “stack” folder. - Open your Git Bash terminal and follow the steps specified in GitHub’s documentation to upload the “stack” folder to your repository.
Additional Resource: Refer to this sample repository present in Contentstack's GitHub organization to learn the folder structure while uploading content to your GitHub repository.
Note: To avoid the following error make sure to create a GitHub release and proceed to import content.
Error: Unable to find a release for '<account>/<repository>'
Now any user can import your stack’s content to their stack by performing the steps mentioned in the Import from Non-Contentstack's GitHub Repository section.