Was this article helpful?
Thanks for your feedback
Having the ability to publish entries and assets in bulk on the specified environments can ease the process of content management. And with Contentstack's bulk-publish-entries-assets script, you can automate the process of bulk publishing your entries and assets.
In this guide, we will look at different scenarios of bulk publishing and how to use the bulk-publish-entries-assets script in each scenario.
Note: If you have the Bulk Publish feature enabled for your plan, this script will make use of Bulk APIs. Make sure you keep the bulkPublish and bulkUnpublish flags to true in the config. If it’s not part of your plan, it will publish the items in queue, one at a time.
We will discuss the use of this script in the following scenarios:
Additional Resources: If you want to perform bulk publish/unpublish operations on entries and assets using command-line interface (CLI), refer to its CLI documentation.
Before we start discussing the scenarios and how to use this script, we need to install the required dependencies and make certain changes in the configuration file.
npm install
module.exports = { apikey:'YOUR_STACK_API_KEY', apiEndPoint:'https://api.contentstack.io', cdnEndPoint:'https://cdn.contentstack.io', manageToken:'YOUR_STACK_MANAGEMENT_TOKEN', }
Note: The above configuration is common for all scenarios. If the Bulk Publish plan is not enabled for your organization, keep bulkPublish and bulkUnpublish flags as false in the config of all cases
Let's now move ahead and discuss the scenarios and how to use this utility.
This is the scenario where you want to publish the unpublished entries or the draft entries on a particular environment.
publish_unpublished_env: { contentTypes:['test'], //list of contentTypes sourceEnv : 'staging', //sourceEnv environments:['staging'], //target environment locale:’en-us’ //your locale bulkPublish:true //keep false if the bulk operation is enabled in your plan }
npm run publish_unpublish
The above command will retrieve all entries of ‘test’ contentType not published in ‘staging’ environment with ‘en-us’ locale and publish it on the destination environment ("staging" in our case).
This is a scenario where you want to publish all assets of a particular stack in bulk. To do this, follow the below steps:
module.exports = { publish_assets:{ environments:['bulktest'], //name of the environment folderUid:’cs_root //UID of the folder, ‘cs_root’ for all assets bulkPublish: true //keep false if the bulk operation is enabled in your plan } }
npm run publish_assets
The above command will publish all the assets of the stack (the ID of which you provided in the main configuration file at the top).
This is a scenario where you want to publish all the entries of the specified content types in a stack.
publish_entries:{ contentTypes:['redirect_rule'], //list of contentTypes to be published locales:['en-us'], //list of locales to be considered for mentioned Content types environments:['bulktest'], //destination publish environments publishAllContentTypes : false, //if you want to publish all contentTypes bulkPublish: true, }
npm run publish_entries
Note: It is recommended to try publishing entries of one content type at a time. In this way, you can avoid the chances of entries failing to get published in bulk.
Your content types may have thousands of entries and assets and unpublishing them manually can be a tedious task. Using this script, you can automate the process of unpublishing all entries and assets of the stack that were published on a particular environment.
bulkUnpublish :{ filter:{ environment: 'bulktest', //source environment content_type_uid: '', //add content type uid to be unpublished,keep blank to consider all locale: 'en-us', //locale filters type:'entry_published,asset_published' //entries and assets both will be unpublished, remove asset_published if u want to unpublish only entries and vice versa. }, deliveryToken:'' , //delivery token of the source environment bulkUnpublish: true, }
npm run unpublish
If you want to unpublish only the entries of specific content type, specify its UID in the content_type_uid filter in the above config. To unpublish only assets, remove entry_published from the type filter or remove asset_published if you want to unpublish only entries.
This is a common scenario where you want to publish the entries, which were recently edited, on a particular environment.
publish_edits_on_env: { contentTypes:['test','helloworld'], // array of contentTypes where edits needs to be checked sourceEnv : 'production', //environment where entry edits needs to be checked publishEnvironments:['production'], //publishing environment (usually same as sourceEnv) locales:['en-us',], // locales of entries where it needs to be checked bulkPublish: true, },In the above config, you need to provide the UID of the content type such as "test", "helloworld", and so on). Source environment is the environment on which the entries were initially published (in our case it is "production"). Publish Environment is the new environment on which the edited entries are to be published (in our case it is "production"). Lastly, provide the locale details (in our case it is "en-us").
npm run publish_edits
The above command will publish the edited entries on the target environment that you have specified in the above config file.
This is again one of the common scenarios where you want to publish the content from one environment to a newly created environment or to some other environment.
cross_env_publish:{ filter: { environment: 'bulktest', // source environment content_type_uid: '', // //Add content type uid to be published. Keep this blank to consider all (keep this empty while publishing only assets) locale: 'en-us', // locale filters. Keep this blank to consider all type: 'asset_published,entry_published', //entries and assets both will be published, remove asset_published if you want to publish only entries and vice versa. } deliveryToken: '', //delivery token of the source environment destEnv:[‘’], //environment where it needs to be published bulkPublish:true }
npm run cross_publish
The above command will publish the entries (of the mentioned content types) and assets from the "bulktest" environment to the environment you have mentioned in the config.
This is a scenario where you want to publish those entries in bulk that had their non-localized field changed. Read more about Non-localized Field.
nonlocalized_field_changes: { sourceEnv: 'production', //source Environment contentTypes: ['testdin'], environments: ['production'], //publishing Environments bulkPublish:true },
npm run publish_localized
The above command will publish the entries (of the specified content types) from the source to the publishing environment.
This is one of the most common scenarios in content management. Often, we keep adding a new field in a content type as and when required. And there could be multiple entries in your content types. Publishing all such entries manually can be time-consuming, but with this script, you can automate this task.
To publish such entries of content types in bulk, follow the steps given below:
addFields: { deleteFields: ['updated_by', 'created_by', 'created_at', 'updated_at', '_version', 'ACL'], locales: ['en-us'], contentTypes: ['helloworld'], // list to contentType entries to be updated environments: ['test'], // list of environments where it needs to be published defaults: { number: null, boolean: false, isodate: [], file: null, reference: [], }, bulkpublish:true, },
npm run add_fields
The above command will publish the entries (of the specified content types that had a field added or updated) on the specified environment.
Note: This script is not applicable to custom and mandatory fields.
To understand this scenario, consider an example. There are 10 entries in a content type. Out of these 10, 4 entries are published on version 4 and 6 of them were never published.
So if you publish all entries of this content type using the script, let's say entries which were at version 4 will now be published at version 5 and the unpublished ones will be published with version 1.
Now, if you want to undo this operation and restore entries to their previous state, use the following command:
$ npm run revert ${logFilename}
In the above command, logFilename is the success logs of a particular execution (like a log of successful publishing of entries to version 5 and 1 as in the above example).
For example:
npm run revert 1587270350288.bulkPublishEntries.success
When you use this script to publish the entries in bulk, there might be a few entries that could not be published due to some reason. The record of such entries is stored in the logs directory with a unique name or identifier ending with .error.
You can retry publishing such entries using the same script with the retryFailed flag along with {logFilename} as shown below:
npm run publish_entries -- -retryFailed ${logFilename}
npm run publish_assets -- -retryFailed ${logFilename}
For example:
npm run publish_entries -- -retryFailed 18003bulkPublishEntries.error
Was this article helpful?
Thanks for your feedback