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.
Now let’s import content to your stack by performing the following steps:
- Contentstack account
- Node.js version 8 or above
- CLI installed on your machine: npm install -g @contentstack/cli
Log in to the CLI Session
Note: By default, this CLI session will work for the app hosted in the North America region. If you want to switch to Europe or Azure North America region, follow the steps mentioned in the Set Region command section.
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:
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.
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.
Option 1: 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:
After running the above command, it will ask you to provide the following inputs:
- Organization name: You’ll get a list of organizations to which you have access. Select the one from the list where your source stack is located. You can also create a new stack if required.
- Stack preference: To create a new stack, you need to select the organization of your choice:
- If you select Yes, you’ll receive a list of all organizations you are a member of. You can choose an organization to which you either have the owner or the admin rights to create a new stack. Then, provide a name to the newly created stack.
- If you select No, you need to choose the organization where your destination stack resides. You will then need to select that particular stack on which you have permission to import content. Refer to the Stack Roles documentation to learn more about permissions.
Tip: To ensure the above operations are error-free, we recommend that you select the option of creating a new destination stack. This is because the new stack will be empty and you can easily import content to it. If you choose to import content to an existing stack, you first need to ensure that the stack is empty and then proceed with the import operation.
Finally, you’ll get the content imported to the destination stack.
Option 2: 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 option 1 section.
The alternate way of using the plugin is by providing the required parameters after the command in a single line.
- --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.
- 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-uid>"
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.
- 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.
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 option 2 section.